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

fix: #3370 fixed process parameter parsing failed on Windows for package gproc #3386

Merged
merged 6 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 1 addition & 9 deletions os/gproc/gproc_process.go
Expand Up @@ -12,7 +12,6 @@ import (
"os"
"os/exec"
"runtime"
"strings"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 17 additions & 0 deletions os/gproc/gproc_process_newprocess.go
@@ -0,0 +1,17 @@
//go:build !windows
gqcn marked this conversation as resolved.
Show resolved Hide resolved

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
}
17 changes: 17 additions & 0 deletions os/gproc/gproc_process_windows.go
@@ -0,0 +1,17 @@
//go:build windows
gqcn marked this conversation as resolved.
Show resolved Hide resolved

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
}
36 changes: 1 addition & 35 deletions os/gproc/gproc_shell.go
Expand Up @@ -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`.
Expand Down Expand Up @@ -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.
Expand Down