-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
63 lines (52 loc) · 1.55 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package libs
import (
"fmt"
"github.com/spf13/viper"
)
type Config struct {
Host string `yaml:"host,omitempty"`
Module string `yaml:"module,omitempty"`
Filename string `yaml:"filename,omitempty"`
Address string `yaml:"address,omitempty"`
Bootstrap []string `yaml:"bootstrap,omitempty"`
Netpath string `yaml:"netpath,omitempty"`
Keypath string `yaml:"keypath,omitempty"`
// TODO: loading WAL instead of configuration
Round int `yaml:"round,omitempty"`
Startk string `yaml:"startk,omitempty"`
Startv string `yaml:"startv,omitempty"`
Validators []string `yaml:"validators,omitempty"`
}
func GetConfig(cfgFile string) (*Config, error) {
if cfgFile == "" {
return defaultConfig(), nil
}
return loadConfig(cfgFile)
}
func defaultConfig() *Config {
return &Config{
Module: "gohotstuff",
Filename: "gohotstuff",
Address: "/ip4/127.0.0.1/tcp/30001",
Round: 0,
Startk: "lets_run_hotstuff",
Startv: "lets_run_hotstuff_value",
Validators: []string{},
}
}
func loadConfig(cfgFile string) (*Config, error) {
if cfgFile == "" || !FileIsExist(cfgFile) {
return nil, fmt.Errorf("config file set error.path:%s", cfgFile)
}
viperObj := viper.New()
viperObj.SetConfigFile(cfgFile)
err := viperObj.ReadInConfig()
if err != nil {
return nil, fmt.Errorf("read config failed.path:%s,err:%v", cfgFile, err)
}
var config Config
if err = viperObj.Unmarshal(&config); err != nil {
return nil, fmt.Errorf("unmatshal config failed.path:%s,err:%v", cfgFile, err)
}
return &config, nil
}