Skip to content

Commit

Permalink
Merge pull request #48 from yeganemehr/fix-widows-build
Browse files Browse the repository at this point in the history
Fix Windows Build Issue by Splitting Taskify into OS-Specific Files
  • Loading branch information
adhocore authored Oct 27, 2024
2 parents a2aceba + cb088d4 commit 8abd859
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 32 deletions.
32 changes: 0 additions & 32 deletions pkg/tasker/tasker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions pkg/tasker/tasker_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build !windows
// +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
}
}
47 changes: 47 additions & 0 deletions pkg/tasker/tasker_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build windows
// +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
}
}

0 comments on commit 8abd859

Please sign in to comment.