From 9e998f4c73fe0999f72efcaadbb1c0336b29db6e Mon Sep 17 00:00:00 2001 From: Daniel Wu Date: Sun, 17 Sep 2023 22:59:11 -0400 Subject: [PATCH] fix sigwinch error with build constraints --- pkg/exec_bash_cmd_any_os.go | 31 +++++++++++++++++++ ...c_bash_cmd.go => exec_bash_cmd_unix_os.go} | 20 ++---------- pkg/exec_bash_cmd_windows_os.go | 7 +++++ 3 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 pkg/exec_bash_cmd_any_os.go rename pkg/{exec_bash_cmd.go => exec_bash_cmd_unix_os.go} (72%) create mode 100644 pkg/exec_bash_cmd_windows_os.go diff --git a/pkg/exec_bash_cmd_any_os.go b/pkg/exec_bash_cmd_any_os.go new file mode 100644 index 0000000..2afa21a --- /dev/null +++ b/pkg/exec_bash_cmd_any_os.go @@ -0,0 +1,31 @@ +package pkg + +import ( + "bytes" + "fmt" + "io" + "os" + "os/exec" + "strings" + + "github.com/pterm/pterm" +) + +func execBashCmdAny(dir string, name string, arg ...string) string { + // Use this if the pty one doesn't work + bash_cmd := exec.Command(name, arg...) + bash_cmd.Dir = dir + pterm.DefaultHeader.WithFullWidth().Println(strings.Join(bash_cmd.Args, " ")) + + var stdoutBuf, stderrBuf bytes.Buffer + bash_cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf) + bash_cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf) + err := bash_cmd.Run() + if err != nil { + pterm.Error.Println("Error starting cmd: ", err) + return fmt.Sprint(err) + } + + outStr, errStr := string(stdoutBuf.String()), string(stderrBuf.String()) + return fmt.Sprint(outStr, errStr) +} diff --git a/pkg/exec_bash_cmd.go b/pkg/exec_bash_cmd_unix_os.go similarity index 72% rename from pkg/exec_bash_cmd.go rename to pkg/exec_bash_cmd_unix_os.go index 412b320..01f6d53 100644 --- a/pkg/exec_bash_cmd.go +++ b/pkg/exec_bash_cmd_unix_os.go @@ -1,8 +1,9 @@ +//go:build !windows + package pkg import ( "bytes" - "fmt" "io" "os" "os/exec" @@ -17,22 +18,7 @@ import ( func ExecBashCmd(runtime_os string, dir string, name string, arg ...string) string { if runtime_os == "windows" { - // Use this if the pty one doesn't work - bash_cmd := exec.Command(name, arg...) - bash_cmd.Dir = dir - pterm.DefaultHeader.WithFullWidth().Println(strings.Join(bash_cmd.Args, " ")) - - var stdoutBuf, stderrBuf bytes.Buffer - bash_cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf) - bash_cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf) - err := bash_cmd.Run() - if err != nil { - pterm.Error.Println("Error starting cmd: ", err) - return fmt.Sprint(err) - } - - outStr, errStr := string(stdoutBuf.String()), string(stderrBuf.String()) - return fmt.Sprint(outStr, errStr) + execBashCmdAny(dir, name, arg...) } // Code below found in pty examples: https://github.com/creack/pty bash_cmd := exec.Command(name, arg...) diff --git a/pkg/exec_bash_cmd_windows_os.go b/pkg/exec_bash_cmd_windows_os.go new file mode 100644 index 0000000..133e22f --- /dev/null +++ b/pkg/exec_bash_cmd_windows_os.go @@ -0,0 +1,7 @@ +//go:build windows + +package pkg + +func ExecBashCmd(runtime_os string, dir string, name string, arg ...string) string { + execBashCmdAny(dir, name, arg...) +}