1Panel/core/utils/cmd/cmd.go

77 lines
1.6 KiB
Go
Raw Normal View History

2024-07-19 19:04:11 +08:00
package cmd
import (
"bytes"
"fmt"
"os/exec"
"time"
"github.com/1Panel-dev/1Panel/core/buserr"
"github.com/1Panel-dev/1Panel/core/constant"
)
func Exec(cmdStr string) (string, error) {
return ExecWithTimeOut(cmdStr, 20*time.Second)
}
func SudoHandleCmd() string {
cmd := exec.Command("sudo", "-n", "ls")
if err := cmd.Run(); err == nil {
return "sudo "
}
return ""
}
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 handleErr(stdout, stderr, err)
}
return stdout.String(), nil
}
func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
cmd := exec.Command("bash", "-c", cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
return "", err
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
after := time.After(timeout)
select {
case <-after:
_ = cmd.Process.Kill()
return "", buserr.New(constant.ErrCmdTimeout)
case err := <-done:
if err != nil {
return handleErr(stdout, stderr, err)
}
}
return stdout.String(), nil
}
func handleErr(stdout, stderr bytes.Buffer, err error) (string, error) {
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
}