Skip to content

[Bugfix] Remove Options Duplications #293

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

Merged
merged 1 commit into from
Jun 30, 2021
Merged
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
8 changes: 8 additions & 0 deletions service/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ func (c ConfigurationPrefixes) Parse(args ...string) (*Configuration, []Configur
var f []ConfigurationFlag
config := NewConfiguration()

flags := map[string]bool{}

for _, arg := range args {
arg = strings.SplitN(arg, "=", 2)[0]

Expand All @@ -224,6 +226,12 @@ func (c ConfigurationPrefixes) Parse(args ...string) (*Configuration, []Configur
return nil, nil, fmt.Errorf("option --%s is essential to the starters behavior and cannot be overwritten", targ)
}

if _, ok := flags[arg]; ok {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems that this can be simplified to

if _, ok := flags[arg]; !ok {
  flags[arg] = true
  ...
}
break

but this does not change the logic, it looks a bit easier to read, IMHO.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It took me a while to understand what is going on here. It seems that you want to select only those args, which are in ConfigurationPrefixes, and only allow each one once. I cannot really judge if this is correct behaviour. What about multiple options like

  --all.log.level=requests=trace --all.log.level=communication=trace

are these still allowed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. usually, linter prefers if you exit if as fast as possible - in this case we check if option is defined and then exit. If no, proceed. It is better to keep smaller piece of code inside block.

  2. In this case we only generate list of flags passed to Cobra, where each one can be parsed multiple times by Cobra itself. So your case is totally valid, you will get []string{"--log.level=requests=trace", "--log.level=communication=trace"} after parsint it by Cobra. Problem was only when you tried to add flag twice - it just ran into panic.

break
} else {
flags[arg] = true
}

f = append(f, ConfigurationFlag{
Key: arg,
CleanKey: ckey,
Expand Down