-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
124 lines (105 loc) · 2.81 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package cmd
import (
"fmt"
"os"
"path"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
type configService struct {
conf configPart `yaml:"inline,omitempty"`
clientConfig *CosmosClientConfig `yaml:"inline,omitempty"`
}
type configPart interface {
CreateNewConfig(string)
ValidateConfig() error
}
func NewConfigService(c configPart) configService {
return configService{
conf: c,
clientConfig: &CosmosClientConfig{},
}
}
// MustYAML returns the yaml string representation of the config
func (c *configService) MustYAML() []byte {
out, err := yaml.Marshal(c)
if err != nil {
panic(err)
}
return out
}
// initConfig reads in config file and ENV variables if set.
// This is called as a persistent pre-run command of the root command.
func initConfig(cmd *cobra.Command, a *appState, o map[string]ClientOverrides) error {
if a.Config == nil {
panic("Must initialize app config with NewConfigService")
}
home, err := cmd.PersistentFlags().GetString(flags.FlagHome)
if err != nil {
return err
}
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
cfgPath := path.Join(home, "config.yaml")
_, err = os.Stat(cfgPath)
if err != nil {
err = a.Config.createNewConfig(home, debug)
if err != nil {
return err
}
}
a.Viper.SetConfigFile(cfgPath)
err = a.Viper.ReadInConfig()
if err != nil {
return fmt.Errorf("failed to read in config: %w", err)
}
// read the config file bytes
file, err := os.ReadFile(a.Viper.ConfigFileUsed())
if err != nil {
return fmt.Errorf("error reading config file: %w", err)
}
// unmarshall them into the struct
if err = yaml.Unmarshal(file, &a.Config); err != nil {
return fmt.Errorf("error unmarshalling config: %w", err)
}
initErr := a.Initialize(home, a.Log, cmd, o)
if initErr != nil {
return initErr
}
// validate configuration
if err := a.Config.conf.ValidateConfig(); err != nil {
return fmt.Errorf("error validating config: %w", err)
}
return nil
}
// createConfig idempotently creates the config.
func (c *configService) createNewConfig(home string, debug bool) error {
cfgPath := path.Join(home, "config.yaml")
// If the config doesn't exist...
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
// And the config folder doesn't exist...
// And the home folder doesn't exist
if _, err := os.Stat(home); os.IsNotExist(err) {
// Create the home folder
if err = os.Mkdir(home, os.ModePerm); err != nil {
return err
}
}
}
if c.conf != nil {
c.conf.CreateNewConfig(home)
}
if c.clientConfig != nil {
c.clientConfig.CreateNewConfig(home)
}
content := c.MustYAML()
// Then create the file...
//content := defaultConfig(path.Join(home, "keys"), debug)
if err := os.WriteFile(cfgPath, content, 0600); err != nil {
return err
}
return nil
}