mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
force our exit trap to always run (for rtnstate commands) (#556)
* add command validation to shellapi. mock out bash/zsh versions * implement validate command fn bash and zsh * test validate command * change rtnstate commands to always end with a builtin, so we always get our exit trap to run * simplify the rtnstate modification, don't add the 'wait' (as this is a different problem/feature) * update schema
This commit is contained in:
@@ -15,9 +15,11 @@ import (
|
||||
mathrand "math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
@@ -635,3 +637,39 @@ func DetectMimeType(path string) string {
|
||||
}
|
||||
return rtn
|
||||
}
|
||||
|
||||
func GetCmdExitCode(cmd *exec.Cmd, err error) int {
|
||||
if cmd == nil || cmd.ProcessState == nil {
|
||||
return GetExitCode(err)
|
||||
}
|
||||
status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus)
|
||||
if !ok {
|
||||
return cmd.ProcessState.ExitCode()
|
||||
}
|
||||
signaled := status.Signaled()
|
||||
if signaled {
|
||||
signal := status.Signal()
|
||||
return 128 + int(signal)
|
||||
}
|
||||
exitStatus := status.ExitStatus()
|
||||
return exitStatus
|
||||
}
|
||||
|
||||
func GetExitCode(err error) int {
|
||||
if err == nil {
|
||||
return 0
|
||||
}
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
return exitErr.ExitCode()
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
func GetFirstLine(s string) string {
|
||||
idx := strings.Index(s, "\n")
|
||||
if idx == -1 {
|
||||
return s
|
||||
}
|
||||
return s[0:idx]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user