5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 04:29:35 +08:00

feat: add exit code to run

This commit is contained in:
Lea Anthony 2018-12-19 12:49:21 -08:00
parent 5e42d67570
commit 3e97a4cc4d

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"syscall"
) )
// ProgramHelper - Utility functions around installed applications // ProgramHelper - Utility functions around installed applications
@ -44,16 +45,15 @@ func (p *ProgramHelper) FindProgram(programName string) *Program {
} }
func (p *Program) GetFullPathToBinary() (string, error) { func (p *Program) GetFullPathToBinary() (string, error) {
result := filepath.Join(p.Path, p.Name) return filepath.Abs(p.Path)
return filepath.Abs(result)
} }
// Run will execute the program with the given parameters // Run will execute the program with the given parameters
// Returns stdout + stderr as strings and an error if one occured // Returns stdout + stderr as strings and an error if one occured
func (p *Program) Run(vars ...string) (stdout, stderr string, err error) { func (p *Program) Run(vars ...string) (stdout, stderr string, err error, exitCode int) {
command, err := p.GetFullPathToBinary() command, err := p.GetFullPathToBinary()
if err != nil { if err != nil {
return "", "", err return "", "", err, 1
} }
cmd := exec.Command(command, vars...) cmd := exec.Command(command, vars...)
var stdo, stde bytes.Buffer var stdo, stde bytes.Buffer
@ -62,5 +62,23 @@ func (p *Program) Run(vars ...string) (stdout, stderr string, err error) {
err = cmd.Run() err = cmd.Run()
stdout = string(stdo.Bytes()) stdout = string(stdo.Bytes())
stderr = string(stde.Bytes()) stderr = string(stde.Bytes())
// https://stackoverflow.com/questions/10385551/get-exit-code-go
if err != nil {
// try to get the exit code
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
} else {
exitCode = 1
if stderr == "" {
stderr = err.Error()
}
}
} else {
// success, exitCode should be 0 if go is ok
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
}
return return
} }