-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[cmd/*] Switch to pflag for all CLI flag parsing #10619
Conversation
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Bug fixes
Non-trivial changes
New/Existing features
Backward compatibility
|
9322f52
to
e1c3867
Compare
968914a
to
c38f6d7
Compare
How about converting the TODOs in the tracking project to Issues? |
Let me know what you think!! #10697 |
This transparently swaps the cli parsing library used by `internal/flag` from the standard library `flag` package to `spf13/pflag`. It also introduces hook points for packages throughout the vitess codebase to register their flags for either all commands using `servenv` or a particular subset of commands. This allows these packages to continue to define their flag variables in a package-private way, but without polluting the global flagset. Signed-off-by: Andrew Mason <[email protected]>
tl;dr stdlib `flag` has [this][1] and `pflag` does not [1]: golang/go@dcf0929 Signed-off-by: Andrew Mason <[email protected]>
Signed-off-by: Andrew Mason <[email protected]>
Signed-off-by: Andrew Mason <[email protected]>
… update test data Signed-off-by: Andrew Mason <[email protected]>
Signed-off-by: Andrew Mason <[email protected]>
rebased to pick up the help text change here |
)" This reverts commit c3f4a99. Signed-off-by: Dirkjan Bussink <[email protected]>
👋 this PR seems to break $ vtctlclient ApplySchema --skip_preflight --ddl_strategy "online -allow-zero-in-date --allow-concurrent --postpone-completion" --sql "alter table corder engine=innodb" commerce
unknown flag: --skip_preflight or $ vtctlclient ApplySchema --ddl_strategy "online -allow-zero-in-date --allow-concurrent --postpone-completion" --sql "alter table corder engine=innodb" commerce
unknown flag: --ddl_strategy or $ vtctlclient ApplySchema --sql "alter table corder engine=innodb" commerce
unknown flag: --sql I'm not sure how tests passed, as they ought to have captured this malfunction. At any case, right now |
(just closing the loop here) we discussed this in other fora, and the issue was a combination of being on different checkout and therefore needing the legacy |
* Switch to `pflag` for all parsing This transparently swaps the cli parsing library used by `internal/flag` from the standard library `flag` package to `spf13/pflag`. It also introduces hook points for packages throughout the vitess codebase to register their flags for either all commands using `servenv` or a particular subset of commands. This allows these packages to continue to define their flag variables in a package-private way, but without polluting the global flagset. Signed-off-by: Andrew Mason <[email protected]> * Workaround exit code difference between stdlib `flag` and `pflag` tl;dr stdlib `flag` has [this][1] and `pflag` does not [1]: golang/go@dcf0929 Signed-off-by: Andrew Mason <[email protected]> * adjust test data for difference in spacing between pflag/stdflag Signed-off-by: Andrew Mason <[email protected]> * update lingering legacy flag tests Signed-off-by: Andrew Mason <[email protected]> * Update vtgate/tabletgateway.go to use new interface to isolate flags, update test data Signed-off-by: Andrew Mason <[email protected]> * update flags in java test code Signed-off-by: Andrew Mason <[email protected]>
* Switch to `pflag` for all parsing This transparently swaps the cli parsing library used by `internal/flag` from the standard library `flag` package to `spf13/pflag`. It also introduces hook points for packages throughout the vitess codebase to register their flags for either all commands using `servenv` or a particular subset of commands. This allows these packages to continue to define their flag variables in a package-private way, but without polluting the global flagset. Signed-off-by: Andrew Mason <[email protected]> * Workaround exit code difference between stdlib `flag` and `pflag` tl;dr stdlib `flag` has [this][1] and `pflag` does not [1]: golang/go@dcf0929 Signed-off-by: Andrew Mason <[email protected]> * adjust test data for difference in spacing between pflag/stdflag Signed-off-by: Andrew Mason <[email protected]> * update lingering legacy flag tests Signed-off-by: Andrew Mason <[email protected]> * Update vtgate/tabletgateway.go to use new interface to isolate flags, update test data Signed-off-by: Andrew Mason <[email protected]> * update flags in java test code Signed-off-by: Andrew Mason <[email protected]>
* Switch to `pflag` for all parsing This transparently swaps the cli parsing library used by `internal/flag` from the standard library `flag` package to `spf13/pflag`. It also introduces hook points for packages throughout the vitess codebase to register their flags for either all commands using `servenv` or a particular subset of commands. This allows these packages to continue to define their flag variables in a package-private way, but without polluting the global flagset. Signed-off-by: Andrew Mason <[email protected]> * Workaround exit code difference between stdlib `flag` and `pflag` tl;dr stdlib `flag` has [this][1] and `pflag` does not [1]: golang/go@dcf0929 Signed-off-by: Andrew Mason <[email protected]> * adjust test data for difference in spacing between pflag/stdflag Signed-off-by: Andrew Mason <[email protected]> * update lingering legacy flag tests Signed-off-by: Andrew Mason <[email protected]> * Update vtgate/tabletgateway.go to use new interface to isolate flags, update test data Signed-off-by: Andrew Mason <[email protected]> * update flags in java test code Signed-off-by: Andrew Mason <[email protected]>
* Switch to `pflag` for all parsing This transparently swaps the cli parsing library used by `internal/flag` from the standard library `flag` package to `spf13/pflag`. It also introduces hook points for packages throughout the vitess codebase to register their flags for either all commands using `servenv` or a particular subset of commands. This allows these packages to continue to define their flag variables in a package-private way, but without polluting the global flagset. * Workaround exit code difference between stdlib `flag` and `pflag` tl;dr stdlib `flag` has [this][1] and `pflag` does not [1]: golang/go@dcf0929 * adjust test data for difference in spacing between pflag/stdflag * update lingering legacy flag tests * Update vtgate/tabletgateway.go to use new interface to isolate flags, update test data * update flags in java test code Signed-off-by: Andrew Mason <[email protected]> Co-authored-by: Andrew Mason <[email protected]>
Description
This PR begins the next step in the implementation of VEP-4, officially moving us to using
pflag
in all of our CLI flag parsing. It does not migrate any flag definitions (except one small proof-of-concept/example), or move any flags (at all) out of the global flagset, which can be done incrementally in follow-up PRs.How to move a package's flags out of the global flagset — The Guide
(if you think of a good place to house this, let me know!)
For each flag in the package you are tackling, you are going to want to determine if it truly should be global and available to every binary (probably flags that control logging and tracing behavior are good candidates), or if they only make sense for a particular binary or particular subset of binaries.
To see which
cmd
entrypoints actually import your package at all (which doesn't actually tell you if the value that flag is set to has any impact on that binary), you can run:The spacing in the
-f
argument togo list
might seem arbitrary, but it's specific and important here, as it lets us do an exact match in theawk
(e.g. you want to know ifvitess.io/vitess/go/vt/vtgate
is imported, but not get false positives if someone importsvitess.io/vitess/go/vt/vtgate/buffer
, or anything else underneath thego/vt/vtgate/*
tree).As an example, here are the
cmd
s that importgrpcvtgateservice
:Armed with this list, you need to make a judgement call as to whether each of these commands actually needs the flags defined in your package. I don't have much to say in the way of guidance here, except to ask the domain experts of the package and the
cmd
entrypoint. This is also why we have endtoend tests, and release candidates :)Once you've decided, you can move your flags to a function (
RegisterFlags
is a good name, as isInstallFlags
, which takes a*pflag.FlagSet
as input), for example:Now that you've done that, you'll hook into
servenv
's new hook points to ensure your flag gets installed in the right places. If you decided your flag should truly be global, you will do:If you've decided your flag belongs only on one or two commands, you'll do:
Related Issue(s)
pflag
#10696Checklist
Deployment Notes