From f83dd6b9902c8ea6da3ee921ef642620e265245d Mon Sep 17 00:00:00 2001 From: "m.kindritskiy" Date: Tue, 25 Jun 2024 10:09:28 +0300 Subject: [PATCH] fix #235 - do not fail if env sh declared but not defined --- config/config/env.go | 40 ++++++++++++++++++++++++++++++++++---- executor/executor.go | 4 ++-- tests/global_env.bats | 13 +++++++------ tests/global_env/lets.yaml | 19 ++++++++++-------- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/config/config/env.go b/config/config/env.go index e64cc61c..63e54b6e 100644 --- a/config/config/env.go +++ b/config/config/env.go @@ -36,12 +36,44 @@ func (e *Envs) UnmarshalYAML(node *yaml.Node) error { keyNode := node.Content[i] valueNode := node.Content[i+1] - var env Env - if err := valueNode.Decode(&env); err != nil { + envAsStr := "" + + if err := valueNode.Decode(&envAsStr); err == nil { + e.Set(keyNode.Value, Env{Name: keyNode.Value, Value: envAsStr}) + continue + } + + envAsMap := struct { + Sh *string + Checksum *Checksum + }{} + + if err := valueNode.Decode(&envAsMap); err != nil { return err } - env.Name = keyNode.Value - e.Set(keyNode.Value, env) + + env := Env{ + Name: keyNode.Value, + Value: "", + Sh: "", + Checksum: Checksum{}, + } + + if envAsMap.Sh == nil && envAsMap.Checksum == nil { + return fmt.Errorf("lets: environment variable '%s' must have value or 'sh' or 'checksum'", keyNode.Value) + } + + if envAsMap.Sh != nil && envAsMap.Checksum != nil { + return fmt.Errorf("lets: environment variable '%s' must have only 'sh' or 'checksum'", keyNode.Value) + } + + if envAsMap.Sh != nil { + env.Sh = *envAsMap.Sh + } else if envAsMap.Checksum != nil { + env.Checksum = *envAsMap.Checksum + } + + e.Set(env.Name, env) } return nil diff --git a/executor/executor.go b/executor/executor.go index 61248ef3..41a3bf4c 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -37,8 +37,8 @@ func (e *ExecuteError) ExitCode() int { } type Executor struct { - cfg *config.Config - out io.Writer + cfg *config.Config + out io.Writer initCalled bool } diff --git a/tests/global_env.bats b/tests/global_env.bats index 937a911c..f7495bc7 100644 --- a/tests/global_env.bats +++ b/tests/global_env.bats @@ -9,10 +9,11 @@ setup() { @test "global_env: should provide env to command" { run lets global-env assert_success - assert_line --index 0 "ONE=1" - assert_line --index 1 "TWO=two" - assert_line --index 2 "THREE=3" - assert_line --index 3 "FOUR=4" - assert_line --index 4 "BAR=Bar" - assert_line --index 5 "FOO=bb1da47569d9fbe3b5f2216fdbd4c9b040ccb5c1" + assert_line --index 0 "INT=1" + assert_line --index 1 "STR=hi" + assert_line --index 2 "STR_INT=1" + assert_line --index 3 "BOOL=true" + assert_line --index 4 "ORIGINAL=b" + assert_line --index 5 "BAR=Bar" + assert_line --index 6 "FOO=bb1da47569d9fbe3b5f2216fdbd4c9b040ccb5c1" } diff --git a/tests/global_env/lets.yaml b/tests/global_env/lets.yaml index bc32e376..e3de2e10 100644 --- a/tests/global_env/lets.yaml +++ b/tests/global_env/lets.yaml @@ -1,9 +1,11 @@ shell: bash env: - ONE: 1 - TWO: two - THREE: "3" + INT: 1 + STR: "hi" + STR_INT: "1" + BOOL: true + ORIGINAL: "a" BAR: sh: echo Bar FOO: @@ -13,11 +15,12 @@ commands: global-env: description: Test global env env: - FOUR: "4" + ORIGINAL: "b" cmd: | - echo ONE=${ONE} - echo TWO=${TWO} - echo THREE=${THREE} - echo FOUR=${FOUR} + echo INT=${INT} + echo STR=${STR} + echo STR_INT=${STR_INT} + echo BOOL=${BOOL} + echo ORIGINAL=${ORIGINAL} echo BAR=${BAR} echo FOO=${FOO}