github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/utils/helper/command.go (about) 1 package helper 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 8 "github.com/caos/orbos/mntr" 9 ) 10 11 func RunWithOutput(monitor mntr.Monitor, cmd exec.Cmd) ([]byte, error) { 12 13 var command string 14 for _, arg := range cmd.Args { 15 if strings.Contains(arg, " ") { 16 command += " \\\"" + arg + "\\\"" 17 continue 18 } 19 command += " " + arg 20 } 21 command = command[1:] 22 23 cmdMonitor := monitor.WithFields(map[string]interface{}{ 24 "cmd": command, 25 }) 26 27 cmdMonitor.Debug("Executing") 28 29 out, err := cmd.CombinedOutput() 30 cmdMonitor.Debug(string(out)) 31 32 if err != nil { 33 return nil, fmt.Errorf("error while executing command: \"%s\": response: %s: %w", strings.Join(cmd.Args, "\" \""), string(out), err) 34 } 35 36 return out, nil 37 } 38 39 func Run(monitor mntr.Monitor, cmd exec.Cmd) error { 40 _, err := RunWithOutput(monitor, cmd) 41 return err 42 }