diff --git a/clicommand/agent_start.go b/clicommand/agent_start.go index e98839075d..910955686d 100644 --- a/clicommand/agent_start.go +++ b/clicommand/agent_start.go @@ -489,7 +489,11 @@ var AgentStartCommand = cli.Command{ defer done() // Remove any config env from the environment to prevent them propagating to bootstrap - UnsetConfigFromEnvironment(c) + err = UnsetConfigFromEnvironment(c) + if err != nil { + fmt.Printf("%s", err) + os.Exit(1) + } // Check if git-mirrors are enabled if experiments.IsEnabled(`git-mirrors`) { diff --git a/clicommand/global.go b/clicommand/global.go index 6fa6a43f1f..e69ca8dcb8 100644 --- a/clicommand/global.go +++ b/clicommand/global.go @@ -1,6 +1,7 @@ package clicommand import ( + "errors" "fmt" "os" "reflect" @@ -150,12 +151,15 @@ func HandleGlobalFlags(l logger.Logger, cfg interface{}) func() { return HandleProfileFlag(l, cfg) } -func UnsetConfigFromEnvironment(c *cli.Context) { +func UnsetConfigFromEnvironment(c *cli.Context) error { flags := append(c.App.Flags, c.Command.Flags...) for _, fl := range flags { // use golang reflection to find EnvVar values on flags r := reflect.ValueOf(fl) f := reflect.Indirect(r).FieldByName(`EnvVar`) + if !f.IsValid() { + return errors.New("EnvVar field not found on flag") + } // split comma delimited env if envVars := f.String(); envVars != `` { for _, env := range strings.Split(envVars, ",") { @@ -163,6 +167,7 @@ func UnsetConfigFromEnvironment(c *cli.Context) { } } } + return nil } func loadAPIClientConfig(cfg interface{}, tokenField string) api.Config {