
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
21 lines
489 B
Go
21 lines
489 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
// WithBeforeFns returns a cli.BeforeFunc that calls each of the provided
|
|
// functions in order.
|
|
// NOTE: The first function to return an error will end execution and
|
|
// be returned as the error value of the composed function.
|
|
func WithBeforeFns(fns ...cli.BeforeFunc) cli.BeforeFunc {
|
|
return func(ctx *cli.Context) error {
|
|
for _, fn := range fns {
|
|
if err := fn(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|