Skip to content

Commit

Permalink
Merge pull request #8 from stevanmilic/main
Browse files Browse the repository at this point in the history
feat: return subcommand's exit code on execute (#7)
  • Loading branch information
roerohan authored Sep 24, 2023
2 parents 80f9689 + ed4a7b8 commit 3d4b9e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ import (
"os/exec"
)

// Execute is used to run a command and print
// the value in stdout and stderr
func Execute(command []string) {
// Execute is used to run a command and print the value in stdout and stderr.
//
// The return value contains the command's exit code.
func Execute(command []string) int {
cmd := exec.Command(command[0], command[1:]...)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

cmd.Run()
if err := cmd.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
return exitError.ExitCode()
}

return -1
}

return 0
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ func main() {
return
}

Execute(command)
os.Exit(Execute(command))
}

0 comments on commit 3d4b9e5

Please sign in to comment.