github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/system/exitcode.go (about)

     1  package system // import "github.com/demonoid81/moby/pkg/system"
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"syscall"
     7  )
     8  
     9  // GetExitCode returns the ExitStatus of the specified error if its type is
    10  // exec.ExitError, returns 0 and an error otherwise.
    11  func GetExitCode(err error) (int, error) {
    12  	exitCode := 0
    13  	if exiterr, ok := err.(*exec.ExitError); ok {
    14  		if procExit, ok := exiterr.Sys().(syscall.WaitStatus); ok {
    15  			return procExit.ExitStatus(), nil
    16  		}
    17  	}
    18  	return exitCode, fmt.Errorf("failed to get exit code")
    19  }