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

fix: Prevent needless CLI parsing #884

Merged

Conversation

nderjung
Copy link
Member

@nderjung nderjung commented Oct 16, 2023

Prerequisite checklist

  • Read the contribution guidelines regarding submitting new changes to the project;
  • Tested your changes against relevant architectures and platforms;
  • Ran make fmt on your commit series before opening this PR;
  • Updated relevant documentation.

Description of changes

Initially, cmd.ParseFlags in the AttributeFlags method was used to associate the provided list of command-line arguments with the respectively registered flag. However, AttributeFlags has since been modified to make this association by directly calling flag.Set. Furthermore, calling cmd.ParseFlags consistently had the unintended side-affect of populating initialized slice structures with duplicate values.

To mitigate against double parsing, which results in duplicate values in, for example, slice types, remove this additional parsing of the flags.

We call cmd.ParseFlags only once after the config manager has been attributed as these values are required by subsequent context systems (e.g. logging). To prevent double parsing on the root command, set DisableFlagParsing to true such that cobra's ExecuteContext method does not end up calling this again on the root command.

This also has the benefit of greatly simplify the code by removing the previously imported execute and executeC to prevent internal parsing and fixing command completion and listing inherited flags.

@nderjung nderjung force-pushed the nderjung/fix/cmdfactory/double-parsing branch from f2993dd to e33b8e3 Compare October 16, 2023 21:21
@nderjung nderjung marked this pull request as draft October 16, 2023 21:22
Initially, `cmd.ParseFlags` in the `AttributeFlags` method was used to
associate the provided list of command-line arguments with the
respectively registered flag.  However, `AttributeFlags` has since been
modified to make this association by directly calling `flag.Set`.
Furthermore, calling `cmd.ParseFlags` consistently had the unintended
sideaffect of populating initialized slice structures with duplicate
values.

To mitigate against double parsing, which results in duplicate values
in, for example, slice types, remove this additional parsing of the
flags.

We call `cmd.ParseFlags` only once after the config manager has bee
attributed as these values are required by subsequent context systems
(e.g. logging).  To prevent double parsing on the root command, set
`DisableFlagParsing` to true such that cobra's `ExecuteContext` method
does not end up calling this again on the root command.

This also has the benefit of greatly simplify the code by removing the
previously imported `execute` and `executeC` to prevent internal
parsing[0].

[0]: unikraft@1e6c1a4

Signed-off-by: Alexander Jung <alex@unikraft.io>
@nderjung nderjung force-pushed the nderjung/fix/cmdfactory/double-parsing branch from e33b8e3 to 929cffd Compare October 16, 2023 21:58
@nderjung nderjung marked this pull request as ready for review October 16, 2023 22:06
Copy link
Member

@craciunoiuc craciunoiuc left a comment

Choose a reason for hiding this comment

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

All good here. Thanks!

Reviewed-by: Cezar Craciunoiu cezar.craciunoiu@unikraft.io
Approved-by: Cezar Craciunoiu cezar.craciunoiu@unikraft.io

@craciunoiuc craciunoiuc merged commit 76ccb69 into unikraft:staging Oct 17, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🚀 Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants