Skip to content

Commit

Permalink
fix: gogf#3370 fixed process parameter parsing failed on Windows for …
Browse files Browse the repository at this point in the history
…package `gproc` (gogf#3386)
  • Loading branch information
wln32 authored Mar 20, 2024
1 parent b3f4821 commit 040118b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 44 deletions.
10 changes: 1 addition & 9 deletions os/gproc/gproc_process.go
Original file line number Diff line number Diff line change
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
23 changes: 23 additions & 0 deletions os/gproc/gproc_process_newprocess.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions os/gproc/gproc_process_newprocess_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 1 addition & 35 deletions os/gproc/gproc_shell.go
Original file line number Diff line number Diff line change
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

0 comments on commit 040118b

Please sign in to comment.