Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle int and float env variable by converting them to string #1641

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading