-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move from global state to functions
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
- Loading branch information
1 parent
f3dcbfa
commit 571b1de
Showing
12 changed files
with
1,048 additions
and
549 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
package main | ||
|
||
import "github.com/urfave/cli/v2" | ||
import ( | ||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var pingCmd = cli.Command{ | ||
Name: "ping", | ||
Usage: "Check the InfluxDB /health endpoint", | ||
Flags: coreFlags, | ||
Name: "ping", | ||
Usage: "Check the InfluxDB /health endpoint", | ||
Before: middleware.WithBeforeFns(withCli(), withApi()), | ||
Flags: coreFlags, | ||
Action: func(ctx *cli.Context) error { | ||
cli, err := newCli(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
client, err := newApiClient(ctx, cli, false) | ||
if err != nil { | ||
return err | ||
} | ||
return cli.Ping(standardCtx(ctx), client.HealthApi) | ||
cli := getCLI(ctx) | ||
client := getAPINoToken(ctx) | ||
return cli.Ping(ctx.Context, client.HealthApi) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.