2022-05-20 22:23:40 +08:00
|
|
|
// Package linux ...
|
|
|
|
//
|
|
|
|
// Description : command ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2022-05-20 21:48
|
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"command/define"
|
|
|
|
"errors"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Execute 执行系统命令
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:49 2022/5/20
|
|
|
|
func Execute(workDir string, command string, param []string) define.Result {
|
|
|
|
//执行命令
|
|
|
|
result := define.Result{
|
2022-05-20 22:31:29 +08:00
|
|
|
WorkDir: workDir,
|
2022-05-20 22:23:40 +08:00
|
|
|
Err: nil,
|
|
|
|
Output: nil,
|
|
|
|
StartTime: time.Now().UnixNano(),
|
|
|
|
FinishTime: 0,
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
result.FinishTime = time.Now().UnixNano()
|
|
|
|
}()
|
2022-05-20 22:31:29 +08:00
|
|
|
if result.CmdPath, result.Err = Check(command); nil != result.Err {
|
|
|
|
return result
|
|
|
|
}
|
2022-05-20 22:23:40 +08:00
|
|
|
cmdInstance := exec.Command(command, param...)
|
|
|
|
cmdInstance.Dir = workDir
|
|
|
|
result.Output, result.Err = cmdInstance.CombinedOutput()
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check 检测命令
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:57 2022/5/20
|
|
|
|
func Check(command string) (string, error) {
|
|
|
|
if path, err := exec.LookPath(command); err != nil {
|
|
|
|
return "", errors.New(command + " 命令不存在 : " + err.Error())
|
|
|
|
} else {
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
}
|