From fd06b7f7a027227b91b3dd6d1d9d0a6cee128d0b Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Wed, 4 Oct 2023 13:57:49 -0500 Subject: [PATCH] fix: allow all environment variables to fallback prefix to `HOMEBREW_` (#10050) See the customer use-case in the code docs. --- cli/clibase/option.go | 9 +++++++++ cli/clibase/option_test.go | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/cli/clibase/option.go b/cli/clibase/option.go index b9a2e871d0..5743b3a4d1 100644 --- a/cli/clibase/option.go +++ b/cli/clibase/option.go @@ -262,6 +262,15 @@ func (optSet *OptionSet) ParseEnv(vs []EnvVar) error { } envVal, ok := envs[opt.Env] + if !ok { + // Homebrew strips all environment variables that do not start with `HOMEBREW_`. + // This prevented using brew to invoke the Coder agent, because the environment + // variables to not get passed down. + // + // A customer wanted to use their custom tap inside a workspace, which was failing + // because the agent lacked the environment variables to authenticate with Git. + envVal, ok = envs[`HOMEBREW_`+opt.Env] + } // Currently, empty values are treated as if the environment variable is // unset. This behavior is technically not correct as there is now no // way for a user to change a Default value to an empty string from diff --git a/cli/clibase/option_test.go b/cli/clibase/option_test.go index 79db040ea7..f093a20ec1 100644 --- a/cli/clibase/option_test.go +++ b/cli/clibase/option_test.go @@ -206,6 +206,26 @@ func TestOptionSet_ParseEnv(t *testing.T) { require.NoError(t, err) require.EqualValues(t, expected, actual.Value) }) + + t.Run("Homebrew", func(t *testing.T) { + t.Parallel() + + var agentToken clibase.String + + os := clibase.OptionSet{ + clibase.Option{ + Name: "Agent Token", + Value: &agentToken, + Env: "AGENT_TOKEN", + }, + } + + err := os.ParseEnv([]clibase.EnvVar{ + {Name: "HOMEBREW_AGENT_TOKEN", Value: "foo"}, + }) + require.NoError(t, err) + require.EqualValues(t, "foo", agentToken) + }) } func TestOptionSet_JsonMarshal(t *testing.T) {