From 6467d366aae8a461a4233435f4786294ab8f62e5 Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 29 Jan 2024 15:00:19 +0100 Subject: [PATCH] Split run function into separate files for OS builds. --- cmd/util.go | 21 --------------------- cmd/util_unix.go | 25 +++++++++++++++++++++++++ cmd/util_windows.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 cmd/util_unix.go create mode 100644 cmd/util_windows.go diff --git a/cmd/util.go b/cmd/util.go index ed9f0e7..25a2f25 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -3,12 +3,8 @@ package cmd import ( "bytes" "fmt" - "io" "os" - "os/exec" - "runtime" "strings" - "syscall" "github.com/fatih/color" "github.com/knqyf263/pet/config" @@ -21,23 +17,6 @@ func editFile(command, file string) error { return run(command, os.Stdin, os.Stdout) } -func run(command string, r io.Reader, w io.Writer) error { - var cmd *exec.Cmd - if len(config.Conf.General.Cmd) > 0 { - line := append(config.Conf.General.Cmd, command) - cmd = exec.Command(line[0], line[1:]...) - } else if runtime.GOOS == "windows" { - cmd = exec.Command("cmd.exe") - cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf("/c \"%s\"", command)} - } else { - cmd = exec.Command("sh", "-c", command) - } - cmd.Stderr = os.Stderr - cmd.Stdout = w - cmd.Stdin = r - return cmd.Run() -} - func filter(options []string, tag string) (commands []string, err error) { var snippets snippet.Snippets if err := snippets.Load(); err != nil { diff --git a/cmd/util_unix.go b/cmd/util_unix.go new file mode 100644 index 0000000..91ad71f --- /dev/null +++ b/cmd/util_unix.go @@ -0,0 +1,25 @@ +//go:build (darwin && cgo) || linux + +package cmd + +import ( + "io" + "os" + "os/exec" + + "github.com/knqyf263/pet/config" +) + +func run(command string, r io.Reader, w io.Writer) error { + var cmd *exec.Cmd + if len(config.Conf.General.Cmd) > 0 { + line := append(config.Conf.General.Cmd, command) + cmd = exec.Command(line[0], line[1:]...) + } else { + cmd = exec.Command("sh", "-c", command) + } + cmd.Stderr = os.Stderr + cmd.Stdout = w + cmd.Stdin = r + return cmd.Run() +} diff --git a/cmd/util_windows.go b/cmd/util_windows.go new file mode 100644 index 0000000..2827fbb --- /dev/null +++ b/cmd/util_windows.go @@ -0,0 +1,28 @@ +//go:build windows + +package cmd + +import ( + "fmt" + "io" + "os" + "os/exec" + "syscall" + + "github.com/knqyf263/pet/config" +) + +func run(command string, r io.Reader, w io.Writer) error { + var cmd *exec.Cmd + if len(config.Conf.General.Cmd) > 0 { + line := append(config.Conf.General.Cmd, command) + cmd = exec.Command(line[0], line[1:]...) + } else { + cmd = exec.Command("cmd.exe") + cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf("/c \"%s\"", command)} + } + cmd.Stderr = os.Stderr + cmd.Stdout = w + cmd.Stdin = r + return cmd.Run() +}