Skip to content

Commit

Permalink
fix(influx): drop global flags and replace with locals
Browse files Browse the repository at this point in the history
many spots in the CLI that global flags show up cause confusion or just
add overhead with no value. This cleans that up. Other commands may
have token removed if it does not pertain.

commands that do not have global flags:
  * influx
  * influx completion
  * influx config (all sub commands as well)
  * influx template
  * influx template validate
  * influx transpile
  * influx version
  * all root commands that have no actions, aka influx organization
  • Loading branch information
jsteenb2 committed Jun 24, 2020
1 parent a7d29b1 commit bce2de6
Show file tree
Hide file tree
Showing 24 changed files with 223 additions and 268 deletions.
26 changes: 16 additions & 10 deletions cmd/influx/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ func cmdAuth(f *globalFlags, opt genericCLIOpts) *cobra.Command {
cmd.Run = seeHelp

cmd.AddCommand(
authActiveCmd(),
authCreateCmd(),
authDeleteCmd(),
authFindCmd(),
authInactiveCmd(),
authActiveCmd(f),
authCreateCmd(f),
authDeleteCmd(f),
authFindCmd(f),
authInactiveCmd(f),
)

return cmd
Expand Down Expand Up @@ -80,12 +80,14 @@ var authCreateFlags struct {
readDBRPPermission bool
}

func authCreateCmd() *cobra.Command {
func authCreateCmd(f *globalFlags) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create authorization",
RunE: checkSetupRunEMiddleware(&flags)(authorizationCreateF),
}

f.registerFlags(cmd)
authCreateFlags.org.register(cmd, false)

cmd.Flags().StringVarP(&authCreateFlags.user, "user", "u", "", "The user name")
Expand Down Expand Up @@ -301,14 +303,15 @@ var authorizationFindFlags struct {
userID string
}

func authFindCmd() *cobra.Command {
func authFindCmd(f *globalFlags) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List authorizations",
Aliases: []string{"find", "ls"},
RunE: checkSetupRunEMiddleware(&flags)(authorizationFindF),
}

f.registerFlags(cmd)
authorizationFindFlags.org.register(cmd, false)
registerPrintOptions(cmd, &authCRUDFlags.hideHeaders, &authCRUDFlags.json)
cmd.Flags().StringVarP(&authorizationFindFlags.user, "user", "u", "", "The user")
Expand Down Expand Up @@ -393,13 +396,14 @@ func authorizationFindF(cmd *cobra.Command, args []string) error {
})
}

func authDeleteCmd() *cobra.Command {
func authDeleteCmd(f *globalFlags) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete authorization",
RunE: checkSetupRunEMiddleware(&flags)(authorizationDeleteF),
}

f.registerFlags(cmd)
registerPrintOptions(cmd, &authCRUDFlags.hideHeaders, &authCRUDFlags.json)
cmd.Flags().StringVarP(&authCRUDFlags.id, "id", "i", "", "The authorization ID (required)")
cmd.MarkFlagRequired("id")
Expand Down Expand Up @@ -458,12 +462,13 @@ func authorizationDeleteF(cmd *cobra.Command, args []string) error {
})
}

func authActiveCmd() *cobra.Command {
func authActiveCmd(f *globalFlags) *cobra.Command {
cmd := &cobra.Command{
Use: "active",
Short: "Active authorization",
RunE: checkSetupRunEMiddleware(&flags)(authorizationActiveF),
}
f.registerFlags(cmd)

registerPrintOptions(cmd, &authCRUDFlags.hideHeaders, &authCRUDFlags.json)
cmd.Flags().StringVarP(&authCRUDFlags.id, "id", "i", "", "The authorization ID (required)")
Expand Down Expand Up @@ -524,13 +529,14 @@ func authorizationActiveF(cmd *cobra.Command, args []string) error {
})
}

