From 7edfb5c34d2cc868a629a86324afa212f1569fae Mon Sep 17 00:00:00 2001 From: Aleksander Cynarski Date: Mon, 6 Dec 2021 17:36:32 +0100 Subject: [PATCH] exit error code fetch from exec.Command --- app/modules/context/context.go | 46 ++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/app/modules/context/context.go b/app/modules/context/context.go index 027738b..dd1ee73 100644 --- a/app/modules/context/context.go +++ b/app/modules/context/context.go @@ -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 { @@ -79,7 +81,27 @@ 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 { @@ -87,5 +109,25 @@ func (lx *LedoContext) ExecCmdSilent(cmd string, cmdArgs []string) error { 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 }