diff --git a/os/gproc/gproc_process.go b/os/gproc/gproc_process.go index d7920f74527..0c29fc713cd 100644 --- a/os/gproc/gproc_process.go +++ b/os/gproc/gproc_process.go @@ -12,7 +12,6 @@ import ( "os" "os/exec" "runtime" - "strings" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" @@ -54,14 +53,7 @@ func NewProcess(path string, args []string, environment ...[]string) *Process { }, } process.Dir, _ = os.Getwd() - if len(args) > 0 { - // Exclude of current binary path. - start := 0 - if strings.EqualFold(path, args[0]) { - start = 1 - } - process.Args = append(process.Args, args[start:]...) - } + process = newProcess(process, args, path) return process } diff --git a/os/gproc/gproc_process_newprocess.go b/os/gproc/gproc_process_newprocess.go new file mode 100644 index 00000000000..f7a1a3d5c07 --- /dev/null +++ b/os/gproc/gproc_process_newprocess.go @@ -0,0 +1,23 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +//go:build !windows + +package gproc + +import "strings" + +func newProcess(p *Process, args []string, path string) *Process { + if len(args) > 0 { + // Exclude of current binary path. + start := 0 + if strings.EqualFold(path, args[0]) { + start = 1 + } + p.Args = append(p.Args, args[start:]...) + } + return p +} diff --git a/os/gproc/gproc_process_newprocess_windows.go b/os/gproc/gproc_process_newprocess_windows.go new file mode 100644 index 00000000000..e9eb8fbac30 --- /dev/null +++ b/os/gproc/gproc_process_newprocess_windows.go @@ -0,0 +1,23 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +//go:build windows + +package gproc + +import ( + "syscall" + + "github.com/gogf/gf/v2/text/gstr" +) + +// Because when the underlying parameters are passed in on the Windows platform, +// escape characters will be added, causing some commands to fail. +func newProcess(p *Process, args []string, path string) *Process { + p.SysProcAttr = &syscall.SysProcAttr{} + p.SysProcAttr.CmdLine = path + " " + gstr.Join(args, " ") + return p +} diff --git a/os/gproc/gproc_shell.go b/os/gproc/gproc_shell.go index d39cf402e38..538a733999e 100644 --- a/os/gproc/gproc_shell.go +++ b/os/gproc/gproc_shell.go @@ -17,7 +17,6 @@ import ( "go.opentelemetry.io/otel/propagation" "github.com/gogf/gf/v2/os/gfile" - "github.com/gogf/gf/v2/text/gstr" ) // Shell executes command `cmd` synchronously with given input pipe `in` and output pipe `out`. @@ -64,40 +63,7 @@ func ShellExec(ctx context.Context, cmd string, environment ...[]string) (result // 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} - } - // Just for "cmd.exe" in windows. - var argStr string - var firstChar, prevChar, lastChar1, lastChar2 byte - array := gstr.SplitAndTrim(cmd, " ") - for _, v := range array { - if len(argStr) > 0 { - argStr += " " - } - firstChar = v[0] - lastChar1 = v[len(v)-1] - lastChar2 = 0 - if len(v) > 1 { - lastChar2 = v[len(v)-2] - } - if prevChar == 0 && (firstChar == '"' || firstChar == '\'') { - // It should remove the first quote char. - argStr += v[1:] - prevChar = firstChar - } else if prevChar != 0 && lastChar2 != '\\' && lastChar1 == prevChar { - // It should remove the last quote char. - argStr += v[:len(v)-1] - args = append(args, argStr) - argStr = "" - prevChar = 0 - } else if len(argStr) > 0 { - argStr += v - } else { - args = append(args, v) - } - } - return + return []string{cmd} } // getShell returns the shell command depending on current working operating system.