Skip to content

Commit

Permalink
Merge pull request #103 from xushiwei/log
Browse files Browse the repository at this point in the history
gsh: exitCode
  • Loading branch information
xushiwei authored Feb 17, 2024
2 parents 2388d63 + ef040f8 commit 6fe5fd4
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions gsh/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ func (p *App) LastErr() error {
return p.err
}

// ExitCode returns exit code of last command execution. Bash-scripting exit codes:
// 1: Catchall for general errors
// 2: Misuse of shell builtins (according to Bash documentation)
// 126: Command invoked cannot execute
// 127: Command not found
// 128+n: Fatal error signal "n"
// 254: Unknown error(*)
func (p *App) ExitCode() int {
if p.err == nil {
return 0
}
switch e := p.err.(type) {
case *exec.ExitError:
return e.ProcessState.ExitCode()
default:
switch e.Error() {
case "exec: no command":
return 127
case "exec: not started":
return 126
default:
return 254
}
}
}

// Capout captures stdout of doSth() execution and save it to output.
func (p *App) Capout(doSth func()) (string, error) {
var out bytes.Buffer
Expand Down

0 comments on commit 6fe5fd4

Please sign in to comment.