mirror of https://github.com/1Panel-dev/1Panel
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.2 KiB
54 lines
1.2 KiB
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os/exec"
|
|
)
|
|
|
|
func Exec(cmdStr string) (string, error) {
|
|
cmd := exec.Command("bash", "-c", cmdStr)
|
|
var stdout, stderr bytes.Buffer
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
errMsg := ""
|
|
if len(stderr.String()) != 0 {
|
|
errMsg = fmt.Sprintf("stderr: %s", stderr.String())
|
|
}
|
|
if len(stdout.String()) != 0 {
|
|
if len(errMsg) != 0 {
|
|
errMsg = fmt.Sprintf("%s; stdout: %s", errMsg, stdout.String())
|
|
} else {
|
|
errMsg = fmt.Sprintf("stdout: %s", stdout.String())
|
|
}
|
|
}
|
|
return errMsg, err
|
|
}
|
|
return stdout.String(), nil
|
|
}
|
|
|
|
func Execf(cmdStr string, a ...interface{}) (string, error) {
|
|
cmd := exec.Command("bash", "-c", fmt.Sprintf(cmdStr, a...))
|
|
var stdout, stderr bytes.Buffer
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
errMsg := ""
|
|
if len(stderr.String()) != 0 {
|
|
errMsg = fmt.Sprintf("stderr: %s", stderr.String())
|
|
}
|
|
if len(stdout.String()) != 0 {
|
|
if len(errMsg) != 0 {
|
|
errMsg = fmt.Sprintf("%s; stdout: %s", errMsg, stdout.String())
|
|
} else {
|
|
errMsg = fmt.Sprintf("stdout: %s", stdout.String())
|
|
}
|
|
}
|
|
return errMsg, err
|
|
}
|
|
return stdout.String(), nil
|
|
}
|