增加喜用命令执行
This commit is contained in:
parent
80d1d51776
commit
a015d2984d
20
define/result.go
Normal file
20
define/result.go
Normal file
@ -0,0 +1,20 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2022-05-20 22:00
|
||||
package define
|
||||
|
||||
// Result 执行结果
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 22:00 2022/5/20
|
||||
type Result struct {
|
||||
Err error // 异常信息
|
||||
Output []byte // 输出的内容
|
||||
StartTime int64 // 开始时间
|
||||
FinishTime int64 // 完成时间
|
||||
}
|
50
execute.go
Normal file
50
execute.go
Normal file
@ -0,0 +1,50 @@
|
||||
// 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{
|
||||
Err: nil,
|
||||
Output: nil,
|
||||
StartTime: time.Now().UnixNano(),
|
||||
FinishTime: 0,
|
||||
}
|
||||
defer func() {
|
||||
result.FinishTime = time.Now().UnixNano()
|
||||
}()
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user