From 5cd57d4f1eb35eae3a8d997576439c95c2f6ae96 Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Tue, 21 Apr 2020 09:50:40 -0700 Subject: [PATCH] fix(influx): make CLI respect root token and host env vars in additionn to config one thing to note here is we are deleting the default value on the host flag when it is registered. The config is the fallback and has the default value set. If the host flag has a default, the determination if the user set it or not is ambiguous. We can't have that. closes: #17812 --- cmd/influx/main.go | 33 ++++++++++++++++++++++++--------- cmd/influx/main_test.go | 16 ++++++++++++++++ cmd/influx/setup.go | 10 ++++------ 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/cmd/influx/main.go b/cmd/influx/main.go index 1015befa44..9900b75163 100644 --- a/cmd/influx/main.go +++ b/cmd/influx/main.go @@ -177,22 +177,31 @@ func (b *cmdInfluxBuilder) cmd(childCmdFns ...func(f *globalFlags, opt genericCL { DestP: &flags.Host, Flag: "host", - Default: "http://localhost:9999", Desc: "HTTP address of Influx", Persistent: true, }, } fOpts.mustRegister(cmd) - if flags.Token == "" { - // migration credential token - migrateOldCredential() + // migration credential token + migrateOldCredential() - // 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 config, then if a token flag - // is provided too, the flag will take precedence. - flags.Config = getConfigFromDefaultPath() + // 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 config, then if a token flag + // is provided too, the flag will take precedence. + cfg := getConfigFromDefaultPath() + + // we have some indirection here b/c of how the Config is embedded on the + // global flags type. For the time being, we check to see if there was a + // value set on flags registered (via env vars), and override the host/token + // values if they are. + if flags.Token != "" { + cfg.Token = flags.Token } + if flags.Host != "" { + cfg.Host = flags.Host + } + flags.Config = cfg cmd.PersistentFlags().BoolVar(&flags.local, "local", false, "Run commands locally against the filesystem") cmd.PersistentFlags().BoolVar(&flags.skipVerify, "skip-verify", false, "SkipVerify controls whether a client verifies the server's certificate chain and host name.") @@ -275,7 +284,10 @@ func getConfigFromDefaultPath() config.Config { if err != nil { return config.DefaultConfig } - activated, _ := config.ParseActiveConfig(r) + activated, err := config.ParseActiveConfig(r) + if err != nil { + return config.DefaultConfig + } return activated } @@ -284,14 +296,17 @@ func migrateOldCredential() { if err != nil { return // no need for migration } + tokB, err := ioutil.ReadFile(filepath.Join(dir, http.DefaultTokenFile)) if err != nil { return // no need for migration } + err = writeConfigToPath(strings.TrimSpace(string(tokB)), "", filepath.Join(dir, http.DefaultConfigsFile), dir) if err != nil { return } + // ignore the remove err _ = os.Remove(filepath.Join(dir, http.DefaultTokenFile)) } diff --git a/cmd/influx/main_test.go b/cmd/influx/main_test.go index 981ccd9d94..28b985120e 100644 --- a/cmd/influx/main_test.go +++ b/cmd/influx/main_test.go @@ -60,6 +60,22 @@ func Test_influx_cmd(t *testing.T) { local: true, }, }, + { + name: "env vars and flags set", + args: []string{"--local=true", "--token=flag-token", "--host=flag-host"}, + envVars: map[string]string{ + "INFLUX_TOKEN": "TOKEN", + "INFLUX_HOST": "HOST", + }, + expected: globalFlags{ + Config: config.Config{ + Token: "flag-token", + Host: "flag-host", + }, + skipVerify: false, + local: true, + }, + }, } for _, tt := range tests { diff --git a/cmd/influx/setup.go b/cmd/influx/setup.go index c9dfc39e24..714fc38abf 100644 --- a/cmd/influx/setup.go +++ b/cmd/influx/setup.go @@ -88,12 +88,10 @@ func setupUserF(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to retrieve data to setup instance: %v", err) } - fmt.Printf("req: %+v\n", req) result, err := s.OnboardUser(context.Background(), req) if err != nil { return fmt.Errorf("failed to setup instance: %v", err) } - fmt.Printf("result: %+v\n", result) w := cmd.OutOrStdout() if setupFlags.json { @@ -129,6 +127,7 @@ func setupF(cmd *cobra.Command, args []string) error { if err != nil { return err } + s := tenant.OnboardClientService{ Client: client, } @@ -144,10 +143,9 @@ func setupF(cmd *cobra.Command, args []string) error { if err != nil { return err } - localSVC := config.NewLocalConfigSVC( - dPath, - dir, - ) + + localSVC := config.NewLocalConfigSVC(dPath, dir) + existingConfigs := make(config.Configs) if _, err := os.Stat(dPath); err == nil { existingConfigs, _ = localSVC.ListConfigs()