refactor: 新增base.Repo与base.App,重构repo与app层代码

This commit is contained in:
meilin.huang
2023-10-26 17:15:49 +08:00
parent 10f6b03fb5
commit a1303b52eb
115 changed files with 1867 additions and 1696 deletions

View File

@@ -18,14 +18,17 @@ type AuthCert struct {
func (ac *AuthCert) BaseAuthCerts(rc *req.Ctx) {
queryCond, page := ginx.BindQueryAndPage(rc.GinCtx, new(entity.AuthCertQuery))
rc.ResData = ac.AuthCertApp.GetPageList(queryCond, page, new([]vo.AuthCertBaseVO))
res, err := ac.AuthCertApp.GetPageList(queryCond, page, new([]vo.AuthCertBaseVO))
biz.ErrIsNil(err)
rc.ResData = res
}
func (ac *AuthCert) AuthCerts(rc *req.Ctx) {
queryCond, page := ginx.BindQueryAndPage(rc.GinCtx, new(entity.AuthCertQuery))
res := new([]*entity.AuthCert)
pageRes := ac.AuthCertApp.GetPageList(queryCond, page, res)
pageRes, err := ac.AuthCertApp.GetPageList(queryCond, page, res)
biz.ErrIsNil(err)
for _, r := range *res {
r.PwdDecrypt()
}
@@ -42,7 +45,7 @@ func (c *AuthCert) SaveAuthCert(rc *req.Ctx) {
rc.ReqParam = acForm
ac.SetBaseInfo(rc.LoginAccount)
c.AuthCertApp.Save(ac)
biz.ErrIsNil(c.AuthCertApp.Save(ac))
}
func (c *AuthCert) Delete(rc *req.Ctx) {

View File

@@ -11,9 +11,11 @@ import (
tagapp "mayfly-go/internal/tag/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/config"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/anyx"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/stringx"
"mayfly-go/pkg/ws"
@@ -44,7 +46,8 @@ func (m *Machine) Machines(rc *req.Ctx) {
}
condition.TagIds = tagIds
res := m.MachineApp.GetMachineList(condition, pageParam, new([]*vo.MachineVO))
res, err := m.MachineApp.GetMachineList(condition, pageParam, new([]*vo.MachineVO))
biz.ErrIsNil(err)
if res.Total == 0 {
rc.ResData = res
return
@@ -61,8 +64,9 @@ func (m *Machine) MachineTags(rc *req.Ctx) {
}
func (m *Machine) MachineStats(rc *req.Ctx) {
stats := m.MachineApp.GetCli(GetMachineId(rc.GinCtx)).GetAllStats()
rc.ResData = stats
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
rc.ResData = cli.GetAllStats()
}
// 保存机器信息
@@ -74,12 +78,13 @@ func (m *Machine) SaveMachine(rc *req.Ctx) {
rc.ReqParam = machineForm
me.SetBaseInfo(rc.LoginAccount)
m.MachineApp.Save(me)
biz.ErrIsNil(m.MachineApp.Save(me))
}
func (m *Machine) TestConn(rc *req.Ctx) {
me := ginx.BindJsonAndCopyTo(rc.GinCtx, new(form.MachineForm), new(entity.Machine))
m.MachineApp.TestConn(me)
// 测试连接
biz.ErrIsNilAppendErr(m.MachineApp.TestConn(me), "该机器无法连接: %s")
}
func (m *Machine) ChangeStatus(rc *req.Ctx) {
@@ -87,7 +92,7 @@ func (m *Machine) ChangeStatus(rc *req.Ctx) {
id := uint64(ginx.PathParamInt(g, "machineId"))
status := int8(ginx.PathParamInt(g, "status"))
rc.ReqParam = collx.Kvs("id", id, "status", status)
m.MachineApp.ChangeStatus(id, status)
biz.ErrIsNil(m.MachineApp.ChangeStatus(id, status))
}
func (m *Machine) DeleteMachine(rc *req.Ctx) {
@@ -126,7 +131,8 @@ func (m *Machine) GetProcess(rc *req.Ctx) {
count := ginx.QueryInt(g, "count", 10)
cmd += "| head -n " + fmt.Sprintf("%d", count)
cli := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().TagPath), "%s")
res, err := cli.Run(cmd)
@@ -139,7 +145,8 @@ func (m *Machine) KillProcess(rc *req.Ctx) {
pid := rc.GinCtx.Query("pid")
biz.NotEmpty(pid, "进程id不能为空")
cli := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().TagPath), "%s")
res, err := cli.Run("sudo kill -9 " + pid)
@@ -151,7 +158,7 @@ func (m *Machine) WsSSH(g *gin.Context) {
defer func() {
if wsConn != nil {
if err := recover(); err != nil {
wsConn.WriteMessage(websocket.TextMessage, []byte(err.(error).Error()))
wsConn.WriteMessage(websocket.TextMessage, []byte(anyx.ToString(err)))
}
wsConn.Close()
}
@@ -161,10 +168,11 @@ func (m *Machine) WsSSH(g *gin.Context) {
// 权限校验
rc := req.NewCtxWithGin(g).WithRequiredPermission(req.NewPermission("machine:terminal"))
if err = req.PermissionHandler(rc); err != nil {
panic(biz.NewBizErr("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m"))
panic(errorx.NewBiz("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m"))
}
cli := m.MachineApp.GetCli(GetMachineId(g))
cli, err := m.MachineApp.GetCli(GetMachineId(g))
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().TagPath), "%s")
cols := ginx.QueryInt(g, "cols", 80)

View File

@@ -22,12 +22,13 @@ func (m *MachineCronJob) MachineCronJobs(rc *req.Ctx) {
cond, pageParam := ginx.BindQueryAndPage(rc.GinCtx, new(entity.MachineCronJob))
vos := new([]*vo.MachineCronJobVO)
pr := m.MachineCronJobApp.GetPageList(cond, pageParam, vos)
pageRes, err := m.MachineCronJobApp.GetPageList(cond, pageParam, vos)
biz.ErrIsNil(err)
for _, mcj := range *vos {
mcj.Running = scheduler.ExistKey(mcj.Key)
}
rc.ResData = pr
rc.ResData = pageRes
}
func (m *MachineCronJob) Save(rc *req.Ctx) {
@@ -35,7 +36,8 @@ func (m *MachineCronJob) Save(rc *req.Ctx) {
mcj := ginx.BindJsonAndCopyTo[*entity.MachineCronJob](rc.GinCtx, jobForm, new(entity.MachineCronJob))
rc.ReqParam = jobForm
mcj.SetBaseInfo(rc.LoginAccount)
cronJobId := m.MachineCronJobApp.Save(mcj)
cronJobId, err := m.MachineCronJobApp.Save(mcj)
biz.ErrIsNil(err)
// 关联机器
m.MachineCronJobApp.CronJobRelateMachines(cronJobId, jobForm.MachineIds, rc.LoginAccount)
@@ -63,5 +65,7 @@ func (m *MachineCronJob) GetRelateCronJobIds(rc *req.Ctx) {
func (m *MachineCronJob) CronJobExecs(rc *req.Ctx) {
cond, pageParam := ginx.BindQueryAndPage[*entity.MachineCronJobExec](rc.GinCtx, new(entity.MachineCronJobExec))
rc.ResData = m.MachineCronJobApp.GetExecPageList(cond, pageParam, new([]entity.MachineCronJobExec))
res, err := m.MachineCronJobApp.GetExecPageList(cond, pageParam, new([]entity.MachineCronJobExec))
biz.ErrIsNil(err)
rc.ResData = res
}

View File

@@ -12,6 +12,7 @@ import (
msgapp "mayfly-go/internal/msg/application"
msgdto "mayfly-go/internal/msg/application/dto"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/req"
@@ -43,7 +44,9 @@ const (
func (m *MachineFile) MachineFiles(rc *req.Ctx) {
g := rc.GinCtx
condition := &entity.MachineFile{MachineId: GetMachineId(g)}
rc.ResData = m.MachineFileApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineFileVO))
res, err := m.MachineFileApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineFileVO))
biz.ErrIsNil(err)
rc.ResData = res
}
func (m *MachineFile) SaveMachineFiles(rc *req.Ctx) {
@@ -52,11 +55,11 @@ func (m *MachineFile) SaveMachineFiles(rc *req.Ctx) {
entity.SetBaseInfo(rc.LoginAccount)
rc.ReqParam = fileForm
m.MachineFileApp.Save(entity)
biz.ErrIsNil(m.MachineFileApp.Save(entity))
}
func (m *MachineFile) DeleteFile(rc *req.Ctx) {
m.MachineFileApp.Delete(GetMachineFileId(rc.GinCtx))
biz.ErrIsNil(m.MachineFileApp.Delete(GetMachineFileId(rc.GinCtx)))
}
/*** sftp相关操作 */
@@ -121,7 +124,9 @@ func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
if !strings.HasSuffix(readPath, "/") {
readPath = readPath + "/"
}
fis := m.MachineFileApp.ReadDir(fid, readPath)
fis, err := m.MachineFileApp.ReadDir(fid, readPath)
biz.ErrIsNilAppendErr(err, "读取目录失败: %s")
fisVO := make([]vo.MachineFileInfo, 0)
for _, fi := range fis {
fisVO = append(fisVO, vo.MachineFileInfo{
@@ -143,7 +148,9 @@ func (m *MachineFile) GetDirSize(rc *req.Ctx) {
fid := GetMachineFileId(g)
readPath := g.Query("path")
rc.ResData = m.MachineFileApp.GetDirSize(fid, readPath)
size, err := m.MachineFileApp.GetDirSize(fid, readPath)
biz.ErrIsNil(err)
rc.ResData = size
}
func (m *MachineFile) GetFileStat(rc *req.Ctx) {
@@ -151,7 +158,9 @@ func (m *MachineFile) GetFileStat(rc *req.Ctx) {
fid := GetMachineFileId(g)
readPath := g.Query("path")
rc.ResData = m.MachineFileApp.FileStat(fid, readPath)
res, err := m.MachineFileApp.FileStat(fid, readPath)
biz.ErrIsNil(err, res)
rc.ResData = res
}
func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
@@ -220,9 +229,12 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
paths := mf.Value["paths"]
folderName := filepath.Dir(paths[0])
mcli := m.MachineFileApp.GetMachineCli(fid, basePath+"/"+folderName)
mcli, err := m.MachineFileApp.GetMachineCli(fid, basePath+"/"+folderName)
biz.ErrIsNil(err)
mi := mcli.GetMachine()
sftpCli := mcli.GetSftpCli()
sftpCli, err := mcli.GetSftpCli()
biz.ErrIsNil(err)
rc.ReqParam = collx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", basePath, folderName))
folderFiles := make([]FolderFile, len(paths))
@@ -258,7 +270,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
if err := recover(); err != nil {
logx.Errorf("文件上传失败: %s", err)
switch t := err.(type) {
case biz.BizError:
case errorx.BizError:
m.MsgApp.CreateAndSend(la, msgdto.ErrSysMsg("文件上传失败", fmt.Sprintf("执行文件上传失败:\n<-e errCode: %d, errMsg: %s", t.Code(), t.Error())))
}
}

View File

@@ -27,7 +27,9 @@ type MachineScript struct {
func (m *MachineScript) MachineScripts(rc *req.Ctx) {
g := rc.GinCtx
condition := &entity.MachineScript{MachineId: GetMachineId(g)}
rc.ResData = m.MachineScriptApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineScriptVO))
res, err := m.MachineScriptApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineScriptVO))
biz.ErrIsNil(err)
rc.ResData = res
}
func (m *MachineScript) SaveMachineScript(rc *req.Ctx) {
@@ -37,7 +39,7 @@ func (m *MachineScript) SaveMachineScript(rc *req.Ctx) {
rc.ReqParam = form
machineScript.SetBaseInfo(rc.LoginAccount)
m.MachineScriptApp.Save(machineScript)
biz.ErrIsNil(m.MachineScriptApp.Save(machineScript))
}
func (m *MachineScript) DeleteMachineScript(rc *req.Ctx) {
@@ -57,8 +59,8 @@ func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
scriptId := GetMachineScriptId(g)
machineId := GetMachineId(g)
ms := m.MachineScriptApp.GetById(scriptId, "MachineId", "Name", "Script")
biz.NotNil(ms, "该脚本不存在")
ms, err := m.MachineScriptApp.GetById(new(entity.MachineScript), scriptId, "MachineId", "Name", "Script")
biz.ErrIsNil(err, "该脚本不存在")
biz.IsTrue(ms.MachineId == application.Common_Script_Machine_Id || ms.MachineId == machineId, "该脚本不属于该机器")
script := ms.Script
@@ -66,7 +68,8 @@ func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
if params := g.Query("params"); params != "" {
script = stringx.TemplateParse(ms.Script, jsonx.ToMap(params))
}
cli := m.MachineApp.GetCli(machineId)
cli, err := m.MachineApp.GetCli(machineId)
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().TagPath), "%s")
res, err := cli.Run(script)

View File

@@ -5,17 +5,23 @@ import (
)
var (
machineFileApp MachineFile = newMachineFileApp(persistence.GetMachineFileRepo(), persistence.GetMachineRepo())
machineScriptApp MachineScript = newMachineScriptApp(persistence.GetMachineScriptRepo(), persistence.GetMachineRepo())
authCertApp AuthCert = newAuthCertApp(persistence.GetAuthCertRepo())
machineApp Machine = newMachineApp(
persistence.GetMachineRepo(),
GetAuthCertApp(),
)
machineFileApp MachineFile = newMachineFileApp(
persistence.GetMachineFileRepo(),
GetMachineApp(),
)
machineScriptApp MachineScript = newMachineScriptApp(
persistence.GetMachineScriptRepo(),
GetMachineApp(),
)
authCertApp AuthCert = newAuthCertApp(persistence.GetAuthCertRepo())
machineCropJobApp MachineCronJob = newMachineCronJobApp(
persistence.GetMachineCronJobRepo(),
persistence.GetMachineCronJobRelateRepo(),

View File

@@ -3,62 +3,56 @@ package application
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/model"
)
type AuthCert interface {
GetPageList(condition *entity.AuthCertQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
base.App[*entity.AuthCert]
Save(ac *entity.AuthCert)
GetPageList(condition *entity.AuthCertQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
GetById(id uint64) *entity.AuthCert
Save(ac *entity.AuthCert) error
GetByIds(ids ...uint64) []*entity.AuthCert
DeleteById(id uint64)
}
func newAuthCertApp(authCertRepo repository.AuthCert) AuthCert {
return &authCertAppImpl{
authCertRepo: authCertRepo,
base.AppImpl[*entity.AuthCert, repository.AuthCert]{Repo: authCertRepo},
}
}
type authCertAppImpl struct {
authCertRepo repository.AuthCert
base.AppImpl[*entity.AuthCert, repository.AuthCert]
}
func (a *authCertAppImpl) GetPageList(condition *entity.AuthCertQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return a.authCertRepo.GetPageList(condition, pageParam, toEntity)
func (a *authCertAppImpl) GetPageList(condition *entity.AuthCertQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return a.GetRepo().GetPageList(condition, pageParam, toEntity)
}
func (a *authCertAppImpl) Save(ac *entity.AuthCert) {
func (a *authCertAppImpl) Save(ac *entity.AuthCert) error {
oldAc := &entity.AuthCert{Name: ac.Name}
err := a.authCertRepo.GetByCondition(oldAc, "Id", "Name")
err := a.GetBy(oldAc, "Id", "Name")
ac.PwdEncrypt()
if ac.Id == 0 {
biz.IsTrue(err != nil, "该凭证名已存在")
a.authCertRepo.Insert(ac)
return
if err == nil {
return errorx.NewBiz("该凭证名已存在")
}
return a.Insert(ac)
}
// 如果存在该库,则校验修改的库是否为该库
if err == nil {
biz.IsTrue(oldAc.Id == ac.Id, "该凭证名已存在")
if err == nil && oldAc.Id != ac.Id {
return errorx.NewBiz("该凭证名已存在")
}
a.authCertRepo.Update(ac)
}
func (a *authCertAppImpl) GetById(id uint64) *entity.AuthCert {
return a.authCertRepo.GetById(id)
return a.UpdateById(ac)
}
func (a *authCertAppImpl) GetByIds(ids ...uint64) []*entity.AuthCert {
return a.authCertRepo.GetByIds(ids...)
}
func (a *authCertAppImpl) DeleteById(id uint64) {
a.authCertRepo.DeleteById(id)
acs := new([]*entity.AuthCert)
a.GetByIdIn(acs, ids)
return *acs
}

View File

@@ -5,7 +5,8 @@ import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/internal/machine/infrastructure/machine"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
@@ -13,90 +14,93 @@ import (
)
type Machine interface {
// 根据条件获取账号信息
GetMachine(condition *entity.Machine, cols ...string) error
base.App[*entity.Machine]
Save(*entity.Machine)
Save(*entity.Machine) error
// 测试机器连接
TestConn(me *entity.Machine)
TestConn(me *entity.Machine) error
// 调整机器状态
ChangeStatus(id uint64, status int8)
ChangeStatus(id uint64, status int8) error
Count(condition *entity.MachineQuery) int64
Delete(id uint64)
// 根据id获取
GetById(id uint64, cols ...string) *entity.Machine
Delete(id uint64) error
// 分页获取机器信息列表
GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) *model.PageResult[*[]*vo.MachineVO]
GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) (*model.PageResult[*[]*vo.MachineVO], error)
// 获取机器连接
GetCli(id uint64) *machine.Cli
GetCli(id uint64) (*machine.Cli, error)
// 获取ssh隧道机器连接
GetSshTunnelMachine(id int) *machine.SshTunnelMachine
GetSshTunnelMachine(id int) (*machine.SshTunnelMachine, error)
}
func newMachineApp(machineRepo repository.Machine, authCertApp AuthCert) Machine {
return &machineAppImpl{
machineRepo: machineRepo,
app := &machineAppImpl{
authCertApp: authCertApp,
}
app.Repo = machineRepo
return app
}
type machineAppImpl struct {
machineRepo repository.Machine
base.AppImpl[*entity.Machine, repository.Machine]
authCertApp AuthCert
}
// 分页获取机器信息列表
func (m *machineAppImpl) GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) *model.PageResult[*[]*vo.MachineVO] {
return m.machineRepo.GetMachineList(condition, pageParam, toEntity, orderBy...)
func (m *machineAppImpl) GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) (*model.PageResult[*[]*vo.MachineVO], error) {
return m.GetRepo().GetMachineList(condition, pageParam, toEntity, orderBy...)
}
func (m *machineAppImpl) Count(condition *entity.MachineQuery) int64 {
return m.machineRepo.Count(condition)
return m.GetRepo().Count(condition)
}
func (m *machineAppImpl) Save(me *entity.Machine) {
func (m *machineAppImpl) Save(me *entity.Machine) error {
oldMachine := &entity.Machine{Ip: me.Ip, Port: me.Port, Username: me.Username}
if me.SshTunnelMachineId > 0 {
oldMachine.SshTunnelMachineId = me.SshTunnelMachineId
}
err := m.GetMachine(oldMachine)
err := m.GetBy(oldMachine)
me.PwdEncrypt()
if me.Id == 0 {
biz.IsTrue(err != nil, "该机器信息已存在")
if err == nil {
return errorx.NewBiz("该机器信息已存在")
}
// 新增机器,默认启用状态
me.Status = entity.MachineStatusEnable
m.machineRepo.Create(me)
return
return m.Insert(me)
}
// 如果存在该库,则校验修改的库是否为该库
if err == nil {
biz.IsTrue(oldMachine.Id == me.Id, "该机器信息已存在")
if err == nil && oldMachine.Id != me.Id {
return errorx.NewBiz("该机器信息已存在")
}
// 关闭连接
machine.DeleteCli(me.Id)
m.machineRepo.UpdateById(me)
return m.UpdateById(me)
}
func (m *machineAppImpl) TestConn(me *entity.Machine) {
func (m *machineAppImpl) TestConn(me *entity.Machine) error {
me.Id = 0
// 测试连接
biz.ErrIsNilAppendErr(machine.TestConn(*m.toMachineInfo(me), func(u uint64) *machine.Info {
mi, err := m.toMachineInfo(me)
if err != nil {
return err
}
return machine.TestConn(*mi, func(u uint64) (*machine.Info, error) {
return m.toMachineInfoById(u)
}), "该机器无法连接: %s")
})
}
func (m *machineAppImpl) ChangeStatus(id uint64, status int8) {
func (m *machineAppImpl) ChangeStatus(id uint64, status int8) error {
if status == entity.MachineStatusDisable {
// 关闭连接
machine.DeleteCli(id)
@@ -104,63 +108,55 @@ func (m *machineAppImpl) ChangeStatus(id uint64, status int8) {
machine := new(entity.Machine)
machine.Id = id
machine.Status = status
m.machineRepo.UpdateById(machine)
return m.UpdateById(machine)
}
// 根据条件获取机器信息
func (m *machineAppImpl) Delete(id uint64) {
func (m *machineAppImpl) Delete(id uint64) error {
// 关闭连接
machine.DeleteCli(id)
gormx.Tx(
return gormx.Tx(
func(db *gorm.DB) error {
// 删除machine表信息
return gormx.DeleteByIdWithDb(db, new(entity.Machine), id)
},
func(db *gorm.DB) error {
// 删除machine_file
return gormx.DeleteByConditionWithDb(db, &entity.MachineFile{MachineId: id})
return gormx.DeleteByWithDb(db, &entity.MachineFile{MachineId: id})
},
func(db *gorm.DB) error {
// 删除machine_script
return gormx.DeleteByConditionWithDb(db, &entity.MachineScript{MachineId: id})
return gormx.DeleteByWithDb(db, &entity.MachineScript{MachineId: id})
},
)
}
// 根据条件获取机器信息
func (m *machineAppImpl) GetMachine(condition *entity.Machine, cols ...string) error {
return m.machineRepo.GetMachine(condition, cols...)
}
func (m *machineAppImpl) GetById(id uint64, cols ...string) *entity.Machine {
return m.machineRepo.GetById(id, cols...)
}
func (m *machineAppImpl) GetCli(machineId uint64) *machine.Cli {
cli, err := machine.GetCli(machineId, func(mid uint64) *machine.Info {
func (m *machineAppImpl) GetCli(machineId uint64) (*machine.Cli, error) {
return machine.GetCli(machineId, func(mid uint64) (*machine.Info, error) {
return m.toMachineInfoById(mid)
})
biz.ErrIsNilAppendErr(err, "获取客户端错误: %s")
return cli
}
func (m *machineAppImpl) GetSshTunnelMachine(machineId int) *machine.SshTunnelMachine {
sshTunnel, err := machine.GetSshTunnelMachine(machineId, func(mid uint64) *machine.Info {
func (m *machineAppImpl) GetSshTunnelMachine(machineId int) (*machine.SshTunnelMachine, error) {
return machine.GetSshTunnelMachine(machineId, func(mid uint64) (*machine.Info, error) {
return m.toMachineInfoById(mid)
})
biz.ErrIsNilAppendErr(err, "获取ssh隧道连接失败: %s")
return sshTunnel
}
// 生成机器信息根据授权凭证id填充用户密码等
func (m *machineAppImpl) toMachineInfoById(machineId uint64) *machine.Info {
me := m.GetById(machineId)
biz.IsTrue(me.Status == entity.MachineStatusEnable, "该机器已被停用")
func (m *machineAppImpl) toMachineInfoById(machineId uint64) (*machine.Info, error) {
me, err := m.GetById(new(entity.Machine), machineId)
if err != nil {
return nil, errorx.NewBiz("机器信息不存在")
}
if me.Status != entity.MachineStatusEnable {
return nil, errorx.NewBiz("该机器已被停用")
}
return m.toMachineInfo(me)
}
func (m *machineAppImpl) toMachineInfo(me *entity.Machine) *machine.Info {
func (m *machineAppImpl) toMachineInfo(me *entity.Machine) (*machine.Info, error) {
mi := new(machine.Info)
mi.Id = me.Id
mi.Name = me.Name
@@ -173,8 +169,10 @@ func (m *machineAppImpl) toMachineInfo(me *entity.Machine) *machine.Info {
mi.SshTunnelMachineId = me.SshTunnelMachineId
if me.UseAuthCert() {
ac := m.authCertApp.GetById(uint64(me.AuthCertId))
biz.NotNil(ac, "授权凭证信息已不存在,请重新关联")
ac, err := m.authCertApp.GetById(new(entity.AuthCert), uint64(me.AuthCertId))
if err != nil {
return nil, errorx.NewBiz("授权凭证信息已不存在,请重新关联")
}
mi.AuthMethod = ac.AuthMethod
ac.PwdDecrypt()
mi.Password = ac.Password
@@ -186,5 +184,5 @@ func (m *machineAppImpl) toMachineInfo(me *entity.Machine) *machine.Info {
}
mi.Password = me.Password
}
return mi
return mi, nil
}

View File

@@ -3,30 +3,33 @@ package application
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/base"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/rediscli"
"mayfly-go/pkg/scheduler"
"mayfly-go/pkg/utils/anyx"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/stringx"
"time"
)
type MachineCronJob interface {
base.App[*entity.MachineCronJob]
// 分页获取机器任务列表信息
GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
// 获取分页执行结果列表
GetExecPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetExecPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
// 根据id获取
GetById(id uint64, cols ...string) *entity.MachineCronJob
Save(entity *entity.MachineCronJob) uint64
Save(entity *entity.MachineCronJob) (uint64, error)
Delete(id uint64)
// 获取管理的机器列表id
// 获取计划任务关联的机器列表id
GetRelateMachineIds(cronJobId uint64) []uint64
// 获取机器关联的计划任务列表
@@ -43,7 +46,8 @@ type MachineCronJob interface {
}
type machineCropJobAppImpl struct {
machineCropJobRepo repository.MachineCronJob
base.AppImpl[*entity.MachineCronJob, repository.MachineCronJob]
machineCropJobRelateRepo repository.MachineCronJobRelate
machineCropJobExecRepo repository.MachineCronJobExec
machineApp Machine
@@ -55,48 +59,50 @@ func newMachineCronJobApp(
machineCropJobExecRepo repository.MachineCronJobExec,
machineApp Machine,
) MachineCronJob {
return &machineCropJobAppImpl{
machineCropJobRepo: machineCropJobRepo,
app := &machineCropJobAppImpl{
machineCropJobRelateRepo: machineCropJobRelateRepo,
machineCropJobExecRepo: machineCropJobExecRepo,
machineApp: machineApp,
}
app.Repo = machineCropJobRepo
return app
}
// 分页获取机器脚本任务列表
func (m *machineCropJobAppImpl) GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return m.machineCropJobRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
func (m *machineCropJobAppImpl) GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return m.GetRepo().GetPageList(condition, pageParam, toEntity, orderBy...)
}
// 获取分页执行结果列表
func (m *machineCropJobAppImpl) GetExecPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (m *machineCropJobAppImpl) GetExecPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return m.machineCropJobExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
// 根据id获取
func (m *machineCropJobAppImpl) GetById(id uint64, cols ...string) *entity.MachineCronJob {
return m.machineCropJobRepo.GetById(id, cols...)
}
// 保存机器任务信息
func (m *machineCropJobAppImpl) Save(mcj *entity.MachineCronJob) uint64 {
func (m *machineCropJobAppImpl) Save(mcj *entity.MachineCronJob) (uint64, error) {
// 更新操作
if mcj.Id != 0 {
m.machineCropJobRepo.UpdateById(mcj)
m.UpdateById(mcj)
cj, err := m.GetById(new(entity.MachineCronJob), mcj.Id)
if err != nil {
return 0, errorx.NewBiz("该任务不存在")
}
// 处理最新的计划任务
m.addCronJob(m.GetById(mcj.Id))
return mcj.Id
m.addCronJob(cj)
return mcj.Id, nil
}
m.addCronJob(mcj)
m.machineCropJobRepo.Insert(mcj)
return mcj.Id
if err := m.Insert(mcj); err != nil {
return 0, err
}
return mcj.Id, nil
}
func (m *machineCropJobAppImpl) Delete(id uint64) {
m.machineCropJobRepo.Delete(id)
m.machineCropJobExecRepo.Delete(&entity.MachineCronJobExec{CronJobId: id})
m.machineCropJobRelateRepo.Delete(&entity.MachineCronJobRelate{CronJobId: id})
m.DeleteById(id)
m.machineCropJobExecRepo.DeleteByCond(&entity.MachineCronJobExec{CronJobId: id})
m.machineCropJobRelateRepo.DeleteByCond(&entity.MachineCronJobRelate{CronJobId: id})
}
func (m *machineCropJobAppImpl) GetRelateMachineIds(cronJobId uint64) []uint64 {
@@ -125,7 +131,7 @@ func (m *machineCropJobAppImpl) CronJobRelateMachines(cronJobId uint64, machineI
m.machineCropJobRelateRepo.BatchInsert(addVals)
for _, delId := range delIds {
m.machineCropJobRelateRepo.Delete(&entity.MachineCronJobRelate{CronJobId: cronJobId, MachineId: delId})
m.machineCropJobRelateRepo.DeleteByCond(&entity.MachineCronJobRelate{CronJobId: cronJobId, MachineId: delId})
}
}
@@ -147,14 +153,14 @@ func (m *machineCropJobAppImpl) MachineRelateCronJobs(machineId uint64, cronJobs
m.machineCropJobRelateRepo.BatchInsert(addVals)
for _, delId := range delIds {
m.machineCropJobRelateRepo.Delete(&entity.MachineCronJobRelate{CronJobId: delId, MachineId: machineId})
m.machineCropJobRelateRepo.DeleteByCond(&entity.MachineCronJobRelate{CronJobId: delId, MachineId: machineId})
}
}
func (m *machineCropJobAppImpl) InitCronJob() {
defer func() {
if err := recover(); err != nil {
logx.Errorf("机器计划任务初始化失败: %s", err.(error).Error())
logx.ErrorTrace("机器计划任务初始化失败: %s", err.(error))
}
}()
@@ -166,7 +172,7 @@ func (m *machineCropJobAppImpl) InitCronJob() {
cond.Status = entity.MachineCronJobStatusEnable
mcjs := new([]entity.MachineCronJob)
pr := m.GetPageList(cond, pageParam, mcjs)
pr, _ := m.GetPageList(cond, pageParam, mcjs)
total := pr.Total
add := 0
@@ -218,7 +224,7 @@ func (m *machineCropJobAppImpl) runCronJob(key string) {
cronJob := new(entity.MachineCronJob)
cronJob.Key = key
err := m.machineCropJobRepo.GetBy(cronJob)
err := m.GetBy(cronJob)
// 不存在或禁用,则移除该任务
if err != nil || cronJob.Status == entity.MachineCronJobStatusDisable {
scheduler.RemoveByKey(key)
@@ -233,7 +239,7 @@ func (m *machineCropJobAppImpl) runCronJob(key string) {
func (m *machineCropJobAppImpl) runCronJob0(mid uint64, cronJob *entity.MachineCronJob) {
defer func() {
if err := recover(); err != nil {
res := err.(error).Error()
res := anyx.ToString(err)
m.machineCropJobExecRepo.Insert(&entity.MachineCronJobExec{
MachineId: mid,
CronJobId: cronJob.Id,
@@ -245,7 +251,9 @@ func (m *machineCropJobAppImpl) runCronJob0(mid uint64, cronJob *entity.MachineC
}
}()
res, err := m.machineApp.GetCli(uint64(mid)).Run(cronJob.Script)
machineCli, err := m.machineApp.GetCli(uint64(mid))
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
res, err := machineCli.Run(cronJob.Script)
if err != nil {
if res == "" {
res = err.Error()

View File

@@ -8,7 +8,7 @@ import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/internal/machine/infrastructure/machine"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/model"
"os"
@@ -19,7 +19,7 @@ import (
type MachineFile interface {
// 分页获取机器文件信息列表
GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
// 根据条件获取
GetMachineFile(condition *entity.MachineFile, cols ...string) error
@@ -27,15 +27,15 @@ type MachineFile interface {
// 根据id获取
GetById(id uint64, cols ...string) *entity.MachineFile
Save(entity *entity.MachineFile)
Save(entity *entity.MachineFile) error
Delete(id uint64)
Delete(id uint64) error
// 获取文件关联的机器信息,主要用于记录日志使用
// GetMachine(fileId uint64) *machine.Info
// 检查文件路径并返回机器id
GetMachineCli(fileId uint64, path ...string) *machine.Cli
GetMachineCli(fileId uint64, path ...string) (*machine.Cli, error)
/** sftp 相关操作 **/
@@ -46,13 +46,13 @@ type MachineFile interface {
CreateFile(fid uint64, path string) (*machine.Info, error)
// 读取目录
ReadDir(fid uint64, path string) []fs.FileInfo
ReadDir(fid uint64, path string) ([]fs.FileInfo, error)
// 获取指定目录内容大小
GetDirSize(fid uint64, path string) string
GetDirSize(fid uint64, path string) (string, error)
// 获取文件stat
FileStat(fid uint64, path string) string
FileStat(fid uint64, path string) (string, error)
// 读取文件内容
ReadFile(fileId uint64, path string) (*sftp.File, *machine.Info, error)
@@ -73,18 +73,19 @@ type MachineFile interface {
Rename(fileId uint64, oldname string, newname string) (*machine.Info, error)
}
func newMachineFileApp(machineFileRepo repository.MachineFile, machineRepo repository.Machine) MachineFile {
return &machineFileAppImpl{machineRepo: machineRepo, machineFileRepo: machineFileRepo}
func newMachineFileApp(machineFileRepo repository.MachineFile, machineApp Machine) MachineFile {
return &machineFileAppImpl{machineApp: machineApp, machineFileRepo: machineFileRepo}
}
type machineFileAppImpl struct {
machineFileRepo repository.MachineFile
machineRepo repository.Machine
machineApp Machine
}
// 分页获取机器脚本信息列表
func (m *machineFileAppImpl) GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (m *machineFileAppImpl) GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return m.machineFileRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
@@ -99,34 +100,41 @@ func (m *machineFileAppImpl) GetById(id uint64, cols ...string) *entity.MachineF
}
// 保存机器文件配置
func (m *machineFileAppImpl) Save(entity *entity.MachineFile) {
biz.NotNil(m.machineRepo.GetById(entity.MachineId, "Name"), "该机器不存在")
if entity.Id != 0 {
m.machineFileRepo.UpdateById(entity)
} else {
m.machineFileRepo.Create(entity)
func (m *machineFileAppImpl) Save(mf *entity.MachineFile) error {
_, err := m.machineApp.GetById(new(entity.Machine), mf.MachineId, "Name")
if err != nil {
return errorx.NewBiz("该机器不存在")
}
if mf.Id != 0 {
return m.machineFileRepo.UpdateById(mf)
}
return m.machineFileRepo.Create(mf)
}
// 根据id删除
func (m *machineFileAppImpl) Delete(id uint64) {
m.machineFileRepo.Delete(id)
func (m *machineFileAppImpl) Delete(id uint64) error {
return m.machineFileRepo.Delete(id)
}
func (m *machineFileAppImpl) ReadDir(fid uint64, path string) []fs.FileInfo {
mcli := m.GetMachineCli(fid, path)
func (m *machineFileAppImpl) ReadDir(fid uint64, path string) ([]fs.FileInfo, error) {
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
fis, err := mcli.GetSftpCli().ReadDir(path)
biz.ErrIsNilAppendErr(err, "读取目录失败: %s")
return fis
_, sftpCli, err := m.GetMachineSftpCli(fid, path)
if err != nil {
return nil, err
}
return sftpCli.ReadDir(path)
}
func (m *machineFileAppImpl) GetDirSize(fid uint64, path string) string {
mcli := m.GetMachineCli(fid, path)
func (m *machineFileAppImpl) GetDirSize(fid uint64, path string) (string, error) {
mcli, err := m.GetMachineCli(fid, path)
if err != nil {
return "", err
}
res, err := mcli.Run(fmt.Sprintf("du -sh %s", path))
if err != nil {
// 若存在目录为空,则可能会返回如下内容。最后一行即为真正目录内容所占磁盘空间大小
@@ -134,56 +142,74 @@ func (m *machineFileAppImpl) GetDirSize(fid uint64, path string) string {
//du: cannot access /proc/19087/fdinfo/3: No such file or directory\n
//18G /\n
if res == "" {
panic(biz.NewBizErr(fmt.Sprintf("获取目录大小失败: %s", err.Error())))
return "", errorx.NewBiz("获取目录大小失败: %s", err.Error())
}
strs := strings.Split(res, "\n")
res = strs[len(strs)-2]
if !strings.Contains(res, "\t") {
panic(biz.NewBizErr(res))
return "", errorx.NewBiz(res)
}
}
// 返回 32K\t/tmp\n
return strings.Split(res, "\t")[0]
return strings.Split(res, "\t")[0], nil
}
func (m *machineFileAppImpl) FileStat(fid uint64, path string) string {
mcli := m.GetMachineCli(fid, path)
res, err := mcli.Run(fmt.Sprintf("stat -L %s", path))
biz.ErrIsNil(err, res)
return res
func (m *machineFileAppImpl) FileStat(fid uint64, path string) (string, error) {
mcli, err := m.GetMachineCli(fid, path)
if err != nil {
return "", err
}
return mcli.Run(fmt.Sprintf("stat -L %s", path))
}
func (m *machineFileAppImpl) MkDir(fid uint64, path string) (*machine.Info, error) {
mcli := m.GetMachineCli(fid, path)
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
err := mcli.GetSftpCli().MkdirAll(path)
return mcli.GetMachine(), err
mi, sftpCli, err := m.GetMachineSftpCli(fid, path)
if err != nil {
return nil, err
}
sftpCli.MkdirAll(path)
return mi, err
}
func (m *machineFileAppImpl) CreateFile(fid uint64, path string) (*machine.Info, error) {
mcli := m.GetMachineCli(fid, path)
file, err := mcli.GetSftpCli().Create(path)
biz.ErrIsNilAppendErr(err, "创建文件失败: %s")
mi, sftpCli, err := m.GetMachineSftpCli(fid, path)
if err != nil {
return nil, err
}
file, err := sftpCli.Create(path)
if err != nil {
return nil, errorx.NewBiz("创建文件失败: %s", err.Error())
}
defer file.Close()
return mcli.GetMachine(), err
return mi, err
}
func (m *machineFileAppImpl) ReadFile(fileId uint64, path string) (*sftp.File, *machine.Info, error) {
mcli := m.GetMachineCli(fileId, path)
mi, sftpCli, err := m.GetMachineSftpCli(fileId, path)
if err != nil {
return nil, nil, err
}
// 读取文件内容
fc, err := mcli.GetSftpCli().Open(path)
return fc, mcli.GetMachine(), err
fc, err := sftpCli.Open(path)
return fc, mi, err
}
// 写文件内容
func (m *machineFileAppImpl) WriteFileContent(fileId uint64, path string, content []byte) (*machine.Info, error) {
mcli := m.GetMachineCli(fileId, path)
mi := mcli.GetMachine()
f, err := mcli.GetSftpCli().OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE|os.O_RDWR)
mi, sftpCli, err := m.GetMachineSftpCli(fileId, path)
if err != nil {
return nil, err
}
f, err := sftpCli.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE|os.O_RDWR)
if err != nil {
return mi, err
}
@@ -194,13 +220,16 @@ func (m *machineFileAppImpl) WriteFileContent(fileId uint64, path string, conten
// 上传文件
func (m *machineFileAppImpl) UploadFile(fileId uint64, path, filename string, reader io.Reader) (*machine.Info, error) {
mcli := m.GetMachineCli(fileId, path)
mi := mcli.GetMachine()
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
createfile, err := mcli.GetSftpCli().Create(path + filename)
mi, sftpCli, err := m.GetMachineSftpCli(fileId, path)
if err != nil {
return nil, err
}
createfile, err := sftpCli.Create(path + filename)
if err != nil {
return mi, err
}
@@ -211,7 +240,10 @@ func (m *machineFileAppImpl) UploadFile(fileId uint64, path, filename string, re
// 删除文件
func (m *machineFileAppImpl) RemoveFile(fileId uint64, path ...string) (*machine.Info, error) {
mcli := m.GetMachineCli(fileId, path...)
mcli, err := m.GetMachineCli(fileId, path...)
if err != nil {
return nil, err
}
minfo := mcli.GetMachine()
// 优先使用命令删除速度快sftp需要递归遍历删除子文件等
@@ -221,7 +253,11 @@ func (m *machineFileAppImpl) RemoveFile(fileId uint64, path ...string) (*machine
}
logx.Errorf("使用命令rm删除文件失败: %s", res)
sftpCli := mcli.GetSftpCli()
sftpCli, err := mcli.GetSftpCli()
if err != nil {
return minfo, err
}
for _, p := range path {
err = sftpCli.RemoveAll(p)
if err != nil {
@@ -232,7 +268,11 @@ func (m *machineFileAppImpl) RemoveFile(fileId uint64, path ...string) (*machine
}
func (m *machineFileAppImpl) Copy(fileId uint64, toPath string, paths ...string) (*machine.Info, error) {
mcli := m.GetMachineCli(fileId, paths...)
mcli, err := m.GetMachineCli(fileId, paths...)
if err != nil {
return nil, err
}
mi := mcli.GetMachine()
res, err := mcli.Run(fmt.Sprintf("cp -r %s %s", strings.Join(paths, " "), toPath))
if err != nil {
@@ -242,28 +282,54 @@ func (m *machineFileAppImpl) Copy(fileId uint64, toPath string, paths ...string)
}
func (m *machineFileAppImpl) Mv(fileId uint64, toPath string, paths ...string) (*machine.Info, error) {
mcli := m.GetMachineCli(fileId, paths...)
mcli, err := m.GetMachineCli(fileId, paths...)
if err != nil {
return nil, err
}
mi := mcli.GetMachine()
res, err := mcli.Run(fmt.Sprintf("mv %s %s", strings.Join(paths, " "), toPath))
if err != nil {
return mi, errors.New(res)
return mi, errorx.NewBiz(res)
}
return mi, err
}
func (m *machineFileAppImpl) Rename(fileId uint64, oldname string, newname string) (*machine.Info, error) {
mcli := m.GetMachineCli(fileId, newname)
return mcli.GetMachine(), mcli.GetSftpCli().Rename(oldname, newname)
mi, sftpCli, err := m.GetMachineSftpCli(fileId, newname)
if err != nil {
return nil, err
}
return mi, sftpCli.Rename(oldname, newname)
}
// 获取文件机器cli
func (m *machineFileAppImpl) GetMachineCli(fid uint64, inputPath ...string) *machine.Cli {
biz.IsTrue(fid != 0, "文件id不能为空")
func (m *machineFileAppImpl) GetMachineCli(fid uint64, inputPath ...string) (*machine.Cli, error) {
mf := m.GetById(fid)
biz.NotNil(mf, "文件不存在")
if mf == nil {
return nil, errorx.NewBiz("文件不存在")
}
for _, path := range inputPath {
// 接口传入的地址需为配置路径的子路径
biz.IsTrue(strings.HasPrefix(path, mf.Path), "无权访问该目录或文件: %s", path)
if !strings.HasPrefix(path, mf.Path) {
return nil, errorx.NewBiz("无权访问该目录或文件: %s", path)
}
}
return GetMachineApp().GetCli(mf.MachineId)
return m.machineApp.GetCli(mf.MachineId)
}
// 获取文件机器 sftp cli
func (m *machineFileAppImpl) GetMachineSftpCli(fid uint64, inputPath ...string) (*machine.Info, *sftp.Client, error) {
mcli, err := m.GetMachineCli(fid, inputPath...)
if err != nil {
return nil, nil, err
}
sftpCli, err := mcli.GetSftpCli()
if err != nil {
return nil, nil, err
}
return mcli.GetMachine(), sftpCli, nil
}

View File

@@ -3,68 +3,58 @@ package application
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/base"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/model"
)
type MachineScript interface {
base.App[*entity.MachineScript]
// 分页获取机器脚本信息列表
GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
// 根据条件获取
GetMachineScript(condition *entity.MachineScript, cols ...string) error
// 根据id获取
GetById(id uint64, cols ...string) *entity.MachineScript
Save(entity *entity.MachineScript)
Save(entity *entity.MachineScript) error
Delete(id uint64)
}
func newMachineScriptApp(machineScriptRepo repository.MachineScript, machineRepo repository.Machine) MachineScript {
return &machineScriptAppImpl{machineRepo: machineRepo, machineScriptRepo: machineScriptRepo}
func newMachineScriptApp(machineScriptRepo repository.MachineScript, machineApp Machine) MachineScript {
app := &machineScriptAppImpl{machineApp: machineApp}
app.Repo = machineScriptRepo
return app
}
type machineScriptAppImpl struct {
machineScriptRepo repository.MachineScript
machineRepo repository.Machine
base.AppImpl[*entity.MachineScript, repository.MachineScript]
machineApp Machine
}
const Common_Script_Machine_Id = 9999999
// 分页获取机器脚本信息列表
func (m *machineScriptAppImpl) GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return m.machineScriptRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
// 根据条件获取
func (m *machineScriptAppImpl) GetMachineScript(condition *entity.MachineScript, cols ...string) error {
return m.machineScriptRepo.GetMachineScript(condition, cols...)
}
// 根据id获取
func (m *machineScriptAppImpl) GetById(id uint64, cols ...string) *entity.MachineScript {
return m.machineScriptRepo.GetById(id, cols...)
func (m *machineScriptAppImpl) GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return m.GetRepo().GetPageList(condition, pageParam, toEntity, orderBy...)
}
// 保存机器脚本
func (m *machineScriptAppImpl) Save(entity *entity.MachineScript) {
func (m *machineScriptAppImpl) Save(ms *entity.MachineScript) error {
// 如果机器id不为公共脚本id则校验机器是否存在
if machineId := entity.MachineId; machineId != Common_Script_Machine_Id {
biz.NotNil(m.machineRepo.GetById(machineId, "Name"), "该机器不存在")
if machineId := ms.MachineId; machineId != Common_Script_Machine_Id {
_, err := m.machineApp.GetById(new(entity.Machine), machineId, "Name")
if err != nil {
return errorx.NewBiz("该机器不存在")
}
}
if entity.Id != 0 {
gormx.UpdateById(entity)
} else {
gormx.Insert(entity)
if ms.Id != 0 {
return m.UpdateById(ms)
}
return m.Insert(ms)
}
// 根据id删除
func (m *machineScriptAppImpl) Delete(id uint64) {
m.machineScriptRepo.Delete(id)
m.DeleteById(id)
}

View File

@@ -2,21 +2,14 @@ package repository
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/base"
"mayfly-go/pkg/model"
)
type AuthCert interface {
GetPageList(condition *entity.AuthCertQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
base.Repo[*entity.AuthCert]
Insert(ac *entity.AuthCert)
GetPageList(condition *entity.AuthCertQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
Update(ac *entity.AuthCert)
GetById(id uint64) *entity.AuthCert
GetByIds(ids ...uint64) []*entity.AuthCert
GetByCondition(condition *entity.AuthCert, cols ...string) error
DeleteById(id uint64)
// GetByIds(ids ...uint64) []*entity.AuthCert
}

View File

@@ -3,22 +3,15 @@ package repository
import (
"mayfly-go/internal/machine/api/vo"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/base"
"mayfly-go/pkg/model"
)
type Machine interface {
base.Repo[*entity.Machine]
// 分页获取机器信息列表
GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) *model.PageResult[*[]*vo.MachineVO]
GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) (*model.PageResult[*[]*vo.MachineVO], error)
Count(condition *entity.MachineQuery) int64
// 根据条件获取账号信息
GetMachine(condition *entity.Machine, cols ...string) error
// 根据id获取
GetById(id uint64, cols ...string) *entity.Machine
Create(entity *entity.Machine)
UpdateById(entity *entity.Machine)
}

View File

@@ -2,41 +2,28 @@ package repository
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/base"
"mayfly-go/pkg/model"
)
type MachineCronJob interface {
GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
base.Repo[*entity.MachineCronJob]
// 根据条件获取
GetBy(condition *entity.MachineCronJob, cols ...string) error
// 根据id获取
GetById(id uint64, cols ...string) *entity.MachineCronJob
Delete(id uint64)
Insert(entity *entity.MachineCronJob)
UpdateById(entity *entity.MachineCronJob)
GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}
type MachineCronJobRelate interface {
base.Repo[*entity.MachineCronJobRelate]
GetList(condition *entity.MachineCronJobRelate) []entity.MachineCronJobRelate
GetMachineIds(cronJobId uint64) []uint64
GetCronJobIds(machineId uint64) []uint64
Delete(condition *entity.MachineCronJobRelate)
BatchInsert(mcjrs []*entity.MachineCronJobRelate)
}
type MachineCronJobExec interface {
GetPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
base.Repo[*entity.MachineCronJobExec]
Insert(mcje *entity.MachineCronJobExec)
Delete(m *entity.MachineCronJobExec)
GetPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}

View File

@@ -7,7 +7,7 @@ import (
type MachineFile interface {
// 分页获取机器脚本信息列表
GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
// 根据条件获取
GetMachineFile(condition *entity.MachineFile, cols ...string) error
@@ -15,9 +15,9 @@ type MachineFile interface {
// 根据id获取
GetById(id uint64, cols ...string) *entity.MachineFile
Delete(id uint64)
Delete(id uint64) error
Create(entity *entity.MachineFile)
Create(entity *entity.MachineFile) error
UpdateById(entity *entity.MachineFile)
UpdateById(entity *entity.MachineFile) error
}

View File

@@ -2,22 +2,13 @@ package repository
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/base"
"mayfly-go/pkg/model"
)
type MachineScript interface {
base.Repo[*entity.MachineScript]
// 分页获取机器脚本信息列表
GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
// 根据条件获取
GetMachineScript(condition *entity.MachineScript, cols ...string) error
// 根据id获取
GetById(id uint64, cols ...string) *entity.MachineScript
Delete(id uint64)
Create(entity *entity.MachineScript)
UpdateById(entity *entity.MachineScript)
GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}

View File

@@ -1,12 +1,11 @@
package machine
import (
"errors"
"fmt"
"mayfly-go/internal/common/consts"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/cache"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
"net"
"time"
@@ -87,10 +86,10 @@ func (c *Cli) Close() {
}
// 获取sftp client
func (c *Cli) GetSftpCli() *sftp.Client {
func (c *Cli) GetSftpCli() (*sftp.Client, error) {
if c.client == nil {
if err := c.connect(); err != nil {
panic(biz.NewBizErr("连接ssh失败" + err.Error()))
return nil, errorx.NewBiz("连接ssh失败: %s", err.Error())
}
}
sftpclient := c.sftpClient
@@ -98,13 +97,13 @@ func (c *Cli) GetSftpCli() *sftp.Client {
if sftpclient == nil {
sc, serr := sftp.NewClient(c.client)
if serr != nil {
panic(biz.NewBizErr("获取sftp client失败" + serr.Error()))
return nil, errorx.NewBiz("获取sftp client失败: %s", serr.Error())
}
sftpclient = sc
c.sftpClient = sftpclient
}
return sftpclient
return sftpclient, nil
}
// 获取session
@@ -172,15 +171,19 @@ func DeleteCli(id uint64) {
}
// 从缓存中获取客户端信息,不存在则回调获取机器信息函数,并新建
func GetCli(machineId uint64, getMachine func(uint64) *Info) (*Cli, error) {
func GetCli(machineId uint64, getMachine func(uint64) (*Info, error)) (*Cli, error) {
if load, ok := cliCache.Get(machineId); ok {
return load.(*Cli), nil
}
me := getMachine(machineId)
err := IfUseSshTunnelChangeIpPort(me, getMachine)
me, err := getMachine(machineId)
if err != nil {
return nil, fmt.Errorf("ssh隧道连接失败: %s", err.Error())
return nil, err
}
err = IfUseSshTunnelChangeIpPort(me, getMachine)
if err != nil {
return nil, errorx.NewBiz("ssh隧道连接失败: %s", err.Error())
}
c, err := newClient(me)
if err != nil {
@@ -194,7 +197,7 @@ func GetCli(machineId uint64, getMachine func(uint64) *Info) (*Cli, error) {
}
// 测试连接使用传值的方式而非引用。因为如果使用了ssh隧道则ip和端口会变为本地映射地址与端口
func TestConn(me Info, getSshTunnelMachine func(uint64) *Info) error {
func TestConn(me Info, getSshTunnelMachine func(uint64) (*Info, error)) error {
originId := me.Id
if originId == 0 {
// 随机设置一个ip如果使用了隧道则用于临时保存隧道
@@ -202,7 +205,9 @@ func TestConn(me Info, getSshTunnelMachine func(uint64) *Info) error {
}
err := IfUseSshTunnelChangeIpPort(&me, getSshTunnelMachine)
biz.ErrIsNilAppendErr(err, "ssh隧道连接失败: %s")
if err != nil {
return fmt.Errorf("ssh隧道连接失败: %s", err.Error())
}
if me.UseSshTunnel() {
defer CloseSshTunnelMachine(me.SshTunnelMachineId, me.Id)
}
@@ -215,11 +220,11 @@ func TestConn(me Info, getSshTunnelMachine func(uint64) *Info) error {
}
// 如果使用了ssh隧道则修改机器ip port为暴露的ip port
func IfUseSshTunnelChangeIpPort(me *Info, getMachine func(uint64) *Info) error {
func IfUseSshTunnelChangeIpPort(me *Info, getMachine func(uint64) (*Info, error)) error {
if !me.UseSshTunnel() {
return nil
}
sshTunnelMachine, err := GetSshTunnelMachine(me.SshTunnelMachineId, func(u uint64) *Info {
sshTunnelMachine, err := GetSshTunnelMachine(me.SshTunnelMachineId, func(u uint64) (*Info, error) {
return getMachine(u)
})
if err != nil {
@@ -272,7 +277,7 @@ func GetSshClient(m *Info) (*ssh.Client, error) {
// 根据机器信息创建客户端对象
func newClient(machine *Info) (*Cli, error) {
if machine == nil {
return nil, errors.New("机器不存在")
return nil, errorx.NewBiz("机器不存在")
}
logx.Infof("[%s]机器连接:%s:%d", machine.Name, machine.Ip, machine.Port)

View File

@@ -136,7 +136,7 @@ func (stm *SshTunnelMachine) Close() {
}
// 获取ssh隧道机器方便统一管理充当ssh隧道的机器避免创建多个ssh client
func GetSshTunnelMachine(machineId int, getMachine func(uint64) *Info) (*SshTunnelMachine, error) {
func GetSshTunnelMachine(machineId int, getMachine func(uint64) (*Info, error)) (*SshTunnelMachine, error) {
sshTunnelMachine := sshTunnelMachines[machineId]
if sshTunnelMachine != nil {
return sshTunnelMachine, nil
@@ -145,7 +145,11 @@ func GetSshTunnelMachine(machineId int, getMachine func(uint64) *Info) (*SshTunn
mutex.Lock()
defer mutex.Unlock()
me := getMachine(uint64(machineId))
me, err := getMachine(uint64(machineId))
if err != nil {
return nil, err
}
sshClient, err := GetSshClient(me)
if err != nil {
return nil, err

View File

@@ -3,49 +3,26 @@ package persistence
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
)
type authCertRepoImpl struct{}
func newAuthCertRepo() repository.AuthCert {
return new(authCertRepoImpl)
type authCertRepoImpl struct {
base.RepoImpl[*entity.AuthCert]
}
func (m *authCertRepoImpl) GetPageList(condition *entity.AuthCertQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func newAuthCertRepo() repository.AuthCert {
return &authCertRepoImpl{base.RepoImpl[*entity.AuthCert]{M: new(entity.AuthCert)}}
}
func (m *authCertRepoImpl) GetPageList(condition *entity.AuthCertQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
qd := gormx.NewQuery(new(entity.AuthCert)).WithCondModel(condition).WithOrderBy(orderBy...)
return gormx.PageQuery(qd, pageParam, toEntity)
}
func (m *authCertRepoImpl) Insert(ac *entity.AuthCert) {
biz.ErrIsNil(gormx.Insert(ac), "新增授权凭证失败")
}
func (m *authCertRepoImpl) Update(ac *entity.AuthCert) {
biz.ErrIsNil(gormx.UpdateById(ac), "更新授权凭证失败")
}
func (m *authCertRepoImpl) GetById(id uint64) *entity.AuthCert {
ac := new(entity.AuthCert)
err := gormx.GetById(ac, id)
if err != nil {
return nil
}
return ac
}
func (m *authCertRepoImpl) GetByIds(ids ...uint64) []*entity.AuthCert {
acs := new([]*entity.AuthCert)
gormx.GetByIdIn(new(entity.AuthCert), acs, ids)
return *acs
}
func (m *authCertRepoImpl) GetByCondition(condition *entity.AuthCert, cols ...string) error {
return gormx.GetBy(condition, cols...)
}
func (m *authCertRepoImpl) DeleteById(id uint64) {
gormx.DeleteById(new(entity.AuthCert), id)
}
// func (m *authCertRepoImpl) GetByIds(ids ...uint64) []*entity.AuthCert {
// acs := new([]*entity.AuthCert)
// gormx.GetByIdIn(new(entity.AuthCert), acs, ids)
// return *acs
// }

View File

@@ -4,7 +4,7 @@ import (
"mayfly-go/internal/machine/api/vo"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils/collx"
@@ -12,14 +12,16 @@ import (
"strings"
)
type machineRepoImpl struct{}
type machineRepoImpl struct {
base.RepoImpl[*entity.Machine]
}
func newMachineRepo() repository.Machine {
return new(machineRepoImpl)
return &machineRepoImpl{base.RepoImpl[*entity.Machine]{M: new(entity.Machine)}}
}
// 分页获取机器信息列表
func (m *machineRepoImpl) GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) *model.PageResult[*[]*vo.MachineVO] {
func (m *machineRepoImpl) GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) (*model.PageResult[*[]*vo.MachineVO], error) {
qd := gormx.NewQuery(new(entity.Machine)).
Like("ip", condition.Ip).
Like("name", condition.Name).
@@ -44,28 +46,5 @@ func (m *machineRepoImpl) Count(condition *entity.MachineQuery) int64 {
where["tag_id"] = condition.TagIds
}
return gormx.CountByCond(new(entity.Machine), where)
}
// 根据条件获取账号信息
func (m *machineRepoImpl) GetMachine(condition *entity.Machine, cols ...string) error {
return gormx.GetBy(condition, cols...)
}
// 根据id获取
func (m *machineRepoImpl) GetById(id uint64, cols ...string) *entity.Machine {
machine := new(entity.Machine)
if err := gormx.GetById(machine, id, cols...); err != nil {
return nil
}
return machine
}
func (m *machineRepoImpl) Create(entity *entity.Machine) {
biz.ErrIsNilAppendErr(gormx.Insert(entity), "创建机器信息失败: %s")
}
func (m *machineRepoImpl) UpdateById(entity *entity.Machine) {
biz.ErrIsNilAppendErr(gormx.UpdateById(entity), "更新机器信息失败: %s")
return m.CountByCond(where)
}

View File

@@ -3,44 +3,21 @@ package persistence
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
)
type machineCropJobRepoImpl struct{}
type machineCropJobRepoImpl struct {
base.RepoImpl[*entity.MachineCronJob]
}
func newMachineCronJobRepo() repository.MachineCronJob {
return new(machineCropJobRepoImpl)
return &machineCropJobRepoImpl{base.RepoImpl[*entity.MachineCronJob]{M: new(entity.MachineCronJob)}}
}
// 分页获取机器信息列表
func (m *machineCropJobRepoImpl) GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (m *machineCropJobRepoImpl) GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
qd := gormx.NewQuery(condition).Like("name", condition.Name).Eq("status", condition.Status).WithOrderBy(orderBy...)
return gormx.PageQuery(qd, pageParam, toEntity)
}
func (m *machineCropJobRepoImpl) GetBy(cond *entity.MachineCronJob, cols ...string) error {
return gormx.GetBy(cond, cols...)
}
func (m *machineCropJobRepoImpl) GetById(id uint64, cols ...string) *entity.MachineCronJob {
res := new(entity.MachineCronJob)
if err := gormx.GetById(res, id, cols...); err == nil {
return res
} else {
return nil
}
}
func (m *machineCropJobRepoImpl) Delete(id uint64) {
biz.ErrIsNil(gormx.DeleteById(new(entity.MachineCronJob), id), "删除失败")
}
func (m *machineCropJobRepoImpl) Insert(entity *entity.MachineCronJob) {
gormx.Insert(entity)
}
func (m *machineCropJobRepoImpl) UpdateById(entity *entity.MachineCronJob) {
gormx.UpdateById(entity)
}

View File

@@ -3,26 +3,21 @@ package persistence
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/base"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
)
type machineCropJobExecRepoImpl struct{}
type machineCropJobExecRepoImpl struct {
base.RepoImpl[*entity.MachineCronJobExec]
}
func newMachineCronJobExecRepo() repository.MachineCronJobExec {
return new(machineCropJobExecRepoImpl)
return &machineCropJobExecRepoImpl{base.RepoImpl[*entity.MachineCronJobExec]{M: new(entity.MachineCronJobExec)}}
}
// 分页获取机器信息列表
func (m *machineCropJobExecRepoImpl) GetPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (m *machineCropJobExecRepoImpl) GetPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
qd := gormx.NewQuery(condition).WithCondModel(condition).WithOrderBy(orderBy...)
return gormx.PageQuery(qd, pageParam, toEntity)
}
func (m *machineCropJobExecRepoImpl) Insert(entity *entity.MachineCronJobExec) {
gormx.Insert(entity)
}
func (m *machineCropJobExecRepoImpl) Delete(mcje *entity.MachineCronJobExec) {
gormx.DeleteByCondition(mcje)
}

View File

@@ -3,24 +3,27 @@ package persistence
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/base"
"mayfly-go/pkg/gormx"
)
type machineCropJobRelateRepoImpl struct{}
type machineCropJobRelateRepoImpl struct {
base.RepoImpl[*entity.MachineCronJobRelate]
}
func newMachineCropJobRelateRepo() repository.MachineCronJobRelate {
return new(machineCropJobRelateRepoImpl)
return &machineCropJobRelateRepoImpl{base.RepoImpl[*entity.MachineCronJobRelate]{M: new(entity.MachineCronJobRelate)}}
}
func (m *machineCropJobRelateRepoImpl) GetList(condition *entity.MachineCronJobRelate) []entity.MachineCronJobRelate {
list := new([]entity.MachineCronJobRelate)
gormx.ListByOrder(condition, list)
m.ListByCond(condition, list)
return *list
}
func (m *machineCropJobRelateRepoImpl) GetMachineIds(cronJobId uint64) []uint64 {
var machineIds []uint64
gormx.ListBy(&entity.MachineCronJobRelate{CronJobId: cronJobId}, &machineIds, "machine_id")
m.ListByCond(&entity.MachineCronJobRelate{CronJobId: cronJobId}, &machineIds, "machine_id")
return machineIds
}
@@ -29,11 +32,3 @@ func (m *machineCropJobRelateRepoImpl) GetCronJobIds(machineId uint64) []uint64
gormx.ListBy(&entity.MachineCronJobRelate{MachineId: machineId}, &cronJobIds, "cron_job_id")
return cronJobIds
}
func (m *machineCropJobRelateRepoImpl) Delete(condition *entity.MachineCronJobRelate) {
gormx.DeleteByCondition(condition)
}
func (m *machineCropJobRelateRepoImpl) BatchInsert(mcjrs []*entity.MachineCronJobRelate) {
gormx.BatchInsert(mcjrs)
}

View File

@@ -3,7 +3,6 @@ package persistence
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
)
@@ -15,7 +14,7 @@ func newMachineFileRepo() repository.MachineFile {
}
// 分页获取机器文件信息列表
func (m *machineFileRepoImpl) GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (m *machineFileRepoImpl) GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
qd := gormx.NewQuery(condition).WithCondModel(condition).WithOrderBy(orderBy...)
return gormx.PageQuery(qd, pageParam, toEntity)
}
@@ -35,14 +34,14 @@ func (m *machineFileRepoImpl) GetById(id uint64, cols ...string) *entity.Machine
}
// 根据id获取
func (m *machineFileRepoImpl) Delete(id uint64) {
biz.ErrIsNil(gormx.DeleteById(new(entity.MachineFile), id), "删除失败")
func (m *machineFileRepoImpl) Delete(id uint64) error {
return gormx.DeleteById(new(entity.MachineFile), id)
}
func (m *machineFileRepoImpl) Create(entity *entity.MachineFile) {
biz.ErrIsNil(gormx.Insert(entity), "新增机器文件配置失败")
func (m *machineFileRepoImpl) Create(entity *entity.MachineFile) error {
return gormx.Insert(entity)
}
func (m *machineFileRepoImpl) UpdateById(entity *entity.MachineFile) {
biz.ErrIsNil(gormx.UpdateById(entity), "更新机器文件失败")
func (m *machineFileRepoImpl) UpdateById(entity *entity.MachineFile) error {
return gormx.UpdateById(entity)
}

View File

@@ -3,47 +3,21 @@ package persistence
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
)
type machineScriptRepoImpl struct{}
type machineScriptRepoImpl struct {
base.RepoImpl[*entity.MachineScript]
}
func newMachineScriptRepo() repository.MachineScript {
return new(machineScriptRepoImpl)
return &machineScriptRepoImpl{base.RepoImpl[*entity.MachineScript]{M: new(entity.MachineScript)}}
}
// 分页获取机器信息列表
func (m *machineScriptRepoImpl) GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (m *machineScriptRepoImpl) GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
qd := gormx.NewQuery(condition).WithCondModel(condition).WithOrderBy(orderBy...)
return gormx.PageQuery(qd, pageParam, toEntity)
}
// 根据条件获取账号信息
func (m *machineScriptRepoImpl) GetMachineScript(condition *entity.MachineScript, cols ...string) error {
return gormx.GetBy(condition, cols...)
}
// 根据id获取
func (m *machineScriptRepoImpl) GetById(id uint64, cols ...string) *entity.MachineScript {
ms := new(entity.MachineScript)
if err := gormx.GetById(ms, id, cols...); err != nil {
return nil
}
return ms
}
// 根据id获取
func (m *machineScriptRepoImpl) Delete(id uint64) {
biz.ErrIsNil(gormx.DeleteById(new(entity.MachineScript), id), "删除失败")
}
func (m *machineScriptRepoImpl) Create(entity *entity.MachineScript) {
gormx.Insert(entity)
}
func (m *machineScriptRepoImpl) UpdateById(entity *entity.MachineScript) {
gormx.UpdateById(entity)
}