From 9a770d6eb8627e17f0a9902fc67fbc2cc0852275 Mon Sep 17 00:00:00 2001 From: Yunchi Luo Date: Thu, 29 Jun 2023 15:43:48 -0400 Subject: [PATCH] cleanups --- server/core/config/raw/step.go | 2 +- server/core/config/raw/step_test.go | 22 ++++++++++----------- server/core/config/valid/repo_cfg.go | 1 + server/core/runtime/env_step_runner.go | 3 ++- server/core/runtime/multienv_step_runner.go | 3 ++- server/core/runtime/run_step_runner_test.go | 3 ++- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/server/core/config/raw/step.go b/server/core/config/raw/step.go index b5fb2c3a1f..de69abc3dc 100644 --- a/server/core/config/raw/step.go +++ b/server/core/config/raw/step.go @@ -215,7 +215,7 @@ func (s Step) Validate() error { } // Sort so tests can be deterministic. sort.Strings(argKeys) - return fmt.Errorf("env steps only support keys %q, %q and %q, found extra keys %q", RunStepName, CommandArgKey, OutputArgKey, strings.Join(argKeys, ",")) + return fmt.Errorf("run steps only support keys %q, %q and %q, found extra keys %q", RunStepName, CommandArgKey, OutputArgKey, strings.Join(argKeys, ",")) } default: return fmt.Errorf("%q is not a valid step type", stepName) diff --git a/server/core/config/raw/step_test.go b/server/core/config/raw/step_test.go index 98415e66f9..215169d68c 100644 --- a/server/core/config/raw/step_test.go +++ b/server/core/config/raw/step_test.go @@ -81,7 +81,7 @@ env: value: direct_value name: test`, exp: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "env": { "value": "direct_value", "name": "test", @@ -96,7 +96,7 @@ env: command: echo 123 name: test`, exp: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "env": { "command": "echo 123", "name": "test", @@ -227,7 +227,7 @@ func TestStep_Validate(t *testing.T) { { description: "env", input: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "env": { "name": "test", "command": "echo 123", @@ -283,7 +283,7 @@ func TestStep_Validate(t *testing.T) { { description: "multiple keys in env", input: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "key1": nil, "key2": nil, }, @@ -312,7 +312,7 @@ func TestStep_Validate(t *testing.T) { { description: "invalid key in env", input: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "invalid": nil, }, }, @@ -353,7 +353,7 @@ func TestStep_Validate(t *testing.T) { { description: "env step with no name key set", input: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "env": { "value": "value", }, @@ -364,7 +364,7 @@ func TestStep_Validate(t *testing.T) { { description: "env step with invalid key", input: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "env": { "abc": "", "invalid2": "", @@ -376,7 +376,7 @@ func TestStep_Validate(t *testing.T) { { description: "env step with both command and value set", input: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "env": { "name": "name", "command": "command", @@ -454,7 +454,7 @@ func TestStep_ToValid(t *testing.T) { { description: "env step", input: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "env": { "name": "test", "command": "echo 123", @@ -561,7 +561,7 @@ func TestStep_ToValid(t *testing.T) { { description: "run step with output", input: raw.Step{ - EnvOrRun: EnvType{ + EnvOrRun: EnvOrRunType{ "run": { "command": "my 'run command'", "output": "hide", @@ -583,4 +583,4 @@ func TestStep_ToValid(t *testing.T) { } type MapType map[string]map[string][]string -type EnvType map[string]map[string]string +type EnvOrRunType map[string]map[string]string diff --git a/server/core/config/valid/repo_cfg.go b/server/core/config/valid/repo_cfg.go index 5a37191a01..ec55773646 100644 --- a/server/core/config/valid/repo_cfg.go +++ b/server/core/config/valid/repo_cfg.go @@ -150,6 +150,7 @@ type Autoplan struct { Enabled bool } +// PostProcessRunOutputOption is an enum of options for post-processing RunCommand output type PostProcessRunOutputOption string const ( diff --git a/server/core/runtime/env_step_runner.go b/server/core/runtime/env_step_runner.go index a9639409aa..eb6556c182 100644 --- a/server/core/runtime/env_step_runner.go +++ b/server/core/runtime/env_step_runner.go @@ -3,6 +3,7 @@ package runtime import ( "strings" + "github.com/runatlantis/atlantis/server/core/config/valid" "github.com/runatlantis/atlantis/server/events/command" ) @@ -20,7 +21,7 @@ func (r *EnvStepRunner) Run(ctx command.ProjectContext, command string, value st } // Pass `false` for streamOutput because this isn't interesting to the user reading the build logs // in the web UI. - res, err := r.RunStepRunner.Run(ctx, command, path, envs, false, "") + res, err := r.RunStepRunner.Run(ctx, command, path, envs, false, valid.PostProcessRunOutputShow) // Trim newline from res to support running `echo env_value` which has // a newline. We don't recommend users run echo -n env_value to remove the // newline because -n doesn't work in the sh shell which is what we use diff --git a/server/core/runtime/multienv_step_runner.go b/server/core/runtime/multienv_step_runner.go index 8774a5e23a..be4ac7ee43 100644 --- a/server/core/runtime/multienv_step_runner.go +++ b/server/core/runtime/multienv_step_runner.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/runatlantis/atlantis/server/core/config/valid" "github.com/runatlantis/atlantis/server/events/command" ) @@ -15,7 +16,7 @@ type MultiEnvStepRunner struct { // Run runs the multienv step command. // The command must return a json string containing the array of name-value pairs that are being added as extra environment variables func (r *MultiEnvStepRunner) Run(ctx command.ProjectContext, command string, path string, envs map[string]string) (string, error) { - res, err := r.RunStepRunner.Run(ctx, command, path, envs, false, "") + res, err := r.RunStepRunner.Run(ctx, command, path, envs, false, valid.PostProcessRunOutputShow) if err == nil { if len(res) > 0 { var sb strings.Builder diff --git a/server/core/runtime/run_step_runner_test.go b/server/core/runtime/run_step_runner_test.go index da45422824..d011254a09 100644 --- a/server/core/runtime/run_step_runner_test.go +++ b/server/core/runtime/run_step_runner_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-version" . "github.com/petergtz/pegomock/v4" + "github.com/runatlantis/atlantis/server/core/config/valid" "github.com/runatlantis/atlantis/server/core/runtime" "github.com/runatlantis/atlantis/server/core/terraform/mocks" "github.com/runatlantis/atlantis/server/events/command" @@ -144,7 +145,7 @@ func TestRunStepRunner_Run(t *testing.T) { ProjectName: c.ProjectName, EscapedCommentArgs: []string{"-target=resource1", "-target=resource2"}, } - out, err := r.Run(ctx, c.Command, tmpDir, map[string]string{"test": "var"}, true, "") + out, err := r.Run(ctx, c.Command, tmpDir, map[string]string{"test": "var"}, true, valid.PostProcessRunOutputShow) if c.ExpErr != "" { ErrContains(t, c.ExpErr, err) return