Skip to content

Commit

Permalink
Fix execution of command with quotes on windows.
Browse files Browse the repository at this point in the history
Commands with quotes can't be handled on windows using standard
exec.Command() calls. Based on the go documentation
https://pkg.go.dev/os/exec#Command the argument handling is converted
to SysProcAttr.
  • Loading branch information
matrixx567 committed Jan 23, 2024
1 parent 4304034 commit 66c2575
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"runtime"
"strings"
"syscall"

"github.com/fatih/color"
"github.com/knqyf263/pet/config"
Expand All @@ -26,7 +27,8 @@ func run(command string, r io.Reader, w io.Writer) error {
line := append(config.Conf.General.Cmd, command)
cmd = exec.Command(line[0], line[1:]...)
} else if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", command)
cmd = exec.Command("cmd.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf("/c \"%s\"", command)}

Check failure on line 31 in cmd/util.go

View workflow job for this annotation

GitHub Actions / Test

unknown field CmdLine in struct literal of type "syscall".SysProcAttr
} else {
cmd = exec.Command("sh", "-c", command)
}
Expand Down

0 comments on commit 66c2575

Please sign in to comment.