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

refactor: Move from global state to functions #53

Merged
merged 20 commits into from
May 3, 2021
Merged

Conversation

stuartcarnie
Copy link
Contributor

@stuartcarnie stuartcarnie commented Apr 29, 2021

This PR represents a few experiments of features I've used in Cobra and some attempts to reduce duplication of code.

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

  3. Adds a Flags() method to the Params structs 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

cmd/influx/bucket.go Outdated Show resolved Hide resolved
internal/bucket.go Outdated Show resolved Hide resolved
Comment on lines 68 to 69
clients := getBucketsClient(ctx)
return getCLI(ctx).BucketsDelete(ctx.Context, &clients, &params)
Copy link
Contributor Author

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.

@danxmoran danxmoran self-requested a review April 29, 2021 12:39
cmd/influx/main.go Show resolved Hide resolved
cmd/influx/main.go Outdated Show resolved Hide resolved
internal/bucket.go Outdated Show resolved Hide resolved
internal/throttler/throttler.go Outdated Show resolved Hide resolved
internal/write.go Outdated Show resolved Hide resolved
internal/throttler/throttler.go Outdated Show resolved Hide resolved
pkg/influxid/id.go Show resolved Hide resolved
pkg/cmd/flag/bytes_per_seconds.go Outdated Show resolved Hide resolved
@stuartcarnie stuartcarnie marked this pull request as ready for review April 30, 2021 05:56
cmd/influx/write.go Outdated Show resolved Hide resolved
danxmoran and others added 3 commits April 30, 2021 15:59
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
@stuartcarnie
Copy link
Contributor Author

Thanks for the review, @danxmoran. I've pushed a commit with the feedback suggestions.

@danxmoran danxmoran changed the base branch from dm-port-bucket-7 to main May 3, 2021 13:14
@danxmoran
Copy link
Contributor

Sorry to dirty the commit history, didn't want to rebase/force-push and disrupt your other open PR 😬

Copy link
Contributor

@danxmoran danxmoran left a 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!

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.

2 participants