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