From 93c73005c65e2e6a55c0c6b7ad3cba36bf4fc0f6 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 30 Jul 2020 20:21:04 +0300 Subject: [PATCH] more aboutt #1 --- go.mod | 16 ++++++++-------- utils/cmd_unix.go | 40 +++++++++++++++++++++++----------------- utils/cmd_windows.go | 2 +- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 92fe6d1..02fda00 100644 --- a/go.mod +++ b/go.mod @@ -5,16 +5,16 @@ go 1.13 replace github.com/AlecAivazis/survey/v2 => github.com/kataras/survey/v2 v2.0.6 require ( - github.com/AlecAivazis/survey/v2 v2.0.7 + github.com/AlecAivazis/survey/v2 v2.0.8 github.com/cheggaaa/pb/v3 v3.0.4 - github.com/creack/pty v1.1.9 + github.com/creack/pty v1.1.11 github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 - github.com/fsnotify/fsnotify v1.4.7 - github.com/kataras/golog v0.0.10 - github.com/kataras/neffos v0.0.12 - github.com/spf13/cobra v0.0.5 - golang.org/x/sync v0.0.0-20190423024810-112230192c58 + github.com/fsnotify/fsnotify v1.4.9 + github.com/kataras/golog v0.0.18 + github.com/kataras/neffos v0.0.16 + github.com/spf13/cobra v1.0.0 + golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 gopkg.in/src-d/go-git.v4 v4.13.1 - gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2 + gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 ) diff --git a/utils/cmd_unix.go b/utils/cmd_unix.go index 8b91016..9c08f19 100644 --- a/utils/cmd_unix.go +++ b/utils/cmd_unix.go @@ -6,6 +6,7 @@ import ( "context" "io" "os/exec" + "path" "strings" "syscall" @@ -35,27 +36,32 @@ func KillCommand(cmd *exec.Cmd) error { func FormatExecutable(bin string) string { return bin } func StartExecutable(dir, bin string, stdout, stderr io.Writer) (*exec.Cmd, error) { - cmd := Command("/bin/sh", "-c", bin) - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} // set parent group id in order to be kill-able. - cmd.Dir = dir - cmd.Stdout = stdout - cmd.Stderr = stderr - _, err := pty.Start(cmd) // it runs cmd.Start(). - if err != nil { - // fork/exec /bin/sh: operation not permitted - if !strings.Contains(err.Error(), "operation not permitted") { - return nil, err - } - - cmd = Command(bin) - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + if IsInsideDocker() { + // If run through docker, this part is required, + // otherwise we should NOT try this because it always gives error: + cmd := Command("/bin/sh", "-c", bin) + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} // set parent group id in order to be kill-able. cmd.Dir = dir cmd.Stdout = stdout cmd.Stderr = stderr - if err = cmd.Start(); err != nil { - return nil, err + _, err := pty.Start(cmd) + // fork/exec /bin/sh: operation not permitted, even without setpgid. + + if err != nil { + if !strings.Contains(err.Error(), "operation not permitted") { + return nil, err + } } } - return cmd, err + cmd := Command(path.Join(dir, bin)) + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + cmd.Dir = dir + cmd.Stdout = stdout + cmd.Stderr = stderr + if err := cmd.Start(); err != nil { + return nil, err + } + + return cmd, nil } diff --git a/utils/cmd_windows.go b/utils/cmd_windows.go index 4eedd30..3a9f52c 100644 --- a/utils/cmd_windows.go +++ b/utils/cmd_windows.go @@ -44,7 +44,7 @@ func KillCommand(cmd *exec.Cmd) error { } func FormatExecutable(bin string) string { - if ext := ".exe"; !strings.HasPrefix(bin, ext) { + if ext := ".exe"; !strings.HasSuffix(bin, ext) { bin += ext }