diff --git a/CHANGELOG.md b/CHANGELOG.md index dd78f11..e6d95db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/cmd/influx/apply.go b/cmd/influx/apply.go index 3290f1a..dffe0b4 100644 --- a/cmd/influx/apply.go +++ b/cmd/influx/apply.go @@ -164,7 +164,7 @@ https://github.com/influxdata/community-templates. Value: ¶ms.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, diff --git a/cmd/influx/auth.go b/cmd/influx/auth.go index 1527eed..87695da 100644 --- a/cmd/influx/auth.go +++ b/cmd/influx/auth.go @@ -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{ diff --git a/cmd/influx/bucket.go b/cmd/influx/bucket.go index 6d2a22e..90eb6dd 100644 --- a/cmd/influx/bucket.go +++ b/cmd/influx/bucket.go @@ -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{ diff --git a/cmd/influx/bucket_schema.go b/cmd/influx/bucket_schema.go index 7dc6b2d..860cca6 100644 --- a/cmd/influx/bucket_schema.go +++ b/cmd/influx/bucket_schema.go @@ -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{ diff --git a/cmd/influx/config.go b/cmd/influx/config.go index e473123..c7ffcbc 100644 --- a/cmd/influx/config.go +++ b/cmd/influx/config.go @@ -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)} diff --git a/cmd/influx/dashboards.go b/cmd/influx/dashboards.go index 024a08d..7132d29 100644 --- a/cmd/influx/dashboards.go +++ b/cmd/influx/dashboards.go @@ -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 diff --git a/cmd/influx/delete.go b/cmd/influx/delete.go index d46829f..d23f463 100644 --- a/cmd/influx/delete.go +++ b/cmd/influx/delete.go @@ -58,7 +58,7 @@ func newDeleteCmd() cli.Command { Destination: ¶ms.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), ¶ms) diff --git a/cmd/influx/export.go b/cmd/influx/export.go index 42dce7f..2e5ab61 100644 --- a/cmd/influx/export.go +++ b/cmd/influx/export.go @@ -317,7 +317,7 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/export/all/ Value: ¶ms.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, diff --git a/cmd/influx/org.go b/cmd/influx/org.go index e0d5312..84c6912 100644 --- a/cmd/influx/org.go +++ b/cmd/influx/org.go @@ -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{ diff --git a/cmd/influx/org_members.go b/cmd/influx/org_members.go index 014fa97..c35f410 100644 --- a/cmd/influx/org_members.go +++ b/cmd/influx/org_members.go @@ -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{ diff --git a/cmd/influx/ping.go b/cmd/influx/ping.go index 68c0b29..a298c50 100644 --- a/cmd/influx/ping.go +++ b/cmd/influx/ping.go @@ -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{ diff --git a/cmd/influx/secret.go b/cmd/influx/secret.go index 4ec94bb..d9277c1 100644 --- a/cmd/influx/secret.go +++ b/cmd/influx/secret.go @@ -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{ diff --git a/cmd/influx/setup.go b/cmd/influx/setup.go index 29237cf..3bc2bc1 100644 --- a/cmd/influx/setup.go +++ b/cmd/influx/setup.go @@ -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{ diff --git a/cmd/influx/stacks.go b/cmd/influx/stacks.go index a02c47d..1727c5d 100644 --- a/cmd/influx/stacks.go +++ b/cmd/influx/stacks.go @@ -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{ diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 969477e..ac2180e 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -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{ diff --git a/cmd/influx/telegrafs.go b/cmd/influx/telegrafs.go index a49e552..e2bd63c 100644 --- a/cmd/influx/telegrafs.go +++ b/cmd/influx/telegrafs.go @@ -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 { diff --git a/cmd/influx/template.go b/cmd/influx/template.go index 9bcc21b..3c0490c 100644 --- a/cmd/influx/template.go +++ b/cmd/influx/template.go @@ -118,7 +118,7 @@ func newTemplateCmd() cli.Command { Destination: ¶ms.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(¶ms)...), - 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, diff --git a/cmd/influx/user.go b/cmd/influx/user.go index 3e03d86..20ed99f 100644 --- a/cmd/influx/user.go +++ b/cmd/influx/user.go @@ -53,7 +53,7 @@ func newUserCreateCmd() cli.Command { Destination: ¶ms.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: ¶ms.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), ¶ms) @@ -133,7 +133,7 @@ func newUserUpdateCmd() cli.Command { Destination: ¶ms.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), ¶ms) @@ -163,7 +163,7 @@ func newUserSetPasswordCmd() cli.Command { Destination: ¶ms.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), ¶ms) diff --git a/cmd/influx/v1_auth.go b/cmd/influx/v1_auth.go index 0eef1ff..82fdb30 100644 --- a/cmd/influx/v1_auth.go +++ b/cmd/influx/v1_auth.go @@ -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{ diff --git a/cmd/influx/v1_dbrp.go b/cmd/influx/v1_dbrp.go index 579ee1c..1ba6b46 100644 --- a/cmd/influx/v1_dbrp.go +++ b/cmd/influx/v1_dbrp.go @@ -55,7 +55,7 @@ func newV1DBRPListCmd() cli.Command { Destination: ¶ms.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: ¶ms.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{ diff --git a/cmd/influx/version.go b/cmd/influx/version.go index aa9ce05..0b7508c 100644 --- a/cmd/influx/version.go +++ b/cmd/influx/version.go @@ -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 diff --git a/pkg/cli/middleware/noargs.go b/pkg/cli/middleware/noargs.go new file mode 100644 index 0000000..4802637 --- /dev/null +++ b/pkg/cli/middleware/noargs.go @@ -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 +} diff --git a/pkg/cli/middleware/noargs_test.go b/pkg/cli/middleware/noargs_test.go new file mode 100644 index 0000000..128baae --- /dev/null +++ b/pkg/cli/middleware/noargs_test.go @@ -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()) + }) + } +}