Skip to content

Commit 8828639

Browse files
committed
Use built-in function to read env vars from current shell
1 parent 091d905 commit 8828639

File tree

2 files changed

+3
-13
lines changed

2 files changed

+3
-13
lines changed

command.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"os/exec"
99
"regexp"
10-
"strings"
1110
"syscall"
1211
"time"
1312
)
@@ -93,19 +92,10 @@ func WithoutTimeout(c *Command) {
9392
// AddEnv adds an environment variable to the command
9493
// If a variable gets passed like ${VAR_NAME} the env variable will be read out by the current shell
9594
func (c *Command) AddEnv(key string, value string) {
96-
vars := parseEnvVariableFromShell(value)
97-
for _, v := range vars {
98-
value = strings.Replace(value, v, os.Getenv(removeEnvVarSyntax(v)), -1)
99-
}
100-
95+
value = os.ExpandEnv(value)
10196
c.Env = append(c.Env, fmt.Sprintf("%s=%s", key, value))
10297
}
10398

104-
// Removes the ${...} characters at the beginng and end of the given string
105-
func removeEnvVarSyntax(v string) string {
106-
return v[2:(len(v) - 1)]
107-
}
108-
10999
//Read all environment variables from the given value
110100
//with the syntax ${VAR_NAME}
111101
func parseEnvVariableFromShell(val string) []string {

command_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func TestCommand_AddEnvWithShellVariable(t *testing.T) {
8181
}
8282

8383
func TestCommand_AddMultipleEnvWithShellVariable(t *testing.T) {
84-
const TestEnvKeyPlanet = "COMMANDER_TEST_PLANET"
85-
const TestEnvKeyName = "COMMANDER_TEST_NAME"
84+
const TestEnvKeyPlanet = "CMD_TEST_PLANET"
85+
const TestEnvKeyName = "CMD_TEST_NAME"
8686
os.Setenv(TestEnvKeyPlanet, "world")
8787
os.Setenv(TestEnvKeyName, "Simon")
8888
defer func() {

0 commit comments

Comments
 (0)