func authInactiveCmd() *cobra.Command {
func authInactiveCmd(f *globalFlags) *cobra.Command {
cmd := &cobra.Command{
Use: "inactive",
Short: "Inactive authorization",
RunE: checkSetupRunEMiddleware(&flags)(authorizationInactiveF),
}

f.registerFlags(cmd)
registerPrintOptions(cmd, &authCRUDFlags.hideHeaders, &authCRUDFlags.json)
cmd.Flags().StringVarP(&authCRUDFlags.id, "id", "i", "", "The authorization ID (required)")
cmd.MarkFlagRequired("id")
Expand Down
2 changes: 2 additions & 0 deletions cmd/influx/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The target directory, and any parent directories, are created automatically.
Data file have extension .tsm; meta data is written to %s in the same directory.`,
bolt.DefaultFilename)

f.registerFlags(cmd)

opts := flagOpts{
{
DestP: &backupFlags.Path,
Expand Down
22 changes: 14 additions & 8 deletions cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
type bucketSVCsFn func() (influxdb.BucketService, influxdb.OrganizationService, error)

func cmdBucket(f *globalFlags, opt genericCLIOpts) *cobra.Command {
builder := newCmdBucketBuilder(newBucketSVCs, opt)
builder.globalFlags = f
builder := newCmdBucketBuilder(newBucketSVCs, f, opt)
return builder.cmd()
}

Expand All @@ -32,15 +31,16 @@ type cmdBucketBuilder struct {
retention string
}

func newCmdBucketBuilder(svcsFn bucketSVCsFn, opts genericCLIOpts) *cmdBucketBuilder {
func newCmdBucketBuilder(svcsFn bucketSVCsFn, f *globalFlags, opts genericCLIOpts) *cmdBucketBuilder {
return &cmdBucketBuilder{
globalFlags: f,
genericCLIOpts: opts,
svcFn: svcsFn,
}
}

func (b *cmdBucketBuilder) cmd() *cobra.Command {
cmd := b.newCmd("bucket", nil, false)
cmd := b.newCmd("bucket", nil)
cmd.Short = "Bucket management commands"
cmd.TraverseChildren = true
cmd.Run = seeHelp
Expand All @@ -55,7 +55,7 @@ func (b *cmdBucketBuilder) cmd() *cobra.Command {
}

func (b *cmdBucketBuilder) cmdCreate() *cobra.Command {
cmd := b.newCmd("create", b.cmdCreateRunEFn, true)
cmd := b.newCmd("create", b.cmdCreateRunEFn)
cmd.Short = "Create bucket"

opts := flagOpts{
Expand Down Expand Up @@ -111,7 +111,7 @@ func (b *cmdBucketBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
}

func (b *cmdBucketBuilder) cmdDelete() *cobra.Command {
cmd := b.newCmd("delete", b.cmdDeleteRunEFn, true)
cmd := b.newCmd("delete", b.cmdDeleteRunEFn)
cmd.Short = "Delete bucket"

cmd.Flags().StringVarP(&b.id, "id", "i", "", "The bucket ID, required if name isn't provided")
Expand Down Expand Up @@ -166,7 +166,7 @@ func (b *cmdBucketBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) er
}

func (b *cmdBucketBuilder) cmdList() *cobra.Command {
cmd := b.newCmd("list", b.cmdListRunEFn, true)
cmd := b.newCmd("list", b.cmdListRunEFn)
cmd.Short = "List buckets"
cmd.Aliases = []string{"find", "ls"}

Expand Down Expand Up @@ -231,7 +231,7 @@ func (b *cmdBucketBuilder) cmdListRunEFn(cmd *cobra.Command, args []string) erro
}

func (b *cmdBucketBuilder) cmdUpdate() *cobra.Command {
cmd := b.newCmd("update", b.cmdUpdateRunEFn, true)
cmd := b.newCmd("update", b.cmdUpdateRunEFn)
cmd.Short = "Update bucket"

opts := flagOpts{
Expand Down Expand Up @@ -289,6 +289,12 @@ func (b *cmdBucketBuilder) cmdUpdateRunEFn(cmd *cobra.Command, args []string) er
return b.printBuckets(bucketPrintOpt{bucket: bkt})
}

func (b *cmdBucketBuilder) newCmd(use string, runE func(*cobra.Command, []string) error) *cobra.Command {
cmd := b.genericCLIOpts.newCmd(use, runE, true)
b.globalFlags.registerFlags(cmd)
return cmd
}

func (b *cmdBucketBuilder) registerPrintFlags(cmd *cobra.Command) {
registerPrintOptions(cmd, &b.hideHeaders, &b.json)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/influx/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestCmdBucket(t *testing.T) {
}

return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
return newCmdBucketBuilder(fakeSVCFn(svc), opt).cmd()
return newCmdBucketBuilder(fakeSVCFn(svc), g, opt).cmd()
}
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func TestCmdBucket(t *testing.T) {
}

return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
return newCmdBucketBuilder(fakeSVCFn(svc), opt).cmd()
return newCmdBucketBuilder(fakeSVCFn(svc), g, opt).cmd()
}
}

Expand Down Expand Up @@ -319,7 +319,7 @@ func TestCmdBucket(t *testing.T) {
}

return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
return newCmdBucketBuilder(fakeSVCFn(svc), opt).cmd()
return newCmdBucketBuilder(fakeSVCFn(svc), g, opt).cmd()
}, calls
}

Expand Down Expand Up @@ -423,7 +423,7 @@ func TestCmdBucket(t *testing.T) {
}

return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
return newCmdBucketBuilder(fakeSVCFn(svc), opt).cmd()
return newCmdBucketBuilder(fakeSVCFn(svc), g, opt).cmd()
}
}

Expand Down
3 changes: 0 additions & 3 deletions cmd/influx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ func cmdConfig(f *globalFlags, opt genericCLIOpts) *cobra.Command {
}
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: f,
svc: config.NewLocalConfigSVC(path, dir),
}
builder.globalFlags = f
return builder.cmd()
}

type cmdConfigBuilder struct {
genericCLIOpts
*globalFlags

name string
url string
Expand Down
7 changes: 0 additions & 7 deletions cmd/influx/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func TestCmdConfig(t *testing.T) {
return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: g,
svc: svc,
}
return builder.cmd()
Expand All @@ -123,7 +122,6 @@ func TestCmdConfig(t *testing.T) {
cmdFn := func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: g,
svc: &mockConfigService{
CreateConfigFn: func(cfg config.Config) (config.Config, error) {
return cfg, nil
Expand Down Expand Up @@ -225,7 +223,6 @@ func TestCmdConfig(t *testing.T) {
return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: g,
svc: svc,
}
return builder.cmd()
Expand Down Expand Up @@ -319,7 +316,6 @@ func TestCmdConfig(t *testing.T) {
return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: g,
svc: svc,
}
return builder.cmd()
Expand All @@ -343,7 +339,6 @@ func TestCmdConfig(t *testing.T) {
cmdFn := func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: g,
svc: &mockConfigService{
CreateConfigFn: func(cfg config.Config) (config.Config, error) {
return cfg, nil
Expand Down Expand Up @@ -431,7 +426,6 @@ func TestCmdConfig(t *testing.T) {
return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: g,
svc: svc,
}
return builder.cmd()
Expand Down Expand Up @@ -484,7 +478,6 @@ func TestCmdConfig(t *testing.T) {
return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: g,
svc: svc,
}
return builder.cmd()
Expand Down
8 changes: 7 additions & 1 deletion cmd/influx/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type cmdDeleteBuilder struct {
}

func (b *cmdDeleteBuilder) cmd() *cobra.Command {
cmd := b.newCmd("delete", b.fluxDeleteF, true)
cmd := b.newCmd("delete", b.fluxDeleteF)
cmd.Short = "Delete points from influxDB"
cmd.Long = `Delete points from influxDB, by specify start, end time
and a sql like predicate string.`
Expand Down Expand Up @@ -97,3 +97,9 @@ func (b *cmdDeleteBuilder) fluxDeleteF(cmd *cobra.Command, args []string) error

return nil
}

func (b *cmdDeleteBuilder) newCmd(use string, runE func(*cobra.Command, []string) error) *cobra.Command {
cmd := b.genericCLIOpts.newCmd(use, runE, true)
b.globalFlags.registerFlags(cmd)
return cmd
}
Loading

0 comments on commit bce2de6

Please sign in to comment.