Skip to content

Commit

Permalink
fix(influx): make CLI respect root token and host env vars in additio…
Browse files Browse the repository at this point in the history
…nn 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
  • Loading branch information
jsteenb2 committed Apr 21, 2020
1 parent 596d8fd commit c3d6d3a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
33 changes: 24 additions & 9 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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
}

Expand All @@ -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))
}
Expand Down
16 changes: 16 additions & 0 deletions cmd/influx/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 4 additions & 6 deletions cmd/influx/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -129,6 +127,7 @@ func setupF(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

s := tenant.OnboardClientService{
Client: client,
}
Expand All @@ -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()
Expand Down

0 comments on commit c3d6d3a

Please sign in to comment.