Skip to content

Commit

Permalink
chore(influx): refactor org cmd with buidler pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Jan 13, 2020
1 parent fdc3e76 commit 3b62340
Show file tree
Hide file tree
Showing 9 changed files with 1,101 additions and 608 deletions.
44 changes: 15 additions & 29 deletions cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

type bucketSVCsFn func() (influxdb.BucketService, influxdb.OrganizationService, error)

func cmdBucket(svcsFn bucketSVCsFn, opts ...genericCLIOptFn) *cobra.Command {
return newCmdBucketBuilder(svcsFn, opts...).cmdBucket()
func cmdBucket(opts ...genericCLIOptFn) *cobra.Command {
return newCmdBucketBuilder(newBucketSVCs, opts...).cmd()
}

type cmdBucketBuilder struct {
Expand Down Expand Up @@ -46,13 +46,11 @@ func newCmdBucketBuilder(svcsFn bucketSVCsFn, opts ...genericCLIOptFn) *cmdBucke
}
}

func (b *cmdBucketBuilder) cmdBucket() *cobra.Command {
cmd := &cobra.Command{
Use: "bucket",
Short: "Bucket management commands",
TraverseChildren: true,
Run: seeHelp,
}
func (b *cmdBucketBuilder) cmd() *cobra.Command {
cmd := b.newCmd("bucket", nil)
cmd.Short = "Bucket management commands"
cmd.TraverseChildren = true
cmd.Run = seeHelp
cmd.AddCommand(
b.cmdCreate(),
b.cmdDelete(),
Expand All @@ -64,11 +62,8 @@ func (b *cmdBucketBuilder) cmdBucket() *cobra.Command {
}

func (b *cmdBucketBuilder) cmdCreate() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create bucket",
RunE: wrapCheckSetup(b.cmdCreateRunEFn),
}
cmd := b.newCmd("create", b.cmdCreateRunEFn)
cmd.Short = "Create bucket"

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

func (b *cmdBucketBuilder) cmdDelete() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete bucket",
RunE: wrapCheckSetup(b.cmdDeleteRunEFn),
}
cmd := b.newCmd("delete", b.cmdDeleteRunEFn)
cmd.Short = "Delete bucket"

cmd.Flags().StringVarP(&b.id, "id", "i", "", "The bucket ID (required)")
cmd.MarkFlagRequired("id")
Expand Down Expand Up @@ -175,11 +167,8 @@ func (b *cmdBucketBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) er
}

