43 lines
1 KiB
Go
43 lines
1 KiB
Go
package tools
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// exported function(s)
|
|
func SpawnSudoProcess(command string, arguments ...string) (string, error) {
|
|
var sudoArguments []string
|
|
sudoArguments = append(sudoArguments, command)
|
|
sudoArguments = append(sudoArguments, arguments...)
|
|
return SpawnProcess("sudo", sudoArguments...)
|
|
}
|
|
|
|
func SpawnProcess(command string, arguments ...string) (string, error) {
|
|
cmd := exec.Command(command, arguments...)
|
|
stdout, stdoutError := cmd.StdoutPipe()
|
|
stderr, stderrError := cmd.StderrPipe()
|
|
cmd.Start()
|
|
if stdoutError != nil {
|
|
return "", stdoutError
|
|
}
|
|
if stderrError != nil {
|
|
return "", stderrError
|
|
}
|
|
resultBytes, stdoutError := io.ReadAll(stdout)
|
|
if stdoutError != nil {
|
|
return "", stdoutError
|
|
}
|
|
errorBytes, stderrError := io.ReadAll(stderr)
|
|
if stderrError != nil {
|
|
return "", stderrError
|
|
}
|
|
cmd.Wait()
|
|
error := strings.Trim(string(errorBytes), "\n")
|
|
if len(error) > 0 {
|
|
return "", errors.New(error)
|
|
}
|
|
return strings.Trim(string(resultBytes), "\n"), nil
|
|
}
|