You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
954 B

5 years ago
  1. package config
  2. import (
  3. "strings"
  4. "github.com/spf13/viper"
  5. "github.com/urfave/cli"
  6. )
  7. type Config struct {
  8. StoragePath string
  9. Port int
  10. Dest string
  11. AuthNodes []string // PubKs in hex format of the AuthNodes for the blockchain
  12. }
  13. func MustRead(c *cli.Context) (*Config, error) {
  14. var config Config
  15. viper.SetConfigType("yaml")
  16. viper.SetConfigName("config")
  17. viper.AddConfigPath(".") // adding home directory as first search path
  18. viper.SetEnvPrefix("slowlorisdb") // so viper.AutomaticEnv will get matching envvars starting with O2M_
  19. viper.AutomaticEnv() // read in environment variables that match
  20. viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  21. if c.GlobalString("config") != "" {
  22. viper.SetConfigFile(c.GlobalString("config"))
  23. }
  24. if err := viper.ReadInConfig(); err != nil {
  25. return nil, err
  26. }
  27. if err := viper.Unmarshal(&config); err != nil {
  28. return nil, err
  29. }
  30. return &config, nil
  31. }