From f645535904862c23a1e0af1e0b334d37e7a13f66 Mon Sep 17 00:00:00 2001 From: Paul Annesley Date: Wed, 18 Jan 2023 16:47:21 +1030 Subject: [PATCH] CLI: 'env' command moved to 'env dump' subcommand (#1920) This makes room for other future subcommands that relate to environment. Co-authored-by: Josh Deprez --- bootstrap/integration/bootstrap_tester.go | 2 +- clicommand/{env.go => env_dump.go} | 30 ++++++++++++----------- hook/scriptwrapper.go | 12 ++++----- hook/scriptwrapper_test.go | 14 +++++------ main.go | 8 +++++- 5 files changed, 37 insertions(+), 29 deletions(-) rename clicommand/{env.go => env_dump.go} (60%) 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/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..497e4bba16 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) @@ -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{} diff --git a/main.go b/main.go index adde42fd27..f803e4c27f 100644 --- a/main.go +++ b/main.go @@ -82,6 +82,13 @@ func main() { clicommand.ArtifactShasumCommand, }, }, + { + Name: "env", + Usage: "Process environment subcommands", + 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, }