-
Notifications
You must be signed in to change notification settings - Fork 24
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
refactor: Move from global state to functions #53
Conversation
0d4a6dc
to
4cfb5de
Compare
cmd/influx/bucket.go
Outdated
clients := getBucketsClient(ctx) | ||
return getCLI(ctx).BucketsDelete(ctx.Context, &clients, ¶ms) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the bucket actions are reduced to 1-2 lines of code.
f0ac72e
to
01dd763
Compare
01dd763
to
571b1de
Compare
This commit represents a few experiments of features I've used in Cobra 1. Uses cli.GenericFlag to encapsulate parsing and validation of flag values at parse time. This removes the burden from the individual CLI commands to parse and validate args and options. 2. Add influxid.ID that may be used by any flag that requires an Influx ID. influxid.ID parses and validates string value is a valid ID, removing this burden from individual commands and ensuring valid values before the command actions begins. 3. Binds cli.Flags directly to params structures to directly capture the values when parsing flags. 4. Moves from global state to local builder functions for the majority of the commands. This allows the commands to bind to flag variables reducing the repeated ctx.String(), ctx.Int(), etc 5. Leverages the BeforeFunc to create middleware and inject the CLI and API client into commands, saving the repeated boilerplate across all of the instantiated commands. This is extensible, so additional middleware can be appends using the middleware.WithBeforeFns
571b1de
to
bf5a85c
Compare
Thanks for the review, @danxmoran. I've pushed a commit with the feedback suggestions. |
Sorry to dirty the commit history, didn't want to rebase/force-push and disrupt your other open PR 😬 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll merge when CI is green to unblock other work in the repo using the new middleware. Thanks for taking the time to contribute the improvements!
This PR represents a few experiments of features I've used in Cobra and some attempts to reduce duplication of code.
Uses
cli.GenericFlag
to encapsulate parsing and validation of flag values at parse time. This removes the burden from the individual CLI commands to parse and validate args and options.Add
flag.ID
that may be used by any flag that requires an Influx ID.flag.ID
parses and validates string value is a valid ID, removing this burden from individual commands and ensuring valid values before the command actions begins.Adds a
Flags()
method to the Params structs to directly capture the values when parsing flags.Moves from global state to local builder functions for the majority of the commands. This allows the commands to bind to flag variables reducing the repeated
ctx.String()
,ctx.Int()
, etcLeverages the
BeforeFunc
to create middleware and inject the CLI and API client into commands, saving the repeated boilerplate across all of the instantiated commands. This is extensible, so additional middleware can be appends using themiddleware.WithBeforeFns