error codes

This commit is contained in:
2022-04-12 21:37:35 +02:00
parent 4839c013b9
commit fe6c6f2373
5 changed files with 38 additions and 27 deletions

View File

@@ -28,7 +28,7 @@ type ExecuteOptions struct {
Dir string
}
func ExecuteCommand(options ExecuteOptions, args ...string) (string, error) {
func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error) {
cmd := exec.Command(options.Command, args...)
env := os.Environ()
for k, v := range options.Envs {
@@ -47,12 +47,16 @@ func ExecuteCommand(options ExecuteOptions, args ...string) (string, error) {
cmd.Stderr = &error
err := cmd.Run()
if err != nil {
return error.String(), err
if exitError, ok := err.(*exec.ExitError); ok {
return exitError.ExitCode(), error.String(), err
} else {
return -1, error.String(), err
}
}
return out.String(), nil
return 0, out.String(), nil
}
func ExecuteResticCommand(options ExecuteOptions, args ...string) (string, error) {
func ExecuteResticCommand(options ExecuteOptions, args ...string) (int, string, error) {
options.Command = RESTIC_BIN
var c = GetConfig()
var optionsAsString = getOptions(c.Global, "")
@@ -80,6 +84,6 @@ func CopyFile(from, to string) error {
}
func CheckIfVolumeExists(volume string) bool {
_, err := ExecuteCommand(ExecuteOptions{Command: "docker"}, "volume", "inspect", volume)
_, _, err := ExecuteCommand(ExecuteOptions{Command: "docker"}, "volume", "inspect", volume)
return err == nil
}