提交 dffa8c91 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

fix code review issues

上级 81f88906
...@@ -15,6 +15,10 @@ import ( ...@@ -15,6 +15,10 @@ import (
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
const (
WinFileSchema = "winfile:///"
)
func InitLog() { func InitLog() {
CONFIG.Zap.Director = filepath.Join(commConsts.WorkDir, CONFIG.Zap.Director) CONFIG.Zap.Director = filepath.Join(commConsts.WorkDir, CONFIG.Zap.Director)
if !dir.IsExist(CONFIG.Zap.Director) { // 判断是否有Director文件夹 if !dir.IsExist(CONFIG.Zap.Director) { // 判断是否有Director文件夹
...@@ -50,7 +54,7 @@ func InitExecLog(workspacePath string) { ...@@ -50,7 +54,7 @@ func InitExecLog(workspacePath string) {
// print to test log file // print to test log file
logPath := filepath.Join(commConsts.ExecLogDir, commConsts.LogText) logPath := filepath.Join(commConsts.ExecLogDir, commConsts.LogText)
if commonUtils.IsWin() { if commonUtils.IsWin() {
logPath = filepath.Join("winfile:///", logPath) logPath = filepath.Join(WinFileSchema, logPath)
zap.RegisterSink("winfile", newWinFileSink) zap.RegisterSink("winfile", newWinFileSink)
} }
...@@ -69,7 +73,7 @@ func InitExecLog(workspacePath string) { ...@@ -69,7 +73,7 @@ func InitExecLog(workspacePath string) {
// print to test result file // print to test result file
logPathResult := filepath.Join(commConsts.ExecLogDir, commConsts.ResultText) logPathResult := filepath.Join(commConsts.ExecLogDir, commConsts.ResultText)
if commonUtils.IsWin() { if commonUtils.IsWin() {
logPathResult = filepath.Join("winfile:///", logPathResult) logPathResult = filepath.Join(WinFileSchema, logPathResult)
zap.RegisterSink("winfile", newWinFileSink) zap.RegisterSink("winfile", newWinFileSink)
} }
config.OutputPaths = []string{logPathResult} config.OutputPaths = []string{logPathResult}
...@@ -133,8 +137,8 @@ func getLogConfig() (config zap.Config) { ...@@ -133,8 +137,8 @@ func getLogConfig() (config zap.Config) {
logPathInfo := filepath.Join(CONFIG.Zap.Director, "info.log") logPathInfo := filepath.Join(CONFIG.Zap.Director, "info.log")
logPathErr := filepath.Join(CONFIG.Zap.Director, "err.log") logPathErr := filepath.Join(CONFIG.Zap.Director, "err.log")
if commonUtils.IsWin() { if commonUtils.IsWin() {
logPathInfo = filepath.Join("winfile:///", logPathInfo) logPathInfo = filepath.Join(WinFileSchema, logPathInfo)
logPathErr = filepath.Join("winfile:///", logPathErr) logPathErr = filepath.Join(WinFileSchema, logPathErr)
} }
config = zap.Config{ config = zap.Config{
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"github.com/easysoft/zentaoatf/internal/server/modules/v1/model" "github.com/easysoft/zentaoatf/internal/server/modules/v1/model"
"github.com/fatih/color" "github.com/fatih/color"
"go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
) )
...@@ -33,7 +32,7 @@ func (r *InterpreterRepo) Get(id uint) (po model.Interpreter, err error) { ...@@ -33,7 +32,7 @@ func (r *InterpreterRepo) Get(id uint) (po model.Interpreter, err error) {
Where("NOT deleted"). Where("NOT deleted").
First(&po).Error First(&po).Error
if err != nil { if err != nil {
logUtils.Errorf(color.RedString("find interpreter by id failed, error: %s.", err.Error())) logUtils.Info(color.RedString("find interpreter by id failed, %s.", err.Error()))
return return
} }
...@@ -48,7 +47,7 @@ func (r *InterpreterRepo) Create(interpreter model.Interpreter) (id uint, err er ...@@ -48,7 +47,7 @@ func (r *InterpreterRepo) Create(interpreter model.Interpreter) (id uint, err er
err = r.DB.Model(&model.Interpreter{}).Create(&interpreter).Error err = r.DB.Model(&model.Interpreter{}).Create(&interpreter).Error
if err != nil { if err != nil {
logUtils.Errorf(color.RedString("create interpreter failed, error: %s.", err.Error())) logUtils.Info(color.RedString("create interpreter failed, %s.", err.Error()))
return 0, err return 0, err
} }
...@@ -65,7 +64,7 @@ func (r *InterpreterRepo) Update(interpreter model.Interpreter) error { ...@@ -65,7 +64,7 @@ func (r *InterpreterRepo) Update(interpreter model.Interpreter) error {
err = r.DB.Model(&model.Interpreter{}).Where("id = ?", interpreter.ID).Updates(&interpreter).Error err = r.DB.Model(&model.Interpreter{}).Where("id = ?", interpreter.ID).Updates(&interpreter).Error
if err != nil { if err != nil {
logUtils.Errorf(color.RedString("update interpreter failed, error: %s.", err.Error())) logUtils.Info(color.RedString("update interpreter failed, %s.", err.Error()))
return err return err
} }
...@@ -76,7 +75,7 @@ func (r *InterpreterRepo) Delete(id uint) (err error) { ...@@ -76,7 +75,7 @@ func (r *InterpreterRepo) Delete(id uint) (err error) {
err = r.DB.Model(&model.Interpreter{}).Where("id = ?", id). err = r.DB.Model(&model.Interpreter{}).Where("id = ?", id).
Updates(map[string]interface{}{"deleted": true}).Error Updates(map[string]interface{}{"deleted": true}).Error
if err != nil { if err != nil {
logUtils.Errorf("delete interpreter by id error", zap.String("error:", err.Error())) logUtils.Info(color.RedString("delete interpreter by id error, %s.", err.Error()))
return return
} }
......
...@@ -36,7 +36,7 @@ func (r *SiteRepo) Paginate(req serverDomain.ReqPaginate) (data domain.PageData, ...@@ -36,7 +36,7 @@ func (r *SiteRepo) Paginate(req serverDomain.ReqPaginate) (data domain.PageData,
err = db.Count(&count).Error err = db.Count(&count).Error
if err != nil { if err != nil {
logUtils.Errorf("count site error", zap.String("error:", err.Error())) logUtils.Info(color.RedString("count site error %s.", err.Error()))
return return
} }
......
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
serverDomain "github.com/easysoft/zentaoatf/internal/server/modules/v1/domain" serverDomain "github.com/easysoft/zentaoatf/internal/server/modules/v1/domain"
"github.com/easysoft/zentaoatf/internal/server/modules/v1/model" "github.com/easysoft/zentaoatf/internal/server/modules/v1/model"
"github.com/fatih/color" "github.com/fatih/color"
"go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
) )
...@@ -40,7 +39,6 @@ func (r *WorkspaceRepo) Paginate(req serverDomain.WorkspaceReqPaginate) (data do ...@@ -40,7 +39,6 @@ func (r *WorkspaceRepo) Paginate(req serverDomain.WorkspaceReqPaginate) (data do
err = db.Count(&count).Error err = db.Count(&count).Error
if err != nil { if err != nil {
logUtils.Errorf("count site error", zap.String("error:", err.Error()))
return return
} }
...@@ -50,7 +48,7 @@ func (r *WorkspaceRepo) Paginate(req serverDomain.WorkspaceReqPaginate) (data do ...@@ -50,7 +48,7 @@ func (r *WorkspaceRepo) Paginate(req serverDomain.WorkspaceReqPaginate) (data do
Scopes(dao.PaginateScope(req.Page, req.PageSize, req.Order, req.Field)). Scopes(dao.PaginateScope(req.Page, req.PageSize, req.Order, req.Field)).
Find(&pos).Error Find(&pos).Error
if err != nil { if err != nil {
logUtils.Errorf("query site error", zap.String("error:", err.Error())) logUtils.Info(color.RedString("query site error, %s.", err.Error()))
return return
} }
...@@ -65,7 +63,7 @@ func (r *WorkspaceRepo) Get(id uint) (po model.Workspace, err error) { ...@@ -65,7 +63,7 @@ func (r *WorkspaceRepo) Get(id uint) (po model.Workspace, err error) {
Where("NOT deleted"). Where("NOT deleted").
First(&po).Error First(&po).Error
if err != nil { if err != nil {
logUtils.Errorf(color.RedString("find workspace by id failed, error: %s.", err.Error())) logUtils.Info(color.RedString("find workspace by id failed, %s.", err.Error()))
return return
} }
...@@ -80,7 +78,7 @@ func (r *WorkspaceRepo) Create(workspace model.Workspace) (id uint, err error) { ...@@ -80,7 +78,7 @@ func (r *WorkspaceRepo) Create(workspace model.Workspace) (id uint, err error) {
err = r.DB.Model(&model.Workspace{}).Create(&workspace).Error err = r.DB.Model(&model.Workspace{}).Create(&workspace).Error
if err != nil { if err != nil {
logUtils.Errorf(color.RedString("create site failed, error: %s.", err.Error())) logUtils.Info(color.RedString("create site failed, %s.", err.Error()))
return 0, err return 0, err
} }
...@@ -132,7 +130,7 @@ func (r *WorkspaceRepo) DeleteByPath(path string, productId uint) (err error) { ...@@ -132,7 +130,7 @@ func (r *WorkspaceRepo) DeleteByPath(path string, productId uint) (err error) {
Error Error
if err != nil { if err != nil {
logUtils.Errorf(color.RedString("by path, delete workspace failed, error: %s.", err.Error())) logUtils.Info(color.RedString("by path, delete workspace failed, %s.", err.Error()))
return return
} }
...@@ -156,7 +154,7 @@ func (r *WorkspaceRepo) FindByPath(workspacePath string) (po model.Workspace, er ...@@ -156,7 +154,7 @@ func (r *WorkspaceRepo) FindByPath(workspacePath string) (po model.Workspace, er
err = db.First(&po).Error err = db.First(&po).Error
if err != nil { if err != nil {
logUtils.Errorf("find workspace by path error", err.Error()) logUtils.Info(color.RedString("find workspace by path error, %s.", err.Error()))
return return
} }
......
...@@ -6,6 +6,10 @@ import ( ...@@ -6,6 +6,10 @@ import (
"time" "time"
) )
const (
BrokerClosed = "broker closed"
)
type Broker interface { type Broker interface {
publish(topic string, msg interface{}) error publish(topic string, msg interface{}) error
subscribe(topic string) (<-chan interface{}, error) subscribe(topic string) (<-chan interface{}, error)
...@@ -50,7 +54,7 @@ func (b *BrokerImpl) close() { ...@@ -50,7 +54,7 @@ func (b *BrokerImpl) close() {
func (b *BrokerImpl) publish(topic string, pub interface{}) error { func (b *BrokerImpl) publish(topic string, pub interface{}) error {
select { select {
case <-b.exit: case <-b.exit:
return errors.New("broker closed") return errors.New(BrokerClosed)
default: default:
} }
...@@ -107,7 +111,7 @@ func (b *BrokerImpl) broadcast(msg interface{}, subscribers []chan interface{}) ...@@ -107,7 +111,7 @@ func (b *BrokerImpl) broadcast(msg interface{}, subscribers []chan interface{})
func (b *BrokerImpl) subscribe(topic string) (<-chan interface{}, error) { func (b *BrokerImpl) subscribe(topic string) (<-chan interface{}, error) {
select { select {
case <-b.exit: case <-b.exit:
return nil, errors.New("broker closed") return nil, errors.New(BrokerClosed)
default: default:
} }
...@@ -121,7 +125,7 @@ func (b *BrokerImpl) subscribe(topic string) (<-chan interface{}, error) { ...@@ -121,7 +125,7 @@ func (b *BrokerImpl) subscribe(topic string) (<-chan interface{}, error) {
func (b *BrokerImpl) unsubscribe(topic string, sub <-chan interface{}) error { func (b *BrokerImpl) unsubscribe(topic string, sub <-chan interface{}) error {
select { select {
case <-b.exit: case <-b.exit:
return errors.New("broker closed") return errors.New(BrokerClosed)
default: default:
} }
......
...@@ -4,16 +4,13 @@ import ( ...@@ -4,16 +4,13 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"fmt" "fmt"
commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common" commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118" i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118"
logUtils "github.com/easysoft/zentaoatf/pkg/lib/log" logUtils "github.com/easysoft/zentaoatf/pkg/lib/log"
stringUtils "github.com/easysoft/zentaoatf/pkg/lib/string" stringUtils "github.com/easysoft/zentaoatf/pkg/lib/string"
"github.com/kataras/iris/v12/websocket"
"io" "io"
"os" "os"
"os/exec" "os/exec"
"regexp"
"strings" "strings"
) )
...@@ -31,12 +28,7 @@ func ExecWinCmd(cmdStr string) (string, error) { ...@@ -31,12 +28,7 @@ func ExecWinCmd(cmdStr string) (string, error) {
} }
func ExeSysCmd(cmdStr string) (string, error) { func ExeSysCmd(cmdStr string) (string, error) {
var cmd *exec.Cmd cmd := getCmd(cmdStr)
if commonUtils.IsWin() {
cmd = exec.Command("cmd", "/C", cmdStr)
} else {
cmd = exec.Command("/bin/bash", "-c", cmdStr)
}
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
...@@ -62,12 +54,7 @@ func ExeShellWithPid(cmdStr string) (string, error, int) { ...@@ -62,12 +54,7 @@ func ExeShellWithPid(cmdStr string) (string, error, int) {
} }
func ExeShellInDirWithPid(cmdStr string, dir string) (ret string, err error, pid int) { func ExeShellInDirWithPid(cmdStr string, dir string) (ret string, err error, pid int) {
var cmd *exec.Cmd cmd := getCmd(cmdStr)
if commonUtils.IsWin() {
cmd = exec.Command("cmd", "/C", cmdStr)
} else {
cmd = exec.Command("/bin/bash", "-c", cmdStr)
}
if dir != "" { if dir != "" {
cmd.Dir = dir cmd.Dir = dir
} }
...@@ -94,12 +81,7 @@ func ExeShellWithOutputInDir(cmdStr string, dir string) ([]string, error) { ...@@ -94,12 +81,7 @@ func ExeShellWithOutputInDir(cmdStr string, dir string) ([]string, error) {
} }
func ExeShellWithEnvVarsAndOutputInDir(cmdStr, dir string, envVars []string) ([]string, error) { func ExeShellWithEnvVarsAndOutputInDir(cmdStr, dir string, envVars []string) ([]string, error) {
var cmd *exec.Cmd cmd := getCmd(cmdStr)
if commonUtils.IsWin() {
cmd = exec.Command("cmd", "/C", cmdStr)
} else {
cmd = exec.Command("/bin/bash", "-c", cmdStr)
}
if dir != "" { if dir != "" {
cmd.Dir = dir cmd.Dir = dir
...@@ -139,123 +121,18 @@ func ExeShellWithEnvVarsAndOutputInDir(cmdStr, dir string, envVars []string) ([] ...@@ -139,123 +121,18 @@ func ExeShellWithEnvVarsAndOutputInDir(cmdStr, dir string, envVars []string) ([]
return output, nil return output, nil
} }
func ExeShellCallback(ch chan int, cmdStr, dir string, func getCmd(cmdStr string) (cmd *exec.Cmd) {
fun func(info string, msg websocket.Message), msg websocket.Message) (err error) {
var cmd *exec.Cmd
if commonUtils.IsWin() { if commonUtils.IsWin() {
cmd = exec.Command("cmd", "/C", cmdStr) cmd = getWinCmd(cmdStr)
} else { } else {
cmd = exec.Command("/bin/bash", "-c", cmdStr) cmd = getLinuxCmd(cmdStr)
}
if dir != "" {
cmd.Dir = dir
}
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
return
}
cmd.Start()
if err != nil {
return
}
reader := bufio.NewReader(stdout)
for {
line, err2 := reader.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
}
line = strings.Trim(line, "\n")
fun(line, msg)
select {
case <-ch:
fmt.Println("exiting...")
ch <- 1
return
default:
fmt.Println("continue...")
}
} }
cmd.Wait()
return return
} }
func getWinCmd(cmdStr string) (cmd *exec.Cmd) {
func GetProcess(app string) (string, error) { return exec.Command("cmd", "/C", cmdStr)
var cmd *exec.Cmd
tmpl := ""
cmdStr := ""
if commonUtils.IsWin() {
tmpl = `tasklist`
cmdStr = fmt.Sprintf(tmpl)
cmd = exec.Command("cmd", "/C", cmdStr)
} else {
tmpl = `ps -ef | grep "%s" | grep -v "grep" | awk '{print $2}'`
cmdStr = fmt.Sprintf(tmpl, app)
cmd = exec.Command("/bin/bash", "-c", cmdStr)
}
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
output := ""
if commonUtils.IsWin() {
arr := strings.Split(out.String(), "\n")
for _, line := range arr {
if strings.Index(line, app+".exe") > -1 {
arr2 := regexp.MustCompile(`\s+`).Split(line, -1)
output = arr2[1]
break
}
}
} else {
output = out.String()
}
return output, err
}
func KillProcess(app string) (string, error) {
var cmd *exec.Cmd
tmpl := ""
cmdStr := ""
if commonUtils.IsWin() {
// tasklist | findstr ztf.exe
tmpl = `taskkill.exe /f /im %s.exe`
cmdStr = fmt.Sprintf(tmpl, app)
cmd = exec.Command("cmd", "/C", cmdStr)
} else {
tmpl = `ps -ef | grep '%s' | grep -v "grep" | awk '{print $2}' | xargs kill -9`
cmdStr = fmt.Sprintf(tmpl, app)
cmd = exec.Command("/bin/bash", "-c", cmdStr)
}
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
output := out.String()
return output, err
} }
func getLinuxCmd(cmdStr string) (cmd *exec.Cmd) {
func KillProcessById(pid int) { return exec.Command("/bin/bash", "-c", cmdStr)
cmdStr := fmt.Sprintf("kill -9 %d", pid)
ExeShell(cmdStr)
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册