Skip to content

Commit

Permalink
CLI: 'env' command moved to 'env dump' subcommand (#1920)
Browse files Browse the repository at this point in the history
This makes room for other future subcommands that relate to environment.

Co-authored-by: Josh Deprez <[email protected]>
  • Loading branch information
pda and DrJosh9000 committed Jan 18, 2023
1 parent d3e3f4f commit f645535
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bootstrap/integration/bootstrap_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
30 changes: 16 additions & 14 deletions clicommand/env.go → clicommand/env_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,31 @@ 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
parsable by other programs. Used when executing hooks to discover changes
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 {
Expand All @@ -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("", " ")
}

Expand Down
12 changes: 6 additions & 6 deletions hook/scriptwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)

Expand Down
14 changes: 7 additions & 7 deletions hook/scriptwrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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{}
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -114,7 +121,6 @@ func main() {
clicommand.StepUpdateCommand,
},
},
clicommand.EnvCommand,
clicommand.BootstrapCommand,
}

Expand Down

0 comments on commit f645535

Please sign in to comment.