Skip to content

Commit

Permalink
fix: handle int and float env variable by converting them to string (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaerten authored May 9, 2024
1 parent 78a69c4 commit 3397f28
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
16 changes: 11 additions & 5 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ func Get(t *ast.Task) []string {
}

environ := os.Environ()

for k, v := range t.Env.ToCacheMap() {
str, isString := v.(string)
if !isString {
if !isTypeAllowed(v) {
continue
}

if _, alreadySet := os.LookupEnv(k); alreadySet {
continue
}

environ = append(environ, fmt.Sprintf("%s=%s", k, str))
environ = append(environ, fmt.Sprintf("%s=%v", k, v))
}

return environ
}

func isTypeAllowed(v any) bool {
switch v.(type) {
case string, int, float32, float64:
return true
default:
return false
}
}
5 changes: 3 additions & 2 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ func TestEnv(t *testing.T) {
Target: "default",
TrimSpace: false,
Files: map[string]string{
"local.txt": "GOOS='linux' GOARCH='amd64' CGO_ENABLED='0'\n",
"global.txt": "FOO='foo' BAR='overriden' BAZ='baz'\n",
"local.txt": "GOOS='linux' GOARCH='amd64' CGO_ENABLED='0'\n",
"global.txt": "FOO='foo' BAR='overriden' BAZ='baz'\n",
"multiple_type.txt": "FOO='1' BAR='' BAZ='1.1'\n",
},
}
tt.Run(t)
Expand Down
9 changes: 9 additions & 0 deletions testdata/env/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tasks:
cmds:
- task: local
- task: global
- task: multiple_type

local:
vars:
Expand All @@ -31,3 +32,11 @@ tasks:
BAR: overriden
cmds:
- echo "FOO='$FOO' BAR='$BAR' BAZ='$BAZ'" > global.txt

multiple_type:
env:
FOO: 1
BAR: true
BAZ: 1.1
cmds:
- echo "FOO='$FOO' BAR='$BAR' BAZ='$BAZ'" > multiple_type.txt

0 comments on commit 3397f28

Please sign in to comment.