Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: expose FlagSet #2959

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

type Config struct {
ConfigFile string
flags *flag.FlagSet
Flags *flag.FlagSet

// generic:
Address string `yaml:"address"`
Expand Down Expand Up @@ -576,7 +576,7 @@ func NewConfig() *Config {
// Passive Health Checks
flag.Var(&cfg.PassiveHealthCheck, "passive-health-check", "sets the parameters for passive health check feature")

cfg.flags = flag
cfg.Flags = flag
return cfg
}

Expand Down Expand Up @@ -609,15 +609,15 @@ func (c *Config) Parse() error {
}

func (c *Config) ParseArgs(progname string, args []string) error {
c.flags.Init(progname, flag.ExitOnError)
err := c.flags.Parse(args)
c.Flags.Init(progname, flag.ExitOnError)
err := c.Flags.Parse(args)
if err != nil {
return err
}

// check if arguments were correctly parsed.
if len(c.flags.Args()) != 0 {
return fmt.Errorf("invalid arguments: %s", c.flags.Args())
if len(c.Flags.Args()) != 0 {
return fmt.Errorf("invalid arguments: %s", c.Flags.Args())
}

configKeys := make(map[string]interface{})
Expand All @@ -634,7 +634,7 @@ func (c *Config) ParseArgs(progname string, args []string) error {

_ = yaml.Unmarshal(yamlFile, configKeys)

err = c.flags.Parse(args)
err = c.Flags.Parse(args)
if err != nil {
return err
}
Expand Down Expand Up @@ -1086,13 +1086,13 @@ func (c *Config) parseEnv() {

func (c *Config) checkDeprecated(configKeys map[string]interface{}, options ...string) {
flagKeys := make(map[string]bool)
c.flags.Visit(func(f *flag.Flag) { flagKeys[f.Name] = true })
c.Flags.Visit(func(f *flag.Flag) { flagKeys[f.Name] = true })

for _, name := range options {
_, ck := configKeys[name]
_, fk := flagKeys[name]
if ck || fk {
f := c.flags.Lookup(name)
f := c.Flags.Lookup(name)
log.Warnf("%s: %s", f.Name, f.Usage)
}
}
Expand Down
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestEnvOverrides_SwarmRedisPassword(t *testing.T) {
func defaultConfig() *Config {
return &Config{
ConfigFile: "testdata/test.yaml",
flags: nil,
Flags: nil,
Address: "localhost:8080",
StatusChecks: nil,
ExpectedBytesPerRequest: 50 * 1024,
Expand Down Expand Up @@ -336,8 +336,8 @@ func Test_NewConfigWithArgs(t *testing.T) {
}

if !tt.wantErr {
if cmp.Equal(cfg, tt.want, cmp.AllowUnexported(listFlag{}, pluginFlag{}, defaultFiltersFlags{}, mapFlags{}), cmpopts.IgnoreUnexported(Config{})) == false {
t.Errorf("config.NewConfig() got vs. want:\n%v", cmp.Diff(cfg, tt.want, cmp.AllowUnexported(listFlag{}, pluginFlag{}, defaultFiltersFlags{}, mapFlags{}), cmpopts.IgnoreUnexported(Config{})))
if cmp.Equal(cfg, tt.want, cmp.AllowUnexported(listFlag{}, pluginFlag{}, defaultFiltersFlags{}, mapFlags{}), cmpopts.IgnoreUnexported(Config{}), cmpopts.IgnoreFields(Config{}, "Flags")) == false {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@szuecs The failing test has been addressed. Before this PR, the value of Config.flags was not considered for comparison during these tests because of cmpopts.IgnoreUnexported(Config{}). Added cmpopts.IgnoreFields(Config{}, "Flags") to re-create the behavior of ignoring the field.

t.Errorf("config.NewConfig() got vs. want:\n%v", cmp.Diff(cfg, tt.want, cmp.AllowUnexported(listFlag{}, pluginFlag{}, defaultFiltersFlags{}, mapFlags{}), cmpopts.IgnoreUnexported(Config{}), cmpopts.IgnoreFields(Config{}, "Flags")))
}
}
})
Expand Down
Loading