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

Conversation

ajanikow
Copy link
Collaborator

No description provided.

Copy link

@KVS85 KVS85 left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Collaborator

@neunhoef neunhoef left a comment

Choose a reason for hiding this comment

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

LGTM, but see comments for a suggestion and a question.

@@ -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.

@ajanikow ajanikow merged commit dec73b1 into master Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants