Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 2f3ca58

Browse files
authored
Merge pull request #903 from inoc603/fix/900
fix: ineffective viper alias
2 parents 78da1b6 + 3b9db8c commit 2f3ca58

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

cmd/dfdaemon/app/root.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var rootCmd = &cobra.Command{
4949
return errors.Wrap(err, "read config file")
5050
}
5151

52-
cfg, err := getConfigFromViper(viper.GetViper())
52+
cfg, err := getConfigFromViper(cmd, viper.GetViper())
5353
if err != nil {
5454
return errors.Wrap(err, "get config from viper")
5555
}
@@ -95,7 +95,7 @@ func init() {
9595
rf.String("localrepo", filepath.Join(os.Getenv("HOME"), ".small-dragonfly/dfdaemon/data/"), "temp output dir of dfdaemon")
9696
rf.String("dfpath", defaultDfgetPath, "dfget path")
9797
rf.Var(netutils.NetLimit(), "ratelimit", "net speed limit")
98-
rf.StringSlice("node", nil, "specify the addresses(host:port) of supernodes that will be passed to dfget.")
98+
rf.StringSlice("node", []string{"127.0.0.1:8002"}, "specify the addresses(host:port) of supernodes that will be passed to dfget.")
9999

100100
exitOnError(bindRootFlags(viper.GetViper()), "bind root command flags")
101101
}
@@ -122,7 +122,6 @@ func readConfigFile(v *viper.Viper, cmd *cobra.Command) error {
122122
}
123123
return err
124124
}
125-
v.RegisterAlias("supernodes", "node")
126125

127126
return nil
128127
}
@@ -145,8 +144,15 @@ func Execute() {
145144
}
146145
}
147146

148-
// getConfigFromViper returns dfdaemon config from the given viper instance.
149-
func getConfigFromViper(v *viper.Viper) (*config.Properties, error) {
147+
// getConfigFromViper returns dfdaemon config from the given viper instance
148+
func getConfigFromViper(cmd *cobra.Command, v *viper.Viper) (*config.Properties, error) {
149+
// override supernodes in config file if --node is sepecified in cli.
150+
// use default value if no supernodes is configured in config file
151+
if cmd.Flags().Lookup("node").Changed ||
152+
len(v.GetStringSlice("supernodes")) == 0 {
153+
v.Set("supernodes", v.GetStringSlice("node"))
154+
}
155+
150156
var cfg config.Properties
151157
if err := v.Unmarshal(&cfg, func(dc *mapstructure.DecoderConfig) {
152158
dc.TagName = "yaml"
@@ -160,6 +166,7 @@ func getConfigFromViper(v *viper.Viper) (*config.Properties, error) {
160166
}); err != nil {
161167
return nil, errors.Wrap(err, "unmarshal yaml")
162168
}
169+
163170
return &cfg, cfg.Validate()
164171
}
165172

cmd/dfdaemon/app/root_test.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,72 @@ func (ts *rootTestSuite) TestConfigNotFound() {
5151
r.True(os.IsNotExist(errors.Cause(readConfigFile(v, rootCmd))))
5252
}
5353

54+
func (ts *rootTestSuite) TestNodeFlag() {
55+
r := ts.Require()
56+
fs := afero.NewMemMapFs()
57+
58+
fs.Create("/dfget")
59+
60+
configName := "dfdaemon.yml"
61+
file, err := fs.Create(configName)
62+
r.Nil(err)
63+
file.WriteString("supernodes:\n- 127.0.0.1:6666")
64+
file.Close()
65+
66+
// flag not set, should use config file
67+
{
68+
v := viper.New()
69+
v.SetFs(fs)
70+
v.Set("dfpath", "/")
71+
rootCmd.Flags().Set("config", configName)
72+
r.Nil(bindRootFlags(v))
73+
r.Nil(readConfigFile(v, rootCmd))
74+
cfg, err := getConfigFromViper(rootCmd, v)
75+
r.Nil(err)
76+
r.Equal([]string{"127.0.0.1:6666"}, cfg.SuperNodes)
77+
}
78+
79+
// flag not set, config file doesn't exist, should use default
80+
{
81+
v := viper.New()
82+
v.SetFs(fs)
83+
v.Set("dfpath", "/")
84+
rootCmd.Flags().Set("config", "xxx")
85+
r.Nil(bindRootFlags(v))
86+
r.NotNil(readConfigFile(v, rootCmd))
87+
cfg, err := getConfigFromViper(rootCmd, v)
88+
r.Nil(err)
89+
r.Equal([]string{"127.0.0.1:8002"}, cfg.SuperNodes)
90+
}
91+
92+
// when --node flag is set, should always use the flag
93+
rootCmd.Flags().Set("node", "127.0.0.1:7777")
94+
95+
{
96+
v := viper.New()
97+
v.SetFs(fs)
98+
v.Set("dfpath", "/")
99+
rootCmd.Flags().Set("config", "xxx")
100+
r.Nil(bindRootFlags(v))
101+
r.NotNil(readConfigFile(v, rootCmd))
102+
cfg, err := getConfigFromViper(rootCmd, v)
103+
r.Nil(err)
104+
r.Equal([]string{"127.0.0.1:7777"}, cfg.SuperNodes)
105+
}
106+
107+
{
108+
v := viper.New()
109+
v.SetFs(fs)
110+
v.Set("dfpath", "/")
111+
rootCmd.Flags().Set("config", configName)
112+
r.Nil(bindRootFlags(v))
113+
r.Nil(readConfigFile(v, rootCmd))
114+
cfg, err := getConfigFromViper(rootCmd, v)
115+
r.Nil(err)
116+
r.Equal([]string{"127.0.0.1:7777"}, cfg.SuperNodes)
117+
}
118+
}
119+
54120
func generateFakeFilename(fs afero.Fs) string {
55121
for i := 0; i < 100; i++ {
56122
d := fmt.Sprintf("/dftest-%d-%d", time.Now().UnixNano(), rand.Int())
@@ -111,7 +177,7 @@ func (ts *rootTestSuite) TestDecodeWithYAML() {
111177
r.Nil(err)
112178
v.Set("registry_mirror.certs", []string{f.Name()})
113179

114-
cfg, err := getConfigFromViper(v)
180+
cfg, err := getConfigFromViper(rootCmd, v)
115181
r.Nil(err)
116182
r.NotNil(cfg.RegistryMirror.Remote)
117183
r.Equal(mockURL, cfg.RegistryMirror.Remote.String())

0 commit comments

Comments
 (0)