From b7402b1f6816d28a6e9bbf1291e2482b38c11a96 Mon Sep 17 00:00:00 2001 From: Songmu Date: Fri, 7 Jun 2019 18:55:55 +0900 Subject: [PATCH] Use syscall.Exec instead of cmd.Run in non-windows environment --- exec.go | 19 +++++++++++++++++++ exec_windows.go | 14 ++++++++++++++ godotenv.go | 9 ++------- 3 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 exec.go create mode 100644 exec_windows.go diff --git a/exec.go b/exec.go new file mode 100644 index 0000000..d3d96da --- /dev/null +++ b/exec.go @@ -0,0 +1,19 @@ +//+build !windows + +package godotenv + +import ( + "os" + "os/exec" + "syscall" +) + +func execv(cmd string, cmdArgs []string) error { + prog, err := exec.LookPath(cmd) + if err != nil { + return err + } + args := append([]string{cmd}, cmdArgs...) + + return syscall.Exec(prog, args, os.Environ()) +} diff --git a/exec_windows.go b/exec_windows.go new file mode 100644 index 0000000..350d751 --- /dev/null +++ b/exec_windows.go @@ -0,0 +1,14 @@ +package godotenv + +import ( + "os" + "os/exec" +) + +func execv(cmd string, cmdArgs []string) error { + command := exec.Command(cmd, cmdArgs...) + command.Stdin = os.Stdin + command.Stdout = os.Stdout + command.Stderr = os.Stderr + return command.Run() +} diff --git a/godotenv.go b/godotenv.go index beaa146..b0becf2 100644 --- a/godotenv.go +++ b/godotenv.go @@ -19,7 +19,6 @@ import ( "fmt" "io" "os" - "os/exec" "regexp" "sort" "strings" @@ -138,11 +137,7 @@ func Unmarshal(str string) (envMap map[string]string, err error) { func Exec(filenames []string, cmd string, cmdArgs []string) error { Load(filenames...) - command := exec.Command(cmd, cmdArgs...) - command.Stdin = os.Stdin - command.Stdout = os.Stdout - command.Stderr = os.Stderr - return command.Run() + return execv(cmd, cmdArgs) } // Write serializes the given environment and writes it to a file @@ -258,7 +253,7 @@ func parseLine(line string, envMap map[string]string) (key string, value string, } key = strings.TrimSpace(key) - re := regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`) + re := regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`) key = re.ReplaceAllString(splitString[0], "$1") // Parse the value