mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-11-16 20:49:16 +08:00
31 lines
615 B
Go
31 lines
615 B
Go
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 {
|
|
return stderr.String(), 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 {
|
|
return stderr.String(), err
|
|
}
|
|
return stdout.String(), nil
|
|
}
|