fix: detect and error out on incorrect positional args (#236)

This commit is contained in:
Daniel Moran 2021-08-20 17:30:30 -04:00 committed by GitHub
parent c0103e7b1b
commit fa12328714
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 204 additions and 64 deletions

View File

@ -5,6 +5,7 @@
1. [221](https://github.com/influxdata/influx-cli/pull/221): Fix shell completion for top-level `influx` commands.
1. [228](https://github.com/influxdata/influx-cli/pull/228): Make global `--http-debug` flag visible in help text.
1. [232](https://github.com/influxdata/influx-cli/pull/232): Don't set empty strings for IDs in permission resources.
1. [236](https://github.com/influxdata/influx-cli/pull/236): Detect and error out on incorrect positional args.
## v2.1.0 [2021-07-29]

View File

@ -164,7 +164,7 @@ https://github.com/influxdata/community-templates.
Value: &params.filters,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
parsedParams := apply.Params{
OrgId: params.orgId,

View File

@ -149,7 +149,7 @@ func newCreateCommand() cli.Command {
Name: "create",
Usage: "Create authorization",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
params.WriteBucketIds = ctx.StringSlice("write-bucket")
params.ReadBucketIds = ctx.StringSlice("read-bucket")
@ -178,7 +178,7 @@ func newDeleteCommand() cli.Command {
Required: true,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := auth.Client{
@ -217,7 +217,7 @@ func newListCommand() cli.Command {
Usage: "List authorizations",
Aliases: []string{"find", "ls"},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := auth.Client{
@ -242,7 +242,7 @@ func newSetActiveCommand() cli.Command {
Required: true,
},
},
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := auth.Client{
@ -267,7 +267,7 @@ func newSetInactiveCommand() cli.Command {
Required: true,
},
},
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := auth.Client{

View File

@ -25,12 +25,9 @@ func newBucketCreateCmd() cli.Command {
SchemaType: api.SCHEMATYPE_IMPLICIT,
}
return cli.Command{
Name: "create",
Usage: "Create bucket",
Before: middleware.WithBeforeFns(
withCli(),
withApi(true),
),
Name: "create",
Usage: "Create bucket",
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.StringFlag{
@ -90,7 +87,7 @@ func newBucketDeleteCmd() cli.Command {
return cli.Command{
Name: "delete",
Usage: "Delete bucket",
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.StringFlag{
@ -134,7 +131,7 @@ func newBucketListCmd() cli.Command {
Name: "list",
Usage: "List buckets",
Aliases: []string{"find", "ls"},
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.StringFlag{
@ -178,7 +175,7 @@ func newBucketUpdateCmd() cli.Command {
Name: "update",
Usage: "Update bucket",
Aliases: []string{"find", "ls"},
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.StringFlag{

View File

@ -14,6 +14,7 @@ func withBucketSchemaClient() cli.BeforeFunc {
return middleware.WithBeforeFns(
withCli(),
withApi(true),
middleware.NoArgs,
func(ctx *cli.Context) error {
client := getAPI(ctx)
ctx.App.Metadata["measurement_schema"] = bucket_schema.Client{

View File

@ -7,6 +7,7 @@ import (
cmd "github.com/influxdata/influx-cli/v2/clients/config"
"github.com/influxdata/influx-cli/v2/config"
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
"github.com/urfave/cli"
)
@ -84,7 +85,7 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/config/
and
https://docs.influxdata.com/influxdb/latest/reference/cli/influx/config/create/
`,
Before: withCli(),
Before: middleware.WithBeforeFns(withCli(), middleware.NoArgs),
Flags: append(
configPathAndPrintFlags,
&cli.StringFlag{
@ -176,7 +177,7 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/config/
and
https://docs.influxdata.com/influxdb/latest/reference/cli/influx/config/set/
`,
Before: withCli(),
Before: middleware.WithBeforeFns(withCli(), middleware.NoArgs),
Flags: append(
configPathAndPrintFlags,
&cli.StringFlag{
@ -236,7 +237,7 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/config/
and
https://docs.influxdata.com/influxdb/latest/reference/cli/influx/config/list/
`,
Before: withCli(),
Before: middleware.WithBeforeFns(withCli(), middleware.NoArgs),
Flags: configPathAndPrintFlags,
Action: func(ctx *cli.Context) error {
client := cmd.Client{CLI: getCLI(ctx)}

View File

@ -29,7 +29,7 @@ Examples:
influx dashboards -i $ID1 -i $ID2
`,
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
rawIds := ctx.StringSlice("id")
params.Ids = rawIds

View File

@ -58,7 +58,7 @@ func newDeleteCmd() cli.Command {
Destination: &params.Predicate,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
client := delete.Client{CLI: getCLI(ctx), DeleteApi: getAPI(ctx).DeleteApi}
return client.Delete(getContext(ctx), &params)

View File

@ -317,7 +317,7 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/export/all/
Value: &params.filters,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
parsedParams := export.AllParams{
OrgId: params.orgId,

View File

@ -27,7 +27,7 @@ func newOrgCreateCmd() cli.Command {
return cli.Command{
Name: "create",
Usage: "Create organization",
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.StringFlag{
@ -57,7 +57,7 @@ func newOrgDeleteCmd() cli.Command {
return cli.Command{
Name: "delete",
Usage: "Delete organization",
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.GenericFlag{
@ -83,7 +83,7 @@ func newOrgListCmd() cli.Command {
Name: "list",
Aliases: []string{"find", "ls"},
Usage: "List organizations",
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.StringFlag{
@ -114,7 +114,7 @@ func newOrgUpdateCmd() cli.Command {
return cli.Command{
Name: "update",
Usage: "Update organization",
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.GenericFlag{

View File

@ -23,7 +23,7 @@ func newOrgMembersAddCmd() cli.Command {
return cli.Command{
Name: "add",
Usage: "Add organization member",
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlagsNoPrint(),
&cli.GenericFlag{
@ -61,7 +61,7 @@ func newOrgMembersListCmd() cli.Command {
Name: "list",
Aliases: []string{"find", "ls"},
Usage: "List organization members",
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.StringFlag{
@ -93,7 +93,7 @@ func newOrgMembersRemoveCmd() cli.Command {
return cli.Command{
Name: "remove",
Usage: "Remove organization member",
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlagsNoPrint(),
&cli.GenericFlag{

View File

@ -10,7 +10,7 @@ func newPingCmd() cli.Command {
return cli.Command{
Name: "ping",
Usage: "Check the InfluxDB /health endpoint",
Before: middleware.WithBeforeFns(withCli(), withApi(false)),
Before: middleware.WithBeforeFns(withCli(), withApi(false), middleware.NoArgs),
Flags: coreFlags(),
Action: func(ctx *cli.Context) error {
client := ping.Client{

View File

@ -31,7 +31,7 @@ func newDeleteSecretCmd() cli.Command {
Name: "delete",
Usage: "Delete secret",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := secret.Client{
@ -52,7 +52,7 @@ func newListSecretCmd() cli.Command {
Usage: "List secrets",
Aliases: []string{"find", "ls"},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := secret.Client{
@ -85,7 +85,7 @@ func newUpdateSecretCmd() cli.Command {
Name: "update",
Usage: "Update secret",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := secret.Client{

View File

@ -11,7 +11,7 @@ func newSetupCmd() cli.Command {
return cli.Command{
Name: "setup",
Usage: "Setup instance with initial user, org, bucket",
Before: middleware.WithBeforeFns(withCli(), withApi(false)),
Before: middleware.WithBeforeFns(withCli(), withApi(false), middleware.NoArgs),
Flags: append(
commonFlagsNoToken(),
&cli.StringFlag{

View File

@ -36,7 +36,7 @@ Examples:
For information about Stacks and how they integrate with InfluxDB templates, see
https://docs.influxdata.com/influxdb/latest/reference/cli/influx/stacks/`,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Flags: append(
commonFlags(),
&cli.StringFlag{

View File

@ -81,7 +81,7 @@ func newTaskFindCmd() cli.Command {
Usage: "List tasks",
Aliases: []string{"find", "ls"},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := task.Client{
@ -135,7 +135,7 @@ func newTaskRetryFailedCmd() cli.Command {
Usage: "Retry failed runs",
Aliases: []string{"rtf"},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := task.Client{
@ -173,7 +173,7 @@ func newTaskUpdateCmd() cli.Command {
Usage: "Update task status or script. Provide a Flux script via the first argument or a file.",
ArgsUsage: "[flux script or '-' for stdin]",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := task.Client{
@ -207,7 +207,7 @@ func newTaskDeleteCmd() cli.Command {
Name: "delete",
Usage: "Delete tasks",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := task.Client{
@ -250,7 +250,7 @@ func newTaskLogFindCmd() cli.Command {
Usage: "List logs for a task",
Aliases: []string{"find", "ls"},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := task.Client{
@ -310,7 +310,7 @@ func newTaskRunFindCmd() cli.Command {
Usage: "List runs for a tasks",
Aliases: []string{"find", "ls"},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := task.Client{
@ -343,7 +343,7 @@ func newTaskRunRetryCmd() cli.Command {
Name: "retry",
Usage: "Retry a run",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := task.Client{

View File

@ -39,7 +39,7 @@ Examples:
newUpdateTelegrafCmd(),
},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := telegrafs.Client{
@ -87,7 +87,7 @@ Examples:
cat $CONFIG_FILE | influx telegrafs create -n $CFG_NAME -d $CFG_DESC
`,
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
conf, err := readConfig(ctx.String("file"))
if err != nil {
@ -133,7 +133,7 @@ Examples:
`,
Aliases: []string{"remove"},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
rawIds := ctx.StringSlice("id")
params.Ids = rawIds
@ -193,7 +193,7 @@ Examples:
cat $CONFIG_FILE | influx telegrafs update -i $ID -n $CFG_NAME -d $CFG_DESC
`,
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
conf, err := readConfig(ctx.String("file"))
if err != nil {

View File

@ -118,7 +118,7 @@ func newTemplateCmd() cli.Command {
Destination: &params.noTableBorders,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
parsedParams := template.SummarizeParams{
OrgId: params.orgId,
@ -149,7 +149,7 @@ func newTemplateValidateCmd() cli.Command {
Name: "validate",
Usage: "Validate the provided template",
Flags: append(commonFlagsNoPrint(), templateFlags(&params)...),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
parsedParams := template.ValidateParams{
OrgId: params.orgId,

View File

@ -53,7 +53,7 @@ func newUserCreateCmd() cli.Command {
Destination: &params.Password,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := user.Client{
@ -80,7 +80,7 @@ func newUserDeleteCmd() cli.Command {
Value: &id,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
client := user.Client{CLI: getCLI(ctx), UsersApi: getAPI(ctx).UsersApi}
return client.Delete(getContext(ctx), id)
@ -107,7 +107,7 @@ func newUserListCmd() cli.Command {
Destination: &params.Name,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
client := user.Client{CLI: getCLI(ctx), UsersApi: getAPI(ctx).UsersApi}
return client.List(getContext(ctx), &params)
@ -133,7 +133,7 @@ func newUserUpdateCmd() cli.Command {
Destination: &params.Name,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
client := user.Client{CLI: getCLI(ctx), UsersApi: getAPI(ctx).UsersApi}
return client.Update(getContext(ctx), &params)
@ -163,7 +163,7 @@ func newUserSetPasswordCmd() cli.Command {
Destination: &params.Password,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
client := user.Client{CLI: getCLI(ctx), UsersApi: getAPI(ctx).UsersApi}
return client.SetPassword(getContext(ctx), &params)

View File

@ -77,7 +77,7 @@ func newCreateV1AuthCmd() cli.Command {
Name: "create",
Usage: "Create authorization",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
params.ReadBucket = ctx.StringSlice("read-bucket")
params.WriteBucket = ctx.StringSlice("write-bucket")
@ -100,7 +100,7 @@ func newRemoveV1AuthCmd() cli.Command {
Name: "delete",
Usage: "Delete authorization",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
if err := params.AuthLookupParams.Validate(); err != nil {
return err
@ -139,7 +139,7 @@ func newListV1AuthCmd() cli.Command {
Usage: "List authorizations",
Aliases: []string{"ls", "find"},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := v1_auth.Client{
@ -160,7 +160,7 @@ func newSetActiveV1AuthCmd() cli.Command {
Name: "set-active",
Usage: "Change the status of an authorization to active",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
if err := params.AuthLookupParams.Validate(); err != nil {
return err
@ -185,7 +185,7 @@ func newSetInactiveV1AuthCmd() cli.Command {
Name: "set-inactive",
Usage: "Change the status of an authorization to inactive",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
if err := params.AuthLookupParams.Validate(); err != nil {
return err
@ -218,7 +218,7 @@ func newSetPswdV1AuthCmd() cli.Command {
Name: "set-password",
Usage: "Set a password for an existing authorization",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := v1_auth.Client{

View File

@ -55,7 +55,7 @@ func newV1DBRPListCmd() cli.Command {
Destination: &params.RP,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := v1dbrps.Client{
@ -100,7 +100,7 @@ func newV1DBRPCreateCmd() cli.Command {
Required: true,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := v1dbrps.Client{
@ -129,7 +129,7 @@ func newV1DBRPDeleteCmd() cli.Command {
Required: true,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := v1dbrps.Client{
@ -167,7 +167,7 @@ func newV1DBRPUpdateCmd() cli.Command {
Destination: &params.RP,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := v1dbrps.Client{

View File

@ -3,13 +3,15 @@ package main
import (
"fmt"
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
"github.com/urfave/cli"
)
func newVersionCmd() cli.Command {
return cli.Command{
Name: "version",
Usage: "Print the influx CLI version",
Name: "version",
Usage: "Print the influx CLI version",
Before: middleware.NoArgs,
Action: func(*cli.Context) error {
fmt.Printf("Influx CLI %s (git: %s) build_date: %s\n", version, commit, date)
return nil

View File

@ -0,0 +1,20 @@
package middleware
import (
"fmt"
"github.com/urfave/cli"
)
var NoArgs cli.BeforeFunc = func(ctx *cli.Context) error {
// `Before` funcs get run prior to resolving subcommands from args
if ctx.NArg() > 0 && ctx.App.Command(ctx.Args()[0]) == nil {
cmdName := ctx.Command.Name
if cmdName == "" && ctx.App.Name != "" {
cmdName = ctx.App.Name
}
// Use the same error format as `cobra.NoArgs` for consistency with the old CLI.
return fmt.Errorf("unknown command %q for %q", ctx.Args()[0], cmdName)
}
return nil
}

View File

@ -0,0 +1,118 @@
package middleware_test
import (
"bytes"
"fmt"
"io"
"testing"
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
)
func Test_NoArgs(t *testing.T) {
setup := func(out io.Writer, withCommand bool, withSubcommand bool) *cli.App {
app := cli.App{
Name: "test",
Before: middleware.NoArgs,
}
flags := []cli.Flag{
cli.StringFlag{
Name: "hello",
Required: true,
},
}
action := func(ctx *cli.Context) error {
_, err := fmt.Fprintf(out, "Hello, %s!", ctx.String("hello"))
return err
}
if withCommand {
app.Commands = []cli.Command{
{
Name: "command",
Before: middleware.NoArgs,
},
}
if withSubcommand {
app.Commands[0].Subcommands = []cli.Command{
{
Name: "subcommand",
Before: middleware.NoArgs,
Flags: flags,
Action: action,
},
}
} else {
app.Commands[0].Flags = flags
app.Commands[0].Action = action
}
} else {
app.Flags = flags
app.Action = action
}
return &app
}
testCases := []struct {
name string
cli []string
command bool
subcommand bool
expectOut string
expectErr string
}{
{
name: "top-level without args",
cli: []string{"--hello", "world"},
expectOut: "Hello, world!",
},
{
name: "top-level with args",
cli: []string{"--hello", "world", "etc"},
expectErr: `unknown command "etc" for "test"`,
},
{
name: "command without args",
cli: []string{"command", "--hello", "world"},
command: true,
expectOut: "Hello, world!",
},
{
name: "command with args",
cli: []string{"command", "--hello", "world", "etc"},
command: true,
expectErr: `unknown command "etc" for "command"`,
},
{
name: "subcommand without args",
cli: []string{"command", "subcommand", "--hello", "world"},
subcommand: true,
expectOut: "Hello, world!",
},
{
name: "subcommand with args",
cli: []string{"command", "subcommand", "--hello", "world", "etc"},
subcommand: true,
expectErr: `unknown command "etc" for "subcommand"`,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var out bytes.Buffer
cmd := setup(&out, tc.command || tc.subcommand, tc.subcommand)
err := cmd.Run(append([]string{"test"}, tc.cli...))
if tc.expectErr != "" {
require.Error(t, err)
require.EqualError(t, err, tc.expectErr)
return
}
require.NoError(t, err)
require.Equal(t, tc.expectOut, out.String())
})
}
}