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

Use syscall.Exec instead of cmd.Run in non-windows environment #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions exec.go
Original file line number Diff line number Diff line change
@@ -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())
}
14 changes: 14 additions & 0 deletions exec_windows.go
Original file line number Diff line number Diff line change
@@ -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()
}
9 changes: 2 additions & 7 deletions godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"io"
"os"
"os/exec"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down