Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd/influx): easy switch config #17472

Merged
merged 1 commit into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
1. [17448](https://github.com/influxdata/influxdb/pull/17448): Add foundation for pkger stacks, stateful package management
1. [17462](https://github.com/influxdata/influxdb/pull/17462): Flag to disable scheduling of tasks
1. [17470](https://github.com/influxdata/influxdb/pull/17470): Add ability to output cli output as json and hide table headers
1. [17472](https://github.com/influxdata/influxdb/pull/17472): Add an easy way to switch config via cli

### Bug Fixes

Expand Down
38 changes: 35 additions & 3 deletions cmd/influx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ type cmdConfigBuilder struct {
}

func (b *cmdConfigBuilder) cmd() *cobra.Command {
cmd := b.newCmd("config", nil, false)
cmd := b.newCmd("config", b.cmdSwitchActiveRunEFn, false)
cmd.Short = "Config management commands"
cmd.Run = seeHelp
cmd.Args = cobra.ExactArgs(1)

cmd.AddCommand(
b.cmdCreate(),
b.cmdDelete(),
Expand All @@ -54,6 +55,37 @@ func (b *cmdConfigBuilder) cmd() *cobra.Command {
return cmd
}

func (b *cmdConfigBuilder) cmdSwitchActiveRunEFn(cmd *cobra.Command, args []string) error {
pp, err := b.svc.ParseConfigs()
if err != nil {
return err
}
b.name = args[0]
p0, ok := pp[b.name]
if !ok {
return &influxdb.Error{
Code: influxdb.ENotFound,
Msg: fmt.Sprintf("name %q is not found", b.name),
}
}
pp[b.name] = p0

if err := pp.Switch(b.name); err != nil {
return err
}

if err = b.svc.WriteConfigs(pp); err != nil {
return err
}

return b.printConfigs(configPrintOpts{
config: cfg{
name: b.name,
Config: pp[b.name],
},
})
}

func (b *cmdConfigBuilder) cmdCreate() *cobra.Command {
cmd := b.newCmd("create", b.cmdCreateRunEFn, false)
cmd.Short = "Create config"
Expand Down Expand Up @@ -201,7 +233,7 @@ func (b *cmdConfigBuilder) cmdUpdateRunEFn(*cobra.Command, []string) error {
return b.printConfigs(configPrintOpts{
config: cfg{
name: b.name,
Config: p0,
Config: pp[b.name],
},
})
}
Expand Down
78 changes: 78 additions & 0 deletions cmd/influx/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,84 @@ func TestCmdConfig(t *testing.T) {
}
})

t.Run("switch", func(t *testing.T) {
tests := []struct {
name string
original config.Configs
expected config.Configs
arg string
}{
{
name: "basic",
arg: "default",
original: config.Configs{
"config1": {
Org: "org2",
Active: true,
Token: "tok2",
Host: "http://localhost:8888",
},
"default": {
Org: "org1",
Active: false,
Token: "tok1",
Host: "http://localhost:9999",
},
},
expected: config.Configs{
"config1": {
Org: "org2",
Active: false,
Token: "tok2",
Host: "http://localhost:8888",
},
"default": {
Org: "org1",
Active: true,
Token: "tok1",
Host: "http://localhost:9999",
},
},
},
}
cmdFn := func(orginal, expected config.Configs) func(*globalFlags, genericCLIOpts) *cobra.Command {
svc := &config.MockConfigService{
ParseConfigsFn: func() (config.Configs, error) {
return orginal, nil
},
WriteConfigsFn: func(pp config.Configs) error {
if diff := cmp.Diff(expected, pp); diff != "" {
return &influxdb.Error{
Msg: fmt.Sprintf("write configs failed, diff %s", diff),
}
}
return nil
},
}

return func(g *globalFlags, opt genericCLIOpts) *cobra.Command {
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: g,
svc: svc,
}
return builder.cmd()
}
}
for _, tt := range tests {
fn := func(t *testing.T) {
builder := newInfluxCmdBuilder(
in(new(bytes.Buffer)),
out(ioutil.Discard),
)
cmd := builder.cmd(cmdFn(tt.original, tt.expected))
cmd.SetArgs([]string{"config", tt.arg})
require.NoError(t, cmd.Execute())
}
t.Run(tt.name, fn)
}
})

t.Run("set", func(t *testing.T) {
tests := []struct {
name string
Expand Down