From 4c9fc240edfcadabf140807cbdd418306f7bd202 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 28 May 2023 12:09:16 +0200 Subject: [PATCH] client: use os/exec/Cmd.Environ() instead of os.Environ() Don't set Env if not set; the default is already handled if it's nil; from the documentation: https://pkg.go.dev/os/exec@go1.20.4#Cmd.Env // If Env is nil, the new process uses the current process's // environment. Use `os/exec/Cmd.Environ()` instead of `os.Environ()`, which was added in go1.19, and handles additional environment variables, such as `PWD` on POSIX systems, and `SYSTEMROOT` on Windows. https://pkg.go.dev/os/exec@go1.20.4#Cmd.Environ Also remove a redundant `fmt.Sprintf()`, as we're only concatenating strings. Signed-off-by: Sebastiaan van Stijn --- client/command.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/client/command.go b/client/command.go index 8da3343..0794d31 100644 --- a/client/command.go +++ b/client/command.go @@ -1,7 +1,6 @@ package client import ( - "fmt" "io" "os" "os/exec" @@ -30,10 +29,9 @@ func NewShellProgramFuncWithEnv(name string, env *map[string]string) ProgramFunc func createProgramCmdRedirectErr(commandName string, args []string, env *map[string]string) *exec.Cmd { programCmd := exec.Command(commandName, args...) - programCmd.Env = os.Environ() if env != nil { for k, v := range *env { - programCmd.Env = append(programCmd.Env, fmt.Sprintf("%s=%s", k, v)) + programCmd.Env = append(programCmd.Environ(), k+"="+v) } } programCmd.Stderr = os.Stderr