github.com/scaleway/scaleway-cli@v1.11.1/pkg/cli/test.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "os/exec" 6 "syscall" 7 ) 8 9 var ( 10 scwcli = "../../scw" 11 publicCommands = []string{ 12 "help", "attach", "commit", "cp", "create", 13 "events", "exec", "history", "images", "info", 14 "inspect", "kill", "login", "logout", "logs", 15 "port", "ps", "rename", "restart", "rm", "rmi", 16 "run", "search", "start", "stop", "tag", "top", 17 "version", "wait", 18 } 19 secretCommands = []string{ 20 "_patch", "_completion", "_flush-cache", "_userdata", "_billing", 21 } 22 publicOptions = []string{ 23 "-h, --help=false", 24 "-D, --debug=false", 25 "-V, --verbose=false", 26 "-q, --quiet=false", 27 "--sensitive=false", 28 "-v, --version=false", 29 } 30 ) 31 32 func shouldFitInTerminal(actual interface{}, expected ...interface{}) string { 33 if len(actual.(string)) < 80 { 34 return "" 35 } 36 return fmt.Sprintf("len(%q)\n -> %d chars (> 80 chars)", actual, len(actual.(string))) 37 } 38 39 func getExitCode(err error) (int, error) { 40 exitCode := 0 41 if exiterr, ok := err.(*exec.ExitError); ok { 42 if procExit, ok := exiterr.Sys().(syscall.WaitStatus); ok { 43 return procExit.ExitStatus(), nil 44 } 45 } 46 return exitCode, fmt.Errorf("failed to get exit code") 47 } 48 49 func processExitCode(err error) (exitCode int) { 50 if err != nil { 51 var exiterr error 52 if exitCode, exiterr = getExitCode(err); exiterr != nil { 53 // TODO: Fix this so we check the error's text. 54 // we've failed to retrieve exit code, so we set it to 127 55 exitCode = 127 56 } 57 } 58 return 59 } 60 61 func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) { 62 exitCode = 0 63 out, err := cmd.CombinedOutput() 64 exitCode = processExitCode(err) 65 output = string(out) 66 return 67 }