diff --git a/CHANGELOG.md b/CHANGELOG.md index 46db8315e74..e403caddac1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cmd/influx/config.go b/cmd/influx/config.go index 5bc6ffdeba1..4184caccaf9 100644 --- a/cmd/influx/config.go +++ b/cmd/influx/config.go @@ -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(), @@ -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" @@ -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], }, }) } diff --git a/cmd/influx/config_test.go b/cmd/influx/config_test.go index 65734d9edc3..919a3ccaa79 100644 --- a/cmd/influx/config_test.go +++ b/cmd/influx/config_test.go @@ -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