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
pull/17825/head
Johnny Steenbergen 2020-04-21 09:50:40 -07:00 committed by Johnny Steenbergen
parent 596d8fde45
commit 5cd57d4f1e
3 changed files with 44 additions and 15 deletions

View File

@ -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))
}

View File

@ -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 {

View File

@ -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()