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.
51 lines
785 B
51 lines
785 B
package cmd
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"io"
|
|
"os/exec"
|
|
"sync"
|
|
)
|
|
|
|
func Exec(cmdStr string) (out string, err error) {
|
|
command := exec.CommandContext(context.Background(), "bash", "-c", cmdStr)
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
|
|
stdout, err := command.StdoutPipe()
|
|
if err != nil {
|
|
return
|
|
}
|
|
readout := bufio.NewReader(stdout)
|
|
go func() {
|
|
defer wg.Done()
|
|
out = getOutput(readout)
|
|
}()
|
|
|
|
err = command.Run()
|
|
if err != nil {
|
|
return
|
|
}
|
|
wg.Wait()
|
|
return
|
|
}
|
|
|
|
func getOutput(reader *bufio.Reader) string {
|
|
var sumOutput string
|
|
outputBytes := make([]byte, 200)
|
|
for {
|
|
n, err := reader.Read(outputBytes)
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
sumOutput += err.Error()
|
|
}
|
|
output := string(outputBytes[:n])
|
|
sumOutput += output
|
|
}
|
|
return sumOutput
|
|
}
|