func (b *cmdBucketBuilder) cmdFind() *cobra.Command {
cmd := &cobra.Command{
Use: "find",
Short: "Find buckets",
RunE: wrapCheckSetup(b.cmdFindRunEFn),
}
cmd := b.newCmd("find", b.cmdFindRunEFn)
cmd.Short = "Find buckets"

opts := flagOpts{
{
Expand Down Expand Up @@ -253,11 +242,8 @@ func (b *cmdBucketBuilder) cmdFindRunEFn(cmd *cobra.Command, args []string) erro
}

func (b *cmdBucketBuilder) cmdUpdate() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "Update bucket",
RunE: wrapCheckSetup(b.cmdUpdateRunEFn),
}
cmd := b.newCmd("update", b.cmdUpdateRunEFn)
cmd.Short = "Update bucket"

opts := flagOpts{
{
Expand Down
10 changes: 5 additions & 5 deletions cmd/influx/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestBucket(t *testing.T) {
func TestCmdBucket(t *testing.T) {
setViperOptions()

orgID := influxdb.ID(9000)
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestBucket(t *testing.T) {
defer addEnvVars(t, tt.envVars)()

cmd := cmdFn(tt.expectedBucket)
cmd.Flags().Parse(tt.flags)
cmd.LocalFlags().Parse(tt.flags)
require.NoError(t, cmd.Execute())
}

Expand Down Expand Up @@ -161,7 +161,7 @@ func TestBucket(t *testing.T) {
fn := func(t *testing.T) {
cmd := cmdFn(tt.expectedID)
idFlag := tt.flag + tt.expectedID.String()
cmd.Flags().Parse([]string{idFlag})
cmd.LocalFlags().Parse([]string{idFlag})
require.NoError(t, cmd.Execute())
}

Expand Down Expand Up @@ -269,7 +269,7 @@ func TestBucket(t *testing.T) {
defer addEnvVars(t, tt.envVars)()

cmd, calls := cmdFn()
cmd.Flags().Parse(tt.flags)
cmd.LocalFlags().Parse(tt.flags)

require.NoError(t, cmd.Execute())
assert.Equal(t, tt.expected, *calls)
Expand Down Expand Up @@ -363,7 +363,7 @@ func TestBucket(t *testing.T) {
defer addEnvVars(t, tt.envVars)()

cmd := cmdFn(tt.expected)
cmd.Flags().Parse(tt.flags)
cmd.LocalFlags().Parse(tt.flags)
require.NoError(t, cmd.Execute())
}

Expand Down
57 changes: 41 additions & 16 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,27 @@ func newHTTPClient() (*httpc.Client, error) {
return httpClient, nil
}

type genericCLIOptFn func(*genericCLIOpts)
type (
runEWrapFn func(fn func(*cobra.Command, []string) error) func(*cobra.Command, []string) error

genericCLIOptFn func(*genericCLIOpts)
)

type genericCLIOpts struct {
in io.Reader
w io.Writer
in io.Reader
w io.Writer
runEWrapFn runEWrapFn
}

func (o genericCLIOpts) newCmd(use string) *cobra.Command {
cmd := &cobra.Command{Use: use}
cmd.SetOutput(o.w)
func (o genericCLIOpts) newCmd(use string, runE func(*cobra.Command, []string) error) *cobra.Command {
cmd := &cobra.Command{
Use: use,
RunE: runE,
}
if runE != nil && o.runEWrapFn != nil {
cmd.RunE = o.runEWrapFn(runE)
}
cmd.SetOut(o.w)
return cmd
}

Expand All @@ -75,29 +86,43 @@ func out(w io.Writer) genericCLIOptFn {
}
}

func runEWrap(fn runEWrapFn) genericCLIOptFn {
return func(opts *genericCLIOpts) {
opts.runEWrapFn = fn
}
}

var flags struct {
token string
host string
local bool
skipVerify bool
}

func influxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "influx",
Short: "Influx Client",
SilenceUsage: true,
func influxCmd(opts ...genericCLIOptFn) *cobra.Command {
opt := genericCLIOpts{
in: os.Stdin,
w: os.Stdout,
}
for _, o := range opts {
o(&opt)
}

cmd := opt.newCmd("influx", nil)
cmd.Short = "Influx Client"
cmd.SilenceUsage = true

setViperOptions()

runEWrapper := runEWrap(wrapCheckSetup)

cmd.AddCommand(
cmdAuth(),
cmdBucket(newBucketSVCs),
cmdBucket(runEWrapper),
cmdDelete(),
cmdOrganization(),
cmdOrganization(runEWrapper),
cmdPing(),
cmdPkg(newPkgerSVC),
cmdPkg(runEWrapper),
cmdQuery(),
cmdTranspile(),
cmdREPL(),
Expand All @@ -107,7 +132,7 @@ func influxCmd() *cobra.Command {
cmdWrite(),
)

opts := flagOpts{
fOpts := flagOpts{
{
DestP: &flags.token,
Flag: "token",
Expand All @@ -123,7 +148,7 @@ func influxCmd() *cobra.Command {
Persistent: true,
},
}
opts.mustRegister(cmd)
fOpts.mustRegister(cmd)

// this is after the flagOpts register b/c we don't want to show the default value
// in the usage display. This will add it as the token value, then if a token flag
Expand Down
Loading

0 comments on commit 3b62340

Please sign in to comment.