Skip to content

Commit

Permalink
exit error code fetch from exec.Command
Browse files Browse the repository at this point in the history
  • Loading branch information
paramah committed Dec 6, 2021
1 parent 36d4292 commit 7edfb5c
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions app/modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"github.com/urfave/cli/v2"
"ledo/app/modules/config"
"ledo/app/modules/mode"
"log"
"os"
"os/exec"
"strings"
"syscall"
)

type LedoContext struct {
Expand Down Expand Up @@ -79,13 +81,53 @@ func (lx *LedoContext) ExecCmd(cmd string, cmdArgs []string) error {
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
return command.Run()

if err := command.Start(); err != nil {
log.Fatalf("cmd.Start: %v", err)
os.Exit(1)
return err
}

if err := command.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
log.Printf("Exit Status: %d", status.ExitStatus())
os.Exit(status.ExitStatus())
return err
}
} else {
log.Fatalf("cmd.Wait: %v", err)
os.Exit(1)
return err
}
}
return nil
}

func (lx *LedoContext) ExecCmdSilent(cmd string, cmdArgs []string) error {
command := exec.Command(cmd, cmdArgs...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = nil
return command.Run()

if err := command.Start(); err != nil {
log.Fatalf("cmd.Start: %v", err)
os.Exit(1)
return err
}

if err := command.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
log.Printf("Exit Status: %d", status.ExitStatus())
os.Exit(status.ExitStatus())
return err
}
} else {
log.Fatalf("cmd.Wait: %v", err)
os.Exit(1)
return err
}
}
return nil
}

0 comments on commit 7edfb5c

Please sign in to comment.