From 2c144f84a948e3af1fc2a4105b1941c88c5c1d4c Mon Sep 17 00:00:00 2001 From: "Amir H. Yeganemehr" Date: Sat, 26 Oct 2024 19:49:15 +0330 Subject: [PATCH 1/2] use windows-specific CREATE_NEW_PROCESS_GROUP flag in tasker.Taskify --- pkg/tasker/tasker.go | 32 ------------------------- pkg/tasker/tasker_other.go | 43 +++++++++++++++++++++++++++++++++ pkg/tasker/tasker_windows.go | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 32 deletions(-) create mode 100644 pkg/tasker/tasker_other.go create mode 100644 pkg/tasker/tasker_windows.go diff --git a/pkg/tasker/tasker.go b/pkg/tasker/tasker.go index c6de655..e514db6 100644 --- a/pkg/tasker/tasker.go +++ b/pkg/tasker/tasker.go @@ -116,38 +116,6 @@ func (t *Tasker) WithContext(ctx context.Context) *Tasker { return t } -// Taskify creates TaskFunc out of plain command wrt given options. -func (t *Tasker) Taskify(cmd string, opt Option) TaskFunc { - sh := Shell(opt.Shell) - - return func(ctx context.Context) (int, error) { - buf := strings.Builder{} - exc := exec.Command(sh[0], sh[1], cmd) - exc.Stderr = &buf - exc.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} - - if t.Log.Writer() != exc.Stderr { - exc.Stdout = t.Log.Writer() - } - - err := exc.Run() - if err == nil { - return 0, nil - } - - for _, ln := range strings.Split(strings.TrimRight(buf.String(), "\r\n"), "\n") { - log.Println(ln) - } - - code := 1 - if exErr, ok := err.(*exec.ExitError); ok { - code = exErr.ExitCode() - } - - return code, err - } -} - // Shell gives a pair of shell and arg. // It returns array of string. func Shell(shell ...string) []string { diff --git a/pkg/tasker/tasker_other.go b/pkg/tasker/tasker_other.go new file mode 100644 index 0000000..a5bc301 --- /dev/null +++ b/pkg/tasker/tasker_other.go @@ -0,0 +1,43 @@ +//go:build !windows + +package tasker + +import ( + "context" + "log" + "os/exec" + "strings" + "syscall" +) + +// Taskify creates TaskFunc out of plain command wrt given options. +func (t *Tasker) Taskify(cmd string, opt Option) TaskFunc { + sh := Shell(opt.Shell) + + return func(ctx context.Context) (int, error) { + buf := strings.Builder{} + exc := exec.Command(sh[0], sh[1], cmd) + exc.Stderr = &buf + exc.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + + if t.Log.Writer() != exc.Stderr { + exc.Stdout = t.Log.Writer() + } + + err := exc.Run() + if err == nil { + return 0, nil + } + + for _, ln := range strings.Split(strings.TrimRight(buf.String(), "\r\n"), "\n") { + log.Println(ln) + } + + code := 1 + if exErr, ok := err.(*exec.ExitError); ok { + code = exErr.ExitCode() + } + + return code, err + } +} diff --git a/pkg/tasker/tasker_windows.go b/pkg/tasker/tasker_windows.go new file mode 100644 index 0000000..dbb07f6 --- /dev/null +++ b/pkg/tasker/tasker_windows.go @@ -0,0 +1,46 @@ +//go:build windows + +package tasker + +import ( + "context" + "log" + "os/exec" + "strings" + "syscall" +) + +// Taskify creates TaskFunc out of plain command wrt given options. + +func (t *Tasker) Taskify(cmd string, opt Option) TaskFunc { + sh := Shell(opt.Shell) + + return func(ctx context.Context) (int, error) { + buf := strings.Builder{} + exc := exec.Command(sh[0], sh[1], cmd) + exc.Stderr = &buf + exc.SysProcAttr = &syscall.SysProcAttr{ + CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP, + } + + if t.Log.Writer() != exc.Stderr { + exc.Stdout = t.Log.Writer() + } + + err := exc.Run() + if err == nil { + return 0, nil + } + + for _, ln := range strings.Split(strings.TrimRight(buf.String(), "\r\n"), "\n") { + log.Println(ln) + } + + code := 1 + if exErr, ok := err.(*exec.ExitError); ok { + code = exErr.ExitCode() + } + + return code, err + } +} From cb088d4f20c3591fb406425864125467f14464e3 Mon Sep 17 00:00:00 2001 From: "Amir H. Yeganemehr" Date: Sun, 27 Oct 2024 11:47:20 +0330 Subject: [PATCH 2/2] Fix build constraints for Go 1.16 --- pkg/tasker/tasker_other.go | 1 + pkg/tasker/tasker_windows.go | 1 + 2 files changed, 2 insertions(+) diff --git a/pkg/tasker/tasker_other.go b/pkg/tasker/tasker_other.go index a5bc301..316b1cb 100644 --- a/pkg/tasker/tasker_other.go +++ b/pkg/tasker/tasker_other.go @@ -1,4 +1,5 @@ //go:build !windows +// +build !windows package tasker diff --git a/pkg/tasker/tasker_windows.go b/pkg/tasker/tasker_windows.go index dbb07f6..a8c5b05 100644 --- a/pkg/tasker/tasker_windows.go +++ b/pkg/tasker/tasker_windows.go @@ -1,4 +1,5 @@ //go:build windows +// +build windows package tasker