Skip to content

Commit

Permalink
Ignore SIGINT while running child process (#58)
Browse files Browse the repository at this point in the history
* Ignore SIGINT while running child process

* Add M1 macOS to Makefile
  • Loading branch information
CGamesPlay committed Oct 19, 2022
1 parent 09b63c8 commit 946c7a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
test:
@go test -cover ./...

.PHONY: dist
dist:
@gox \
--osarch "!darwin/386" \
--osarch "darwin/amd64 darwin/arm64 linux/amd64 linux/arm" \
--output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}"
rm dist/*.gz
ls dist/* | while read i; do gzip $$i; done

clean:
rm -fr dist
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/tj/robo/config"
)

var version = "0.7.0"
var version = "0.7.0-cgamesplay"

const usage = `
Usage:
Expand Down
16 changes: 14 additions & 2 deletions task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
Expand Down Expand Up @@ -89,6 +90,17 @@ func (r *Runnable) Run(lookupPath string, args []string, env []string) error {
return fmt.Errorf("nothing to run (add script, command, or exec key)")
}

func (r *Runnable) runInternal(cmd *exec.Cmd) error {
if err := cmd.Start(); err != nil {
return err
}
// Ignore SIGINT while waiting for a child process. The child process will
// receive the signal and abort normally, then we will continue.
signal.Ignore(os.Interrupt);
defer signal.Reset(os.Interrupt);
return cmd.Wait();
}

// RunScript runs the target shell `script` file.
func (r *Runnable) RunScript(lookupPath string, args []string, env []string) error {
var path = r.Script
Expand All @@ -114,7 +126,7 @@ func (r *Runnable) RunScript(lookupPath string, args []string, env []string) err
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
return r.runInternal(cmd)
}

// RunCommand runs the `command` via the shell.
Expand All @@ -125,7 +137,7 @@ func (r *Runnable) RunCommand(args []string, env []string) error {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
return r.runInternal(cmd)
}

// RunExec runs the `exec` command.
Expand Down

0 comments on commit 946c7a8

Please sign in to comment.