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

Commit f646983

Browse files
committed
fix: ineffective viper alias
Signed-off-by: inoc603 <[email protected]>
1 parent 7dd9e88 commit f646983

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

cmd/dfdaemon/app/root.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var rootCmd = &cobra.Command{
4747
return errors.Wrap(err, "read config file")
4848
}
4949

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

9898
exitOnError(bindRootFlags(viper.GetViper()), "bind root command flags")
9999
}
@@ -120,7 +120,6 @@ func readConfigFile(v *viper.Viper, cmd *cobra.Command) error {
120120
}
121121
return err
122122
}
123-
v.RegisterAlias("supernodes", "node")
124123

125124
return nil
126125
}
@@ -144,7 +143,14 @@ func Execute() {
144143
}
145144

146145
// getConfigFromViper returns dfdaemon config from the given viper instance
147-
func getConfigFromViper(v *viper.Viper) (*config.Properties, error) {
146+
func getConfigFromViper(cmd *cobra.Command, v *viper.Viper) (*config.Properties, error) {
147+
// override supernodes in config file if --node is sepecified in cli.
148+
// use default value if no supernodes is configured in config file
149+
if cmd.Flags().Lookup("node").Changed ||
150+
len(v.GetStringSlice("supernodes")) == 0 {
151+
v.Set("supernodes", v.GetStringSlice("node"))
152+
}
153+
148154
var cfg config.Properties
149155
if err := v.Unmarshal(&cfg, func(dc *mapstructure.DecoderConfig) {
150156
dc.TagName = "yaml"
@@ -156,6 +162,7 @@ func getConfigFromViper(v *viper.Viper) (*config.Properties, error) {
156162
}); err != nil {
157163
return nil, errors.Wrap(err, "unmarshal yaml")
158164
}
165+
159166
return &cfg, cfg.Validate()
160167
}
161168

cmd/dfdaemon/app/root_test.go

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,73 @@ 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+
fmt.Println(cfg)
105+
r.Equal([]string{"127.0.0.1:7777"}, cfg.SuperNodes)
106+
}
107+
108+
{
109+
v := viper.New()
110+
v.SetFs(fs)
111+
v.Set("dfpath", "/")
112+
rootCmd.Flags().Set("config", configName)
113+
r.Nil(bindRootFlags(v))
114+
r.Nil(readConfigFile(v, rootCmd))
115+
cfg, err := getConfigFromViper(rootCmd, v)
116+
r.Nil(err)
117+
r.Equal([]string{"127.0.0.1:7777"}, cfg.SuperNodes)
118+
}
119+
}
120+
54121
func generateFakeFilename(fs afero.Fs) string {
55122
for i := 0; i < 100; i++ {
56123
d := fmt.Sprintf("/dftest-%d-%d", time.Now().UnixNano(), rand.Int())
@@ -109,7 +176,7 @@ func (ts *rootTestSuite) TestDecodeWithYAML() {
109176
f.Close()
110177
v.Set("registry_mirror.certs", []string{f.Name()})
111178

112-
cfg, err := getConfigFromViper(v)
179+
cfg, err := getConfigFromViper(rootCmd, v)
113180
r.Nil(err)
114181
r.NotNil(cfg.RegistryMirror.Remote)
115182
r.Equal(mockURL, cfg.RegistryMirror.Remote.String())

0 commit comments

Comments
 (0)