From d3ecc7022ae4e19214aff23c0795b5c7708d536f Mon Sep 17 00:00:00 2001 From: Paul Annesley Date: Wed, 18 Jan 2023 14:58:28 +1030 Subject: [PATCH 1/3] CLI: 'env' command moved to 'env dump' subcommand This makes room for other future subcommands that relate to environment. --- clicommand/{env.go => env_dump.go} | 30 ++++++++++++++++-------------- hook/scriptwrapper.go | 12 ++++++------ hook/scriptwrapper_test.go | 12 ++++++------ main.go | 8 +++++++- 4 files changed, 35 insertions(+), 27 deletions(-) rename clicommand/{env.go => env_dump.go} (60%) diff --git a/clicommand/env.go b/clicommand/env_dump.go similarity index 60% rename from clicommand/env.go rename to clicommand/env_dump.go index f704b61bf1..001a24d10a 100644 --- a/clicommand/env.go +++ b/clicommand/env_dump.go @@ -9,8 +9,8 @@ import ( "github.com/urfave/cli" ) -const envDescription = `Usage: - buildkite-agent env [options] +var EnvDumpHelpDescription = `Usage: + buildkite-agent env dump [options] Description: Prints out the environment of the current process as a JSON object, easily @@ -18,20 +18,22 @@ Description: that hooks make to the environment. Example: - $ buildkite-agent env - Prints the environment passed into the process -` + $ buildkite-agent env dump --format json-pretty` -var EnvCommand = cli.Command{ - Name: "env", - Usage: "Prints out the environment of the current process as a JSON object", - Description: envDescription, +type EnvDumpConfig struct { +} + +var EnvDumpCommand = cli.Command{ + Name: "dump", + Usage: "Print the environment of the current process as a JSON object", + Description: EnvDumpHelpDescription, Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "pretty", - Usage: "Pretty print the JSON output", - EnvVar: "BUILDKITE_AGENT_ENV_PRETTY", + cli.StringFlag{ + Name: "format", + Usage: "Output format; json or json-pretty", + EnvVar: "BUILDKITE_AGENT_ENV_DUMP_FORMAT", + Value: "json", }, }, Action: func(c *cli.Context) error { @@ -45,7 +47,7 @@ var EnvCommand = cli.Command{ } enc := json.NewEncoder(c.App.Writer) - if c.Bool("pretty") { + if c.String("format") == "json-pretty" { enc.SetIndent("", " ") } diff --git a/hook/scriptwrapper.go b/hook/scriptwrapper.go index fdcbdc6081..b2a25b0936 100644 --- a/hook/scriptwrapper.go +++ b/hook/scriptwrapper.go @@ -20,26 +20,26 @@ const ( batchScript = `@echo off SETLOCAL ENABLEDELAYEDEXPANSION -buildkite-agent env > "{{.BeforeEnvFileName}}" +buildkite-agent env dump > "{{.BeforeEnvFileName}}" CALL "{{.PathToHook}}" SET BUILDKITE_HOOK_EXIT_STATUS=!ERRORLEVEL! SET BUILDKITE_HOOK_WORKING_DIR=%CD% -buildkite-agent env > "{{.AfterEnvFileName}}" +buildkite-agent env dump > "{{.AfterEnvFileName}}" EXIT %BUILDKITE_HOOK_EXIT_STATUS%` powershellScript = `$ErrorActionPreference = "STOP" -buildkite-agent env | Set-Content "{{.BeforeEnvFileName}}" +buildkite-agent env dump | Set-Content "{{.BeforeEnvFileName}}" {{.PathToHook}} if ($LASTEXITCODE -eq $null) {$Env:BUILDKITE_HOOK_EXIT_STATUS = 0} else {$Env:BUILDKITE_HOOK_EXIT_STATUS = $LASTEXITCODE} $Env:BUILDKITE_HOOK_WORKING_DIR = $PWD | Select-Object -ExpandProperty Path -buildkite-agent env | Set-Content "{{.AfterEnvFileName}}" +buildkite-agent env dump | Set-Content "{{.AfterEnvFileName}}" exit $Env:BUILDKITE_HOOK_EXIT_STATUS` - bashScript = `buildkite-agent env > "{{.BeforeEnvFileName}}" + bashScript = `buildkite-agent env dump > "{{.BeforeEnvFileName}}" . "{{.PathToHook}}" export BUILDKITE_HOOK_EXIT_STATUS=$? export BUILDKITE_HOOK_WORKING_DIR=$PWD -buildkite-agent env > "{{.AfterEnvFileName}}" +buildkite-agent env dump > "{{.AfterEnvFileName}}" exit $BUILDKITE_HOOK_EXIT_STATUS` ) diff --git a/hook/scriptwrapper_test.go b/hook/scriptwrapper_test.go index bffb962cfa..e9224ada55 100644 --- a/hook/scriptwrapper_test.go +++ b/hook/scriptwrapper_test.go @@ -104,11 +104,11 @@ func TestHookScriptsAreGeneratedCorrectlyOnWindowsBatch(t *testing.T) { // See: https://pkg.go.dev/fmt > ctrl-f for "%%" scriptTemplate := `@echo off SETLOCAL ENABLEDELAYEDEXPANSION -buildkite-agent env > "%s" +buildkite-agent env dump > "%s" CALL "%s" SET BUILDKITE_HOOK_EXIT_STATUS=!ERRORLEVEL! SET BUILDKITE_HOOK_WORKING_DIR=%%CD%% -buildkite-agent env > "%s" +buildkite-agent env dump > "%s" EXIT %%BUILDKITE_HOOK_EXIT_STATUS%%` assertScriptLike(t, scriptTemplate, hookFile.Name(), wrapper) @@ -134,11 +134,11 @@ func TestHookScriptsAreGeneratedCorrectlyOnWindowsPowershell(t *testing.T) { defer wrapper.Close() scriptTemplate := `$ErrorActionPreference = "STOP" -buildkite-agent env | Set-Content "%s" +buildkite-agent env dump | Set-Content "%s" %s if ($LASTEXITCODE -eq $null) {$Env:BUILDKITE_HOOK_EXIT_STATUS = 0} else {$Env:BUILDKITE_HOOK_EXIT_STATUS = $LASTEXITCODE} $Env:BUILDKITE_HOOK_WORKING_DIR = $PWD | Select-Object -ExpandProperty Path -buildkite-agent env | Set-Content "%s" +buildkite-agent env dump | Set-Content "%s" exit $Env:BUILDKITE_HOOK_EXIT_STATUS` assertScriptLike(t, scriptTemplate, hookFile.Name(), wrapper) @@ -163,11 +163,11 @@ func TestHookScriptsAreGeneratedCorrectlyOnUnix(t *testing.T) { defer wrapper.Close() - scriptTemplate := `buildkite-agent env > "%s" + scriptTemplate := `buildkite-agent env dump > "%s" . "%s" export BUILDKITE_HOOK_EXIT_STATUS=$? export BUILDKITE_HOOK_WORKING_DIR=$PWD -buildkite-agent env > "%s" +buildkite-agent env dump > "%s" exit $BUILDKITE_HOOK_EXIT_STATUS` assertScriptLike(t, scriptTemplate, hookFile.Name(), wrapper) diff --git a/main.go b/main.go index adde42fd27..dd020d00e7 100644 --- a/main.go +++ b/main.go @@ -82,6 +82,13 @@ func main() { clicommand.ArtifactShasumCommand, }, }, + { + Name: "env", + Usage: "Something something environment variables", + Subcommands: []cli.Command{ + clicommand.EnvDumpCommand, + }, + }, { Name: "meta-data", Usage: "Get/set data from Buildkite jobs", @@ -114,7 +121,6 @@ func main() { clicommand.StepUpdateCommand, }, }, - clicommand.EnvCommand, clicommand.BootstrapCommand, } From 3a1f8aa2259ab793d83ae3186dcc09b51ee26a71 Mon Sep 17 00:00:00 2001 From: Paul Annesley Date: Wed, 18 Jan 2023 15:34:50 +1030 Subject: [PATCH 2/3] Top-level `env` Usage text fixed. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index dd020d00e7..f803e4c27f 100644 --- a/main.go +++ b/main.go @@ -84,7 +84,7 @@ func main() { }, { Name: "env", - Usage: "Something something environment variables", + Usage: "Process environment subcommands", Subcommands: []cli.Command{ clicommand.EnvDumpCommand, }, From 7f71f7cefc79cc0a9535af7556baae3f5f2ef474 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Wed, 18 Jan 2023 17:00:24 +1100 Subject: [PATCH 3/3] agent.Expect("env") -> agent.Expect("env", "dump") --- bootstrap/integration/bootstrap_tester.go | 2 +- hook/scriptwrapper_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bootstrap/integration/bootstrap_tester.go b/bootstrap/integration/bootstrap_tester.go index b2ac291ee2..9fead11df7 100644 --- a/bootstrap/integration/bootstrap_tester.go +++ b/bootstrap/integration/bootstrap_tester.go @@ -180,7 +180,7 @@ func (b *BootstrapTester) HasMock(name string) bool { // MockAgent creates a mock for the buildkite-agent binary func (b *BootstrapTester) MockAgent(t *testing.T) *bintest.Mock { agent := b.MustMock(t, "buildkite-agent") - agent.Expect("env"). + agent.Expect("env", "dump"). Min(0). Max(bintest.InfiniteTimes). AndCallFunc(mockEnvAsJSONOnStdout(b)) diff --git a/hook/scriptwrapper_test.go b/hook/scriptwrapper_test.go index e9224ada55..497e4bba16 100644 --- a/hook/scriptwrapper_test.go +++ b/hook/scriptwrapper_test.go @@ -307,7 +307,7 @@ func mockAgent() (*bintest.Mock, func(), error) { return nil, func() {}, err } - agent.Expect("env"). + agent.Expect("env", "dump"). Exactly(2). AndCallFunc(func(c *bintest.Call) { envMap := map[string]string{}