59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
|
// Package cmd...
|
||
|
//
|
||
|
// Description : cmd...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 2021-11-12 2:21 下午
|
||
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"os/exec"
|
||
|
"strings"
|
||
|
|
||
|
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
||
|
)
|
||
|
|
||
|
// Execute 执行指令
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 2:22 下午 2021/11/12
|
||
|
func Execute(cmdConfig Config) *Result {
|
||
|
if len(cmdConfig.WorkDir) == 0 {
|
||
|
cmdConfig.WorkDir, _ = util.GetProjectPath()
|
||
|
}
|
||
|
paramList := buildCmdParameter(cmdConfig.Script, cmdConfig.ParameterList)
|
||
|
cmdInstance := exec.Command(cmdConfig.Command, paramList...)
|
||
|
// 设置指令的工作目录
|
||
|
cmdInstance.Dir = cmdConfig.WorkDir
|
||
|
result := &Result{
|
||
|
Err: nil,
|
||
|
Output: nil,
|
||
|
WorkDir: cmdConfig.WorkDir,
|
||
|
ExecuteCommand: cmdConfig.Command + " " + strings.Join(paramList, " "),
|
||
|
}
|
||
|
result.Output, result.Err = cmdInstance.CombinedOutput()
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
// buildCmdParameter 构建参数列表
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 2:53 下午 2021/11/12
|
||
|
func buildCmdParameter(script string, parameterList []Parameter) []string {
|
||
|
paramList := make([]string, 0)
|
||
|
if len(script) > 0 {
|
||
|
paramList = append(paramList, script)
|
||
|
}
|
||
|
for _, item := range parameterList {
|
||
|
if len(item.Key) == 0 {
|
||
|
paramList = append(paramList, item.Value)
|
||
|
continue
|
||
|
}
|
||
|
paramList = append(paramList, item.Key+"="+item.Value)
|
||
|
}
|
||
|
return paramList
|
||
|
}
|