Skip to content

Commit

Permalink
capture non-zero exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
verzac committed Jan 29, 2024
1 parent bb03634 commit fb89f31
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ func NewRunCmd() *cobra.Command {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

defer func() {
os.Exit(cmd.ProcessState.ExitCode())
}()

m := marker.NewNotificationMarker(cmd)
defer m.Done()

err := cmd.Run()
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
os.Exit(1)
fmt.Printf("n-cli run error: %s\n", err.Error())
}
},
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/notifier/marker/marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ func NewNotificationMarker(cmd *exec.Cmd) NotificationMarker {
func (m *NotificationMarkerImpl) Done() {
elapsed := time.Since(m.StartedFrom)
// elapsedMs := elapsed.Milliseconds()
prettyCommand := strings.Join(m.Command.Args, " ")

if m.Command.ProcessState.ExitCode() < 0 {
exitCode := m.Command.ProcessState.ExitCode()
msg := ""
if exitCode < 0 {
return
} else if exitCode == 0 {
msg = fmt.Sprintf("Command `%s` COMPLETE. Elapsed: %s", prettyCommand, elapsed.String())
} else {
msg = fmt.Sprintf("Command `%s` FAILED. Status=%d. Elapsed: %s", prettyCommand, exitCode, elapsed.String())
}

msg := fmt.Sprintf("Command `%s` is complete. Elapsed: %s", strings.Join(m.Command.Args, " "), elapsed.String())
err := notifier.Notify(msg)
if err != nil {
fmt.Printf("Error encountered when sending notification: %s\n", err.Error())
Expand Down

0 comments on commit fb89f31

Please sign in to comment.