-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from yeganemehr/fix-widows-build
Fix Windows Build Issue by Splitting Taskify into OS-Specific Files
- Loading branch information
Showing
3 changed files
with
91 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |