mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-06 18:10:26 +08:00
集群可以设置systemd系统服务
This commit is contained in:
61
internal/utils/command_executor.go
Normal file
61
internal/utils/command_executor.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// 命令执行器
|
||||
type CommandExecutor struct {
|
||||
commands []*Command
|
||||
}
|
||||
|
||||
// 获取新对象
|
||||
func NewCommandExecutor() *CommandExecutor {
|
||||
return &CommandExecutor{}
|
||||
}
|
||||
|
||||
// 添加命令
|
||||
func (this *CommandExecutor) Add(command string, arg ...string) {
|
||||
this.commands = append(this.commands, &Command{
|
||||
Name: command,
|
||||
Args: arg,
|
||||
})
|
||||
}
|
||||
|
||||
// 执行命令
|
||||
func (this *CommandExecutor) Run() (output string, err error) {
|
||||
if len(this.commands) == 0 {
|
||||
return "", errors.New("no commands no run")
|
||||
}
|
||||
var lastCmd *exec.Cmd = nil
|
||||
var lastData []byte = nil
|
||||
for _, command := range this.commands {
|
||||
cmd := exec.Command(command.Name, command.Args...)
|
||||
stdout := bytes.NewBuffer([]byte{})
|
||||
cmd.Stdout = stdout
|
||||
if lastCmd != nil {
|
||||
cmd.Stdin = bytes.NewBuffer(lastData)
|
||||
}
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
_, ok := err.(*exec.ExitError)
|
||||
if ok {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return "", err
|
||||
}
|
||||
lastData = stdout.Bytes()
|
||||
|
||||
lastCmd = cmd
|
||||
}
|
||||
|
||||
return string(bytes.TrimSpace(lastData)), nil
|
||||
}
|
||||
Reference in New Issue
Block a user