From ffedfb7c0c9cbc176c184658aa7bee9c1a077503 Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 14 Sep 2021 20:25:23 +0800 Subject: [PATCH] improve package gproc for customize environment variables when creating process --- os/gproc/gproc.go | 22 +++++++++++----------- os/gproc/gproc_process.go | 4 +--- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/os/gproc/gproc.go b/os/gproc/gproc.go index 33fe32f00..4fc36b25f 100644 --- a/os/gproc/gproc.go +++ b/os/gproc/gproc.go @@ -80,9 +80,9 @@ func Uptime() time.Duration { return time.Now().Sub(processStartTime) } -// Shell executes command synchronizingly with given input pipe and output pipe . -// The command reads the input parameters from input pipe , and writes its output automatically -// to output pipe . +// Shell executes command synchronously with given input pipe and output pipe `out`. +// The command reads the input parameters from input pipe `in`, and writes its output automatically +// to output pipe `out`. func Shell(cmd string, out io.Writer, in io.Reader) error { p := NewProcess(getShell(), append([]string{getShellOption()}, parseCommand(cmd)...)) p.Stdin = in @@ -90,13 +90,13 @@ func Shell(cmd string, out io.Writer, in io.Reader) error { return p.Run() } -// ShellRun executes given command synchronizingly and outputs the command result to the stdout. +// ShellRun executes given command `cmd` synchronously and outputs the command result to the stdout. func ShellRun(cmd string) error { p := NewProcess(getShell(), append([]string{getShellOption()}, parseCommand(cmd)...)) return p.Run() } -// ShellExec executes given command synchronizingly and returns the command result. +// ShellExec executes given command `cmd` synchronously and returns the command result. func ShellExec(cmd string, environment ...[]string) (string, error) { buf := bytes.NewBuffer(nil) p := NewProcess(getShell(), append([]string{getShellOption()}, parseCommand(cmd)...), environment...) @@ -106,10 +106,10 @@ func ShellExec(cmd string, environment ...[]string) (string, error) { return buf.String(), err } -// parseCommand parses command into slice arguments. +// parseCommand parses command `cmd` into slice arguments. // -// Note that it just parses the for "cmd.exe" binary in windows, but it is not necessary -// parsing the for other systems using "bash"/"sh" binary. +// Note that it just parses the `cmd` for "cmd.exe" binary in windows, but it is not necessary +// parsing the `cmd` for other systems using "bash"/"sh" binary. func parseCommand(cmd string) (args []string) { if runtime.GOOS != "windows" { return []string{cmd} @@ -170,7 +170,7 @@ func getShell() string { } } -// getShellOption returns the shell option depending on current working operation system. +// getShellOption returns the shell option depending on current working operating system. // It returns "/c" for windows, and "-c" for others. func getShellOption() string { switch runtime.GOOS { @@ -181,7 +181,7 @@ func getShellOption() string { } } -// SearchBinary searches the binary in current working folder and PATH environment. +// SearchBinary searches the binary `file` in current working folder and PATH environment. func SearchBinary(file string) string { // Check if it's absolute path of exists at current working directory. if gfile.Exists(file) { @@ -190,7 +190,7 @@ func SearchBinary(file string) string { return SearchBinaryPath(file) } -// SearchBinaryPath searches the binary in PATH environment. +// SearchBinaryPath searches the binary `file` in PATH environment. func SearchBinaryPath(file string) string { array := ([]string)(nil) switch runtime.GOOS { diff --git a/os/gproc/gproc_process.go b/os/gproc/gproc_process.go index a560c0eb8..51249e5d6 100644 --- a/os/gproc/gproc_process.go +++ b/os/gproc/gproc_process.go @@ -29,9 +29,7 @@ type Process struct { func NewProcess(path string, args []string, environment ...[]string) *Process { env := os.Environ() if len(environment) > 0 { - for k, v := range environment[0] { - env[k] = v - } + env = append(env, environment[0]...) } process := &Process{ Manager: nil,