Skip to content

Commit dee79d7

Browse files
committed
refactor: 解耦script和task
1 parent 9da8104 commit dee79d7

File tree

11 files changed

+87
-87
lines changed

11 files changed

+87
-87
lines changed

api/router.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"tdp-cloud/api/domain"
88
"tdp-cloud/api/machine"
99
"tdp-cloud/api/passport"
10+
"tdp-cloud/api/script"
1011
"tdp-cloud/api/sshkey"
11-
"tdp-cloud/api/task_history"
12-
"tdp-cloud/api/task_script"
12+
"tdp-cloud/api/taskline"
1313
"tdp-cloud/api/terminal"
1414
"tdp-cloud/api/user"
1515
"tdp-cloud/api/vendor"
@@ -34,9 +34,9 @@ func Router(engine *gin.Engine) {
3434
domain.Router(api)
3535
machine.Router(api)
3636
passport.Router(api)
37+
script.Router(api)
3738
sshkey.Router(api)
38-
task_history.Router(api)
39-
task_script.Router(api)
39+
taskline.Router(api)
4040
user.Router(api)
4141
vendor.Router(api)
4242
workhub.Router(api)

api/task_script/controller.go renamed to api/script/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package task_script
1+
package script
22

33
import (
44
"github.com/gin-gonic/gin"
55
"github.com/spf13/cast"
66

7-
script "tdp-cloud/module/dborm/task_script"
7+
"tdp-cloud/module/dborm/script"
88
)
99

1010
// 脚本列表

api/task_script/router.go renamed to api/script/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package task_script
1+
package script
22

33
import (
44
"github.com/gin-gonic/gin"

api/task_history/router.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

api/task_history/controller.go renamed to api/taskline/controller.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package task_history
1+
package taskline
22

33
import (
44
"github.com/gin-gonic/gin"
55
"github.com/spf13/cast"
66

7-
history "tdp-cloud/module/dborm/task_history"
7+
taskline "tdp-cloud/module/dborm/taskline"
88
)
99

1010
// 记录列表
@@ -13,7 +13,7 @@ func list(c *gin.Context) {
1313

1414
userId := c.GetUint("UserId")
1515

16-
if lst, err := history.FetchAll(userId); err == nil {
16+
if lst, err := taskline.FetchAll(userId); err == nil {
1717
c.Set("Payload", gin.H{"Datasets": lst})
1818
} else {
1919
c.Set("Error", err)
@@ -28,7 +28,7 @@ func detail(c *gin.Context) {
2828
userId := c.GetUint("UserId")
2929
id := cast.ToUint(c.Param("id"))
3030

31-
if res, err := history.Fetch(id, userId); err == nil {
31+
if res, err := taskline.Fetch(id, userId); err == nil {
3232
c.Set("Payload", res)
3333
} else {
3434
c.Set("Error", err)
@@ -40,7 +40,7 @@ func detail(c *gin.Context) {
4040

4141
func create(c *gin.Context) {
4242

43-
var rq *history.CreateParam
43+
var rq *taskline.CreateParam
4444

4545
if err := c.ShouldBind(&rq); err != nil {
4646
c.Set("Error", err)
@@ -49,7 +49,7 @@ func create(c *gin.Context) {
4949

5050
rq.UserId = c.GetUint("UserId")
5151

52-
if id, err := history.Create(rq); err == nil {
52+
if id, err := taskline.Create(rq); err == nil {
5353
c.Set("Message", "添加成功")
5454
c.Set("Payload", gin.H{"Id": id})
5555
} else {
@@ -62,7 +62,7 @@ func create(c *gin.Context) {
6262

6363
func update(c *gin.Context) {
6464

65-
var rq *history.UpdateParam
65+
var rq *taskline.UpdateParam
6666

6767
if err := c.ShouldBind(&rq); err != nil {
6868
c.Set("Error", err)
@@ -71,7 +71,7 @@ func update(c *gin.Context) {
7171

7272
rq.UserId = c.GetUint("UserId")
7373

74-
if err := history.Update(rq); err == nil {
74+
if err := taskline.Update(rq); err == nil {
7575
c.Set("Message", "更新成功")
7676
} else {
7777
c.Set("Error", err)
@@ -86,7 +86,7 @@ func delete(c *gin.Context) {
8686
userId := c.GetUint("UserId")
8787
id := cast.ToUint(c.Param("id"))
8888

89-
if err := history.Delete(id, userId); err == nil {
89+
if err := taskline.Delete(id, userId); err == nil {
9090
c.Set("Message", "删除成功")
9191
} else {
9292
c.Set("Error", err)

api/taskline/router.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package taskline
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
6+
"tdp-cloud/module/midware"
7+
)
8+
9+
func Router(api *gin.RouterGroup) {
10+
11+
rg := api.Group("/task")
12+
13+
rg.Use(midware.AuthGuard())
14+
15+
{
16+
rg.GET("/taskline", list)
17+
rg.POST("/taskline", create)
18+
rg.GET("/taskline/:id", detail)
19+
rg.PATCH("/taskline/:id", update)
20+
rg.DELETE("/taskline/:id", delete)
21+
}
22+
23+
}

module/dborm/task_script/model.go renamed to module/dborm/script/model.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package task_script
1+
package script
22

33
import (
44
"tdp-cloud/module/dborm"
@@ -19,7 +19,7 @@ type CreateParam struct {
1919

2020
func Create(post *CreateParam) (uint, error) {
2121

22-
item := &dborm.TaskScript{
22+
item := &dborm.Script{
2323
UserId: post.UserId,
2424
Name: post.Name,
2525
CommandType: post.CommandType,
@@ -53,8 +53,8 @@ type UpdateParam struct {
5353
func Update(post *UpdateParam) error {
5454

5555
result := dborm.Db.
56-
Where(&dborm.TaskScript{Id: post.Id, UserId: post.UserId}).
57-
Updates(dborm.TaskScript{
56+
Where(&dborm.Script{Id: post.Id, UserId: post.UserId}).
57+
Updates(dborm.Script{
5858
Name: post.Name,
5959
CommandType: post.CommandType,
6060
Username: post.Username,
@@ -70,23 +70,23 @@ func Update(post *UpdateParam) error {
7070

7171
// 获取脚本列表
7272

73-
func FetchAll(userId uint) ([]*dborm.TaskScript, error) {
73+
func FetchAll(userId uint) ([]*dborm.Script, error) {
7474

75-
var items []*dborm.TaskScript
75+
var items []*dborm.Script
7676

77-
result := dborm.Db.Where(&dborm.TaskScript{UserId: userId}).Find(&items)
77+
result := dborm.Db.Where(&dborm.Script{UserId: userId}).Find(&items)
7878

7979
return items, result.Error
8080

8181
}
8282

8383
// 获取脚本
8484

85-
func Fetch(id, userId uint) (*dborm.TaskScript, error) {
85+
func Fetch(id, userId uint) (*dborm.Script, error) {
8686

87-
var item *dborm.TaskScript
87+
var item *dborm.Script
8888

89-
result := dborm.Db.Where(&dborm.TaskScript{Id: id, UserId: userId}).Find(&item)
89+
result := dborm.Db.Where(&dborm.Script{Id: id, UserId: userId}).Find(&item)
9090

9191
return item, result.Error
9292

@@ -96,9 +96,9 @@ func Fetch(id, userId uint) (*dborm.TaskScript, error) {
9696

9797
func Delete(id, userId uint) error {
9898

99-
var item *dborm.TaskScript
99+
var item *dborm.Script
100100

101-
result := dborm.Db.Where(&dborm.TaskScript{Id: id, UserId: userId}).Delete(&item)
101+
result := dborm.Db.Where(&dborm.Script{Id: id, UserId: userId}).Delete(&item)
102102

103103
return result.Error
104104

module/dborm/table.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ type Session struct {
6060
UpdatedAt int64
6161
}
6262

63+
// 命令脚本
64+
65+
type Script struct {
66+
Id uint `gorm:"primaryKey"`
67+
UserId uint `gorm:"index"`
68+
Name string
69+
CommandType string
70+
Username string
71+
WorkDirectory string
72+
Content string
73+
Description string
74+
Timeout uint
75+
CreatedAt int64
76+
UpdatedAt int64
77+
}
78+
6379
// SSH 密钥
6480

6581
type Sshkey struct {
@@ -74,7 +90,7 @@ type Sshkey struct {
7490

7591
// 任务记录
7692

77-
type TaskHistory struct {
93+
type Taskline struct {
7894
Id uint `gorm:"primaryKey"`
7995
UserId uint `gorm:"index"`
8096
Subject string
@@ -87,22 +103,6 @@ type TaskHistory struct {
87103
UpdatedAt int64
88104
}
89105

90-
// 任务脚本
91-
92-
type TaskScript struct {
93-
Id uint `gorm:"primaryKey"`
94-
UserId uint `gorm:"index"`
95-
Name string
96-
CommandType string
97-
Username string
98-
WorkDirectory string
99-
Content string
100-
Description string
101-
Timeout uint
102-
CreatedAt int64
103-
UpdatedAt int64
104-
}
105-
106106
// 用户
107107

108108
type User struct {

module/dborm/task_history/model.go renamed to module/dborm/taskline/model.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package task_history
1+
package taskline
22

33
import (
44
"tdp-cloud/module/dborm"
@@ -18,7 +18,7 @@ type CreateParam struct {
1818

1919
func Create(post *CreateParam) (uint, error) {
2020

21-
item := &dborm.TaskHistory{
21+
item := &dborm.Taskline{
2222
UserId: post.UserId,
2323
Subject: post.Subject,
2424
HostName: post.HostName,
@@ -50,8 +50,8 @@ type UpdateParam struct {
5050
func Update(post *UpdateParam) error {
5151

5252
result := dborm.Db.
53-
Where(&dborm.TaskHistory{Id: post.Id, UserId: post.UserId}).
54-
Updates(dborm.TaskHistory{
53+
Where(&dborm.Taskline{Id: post.Id, UserId: post.UserId}).
54+
Updates(dborm.Taskline{
5555
Subject: post.Subject,
5656
HostName: post.HostName,
5757
WorkerId: post.WorkerId,
@@ -66,12 +66,12 @@ func Update(post *UpdateParam) error {
6666

6767
// 获取任务列表
6868

69-
func FetchAll(userId uint) ([]*dborm.TaskHistory, error) {
69+
func FetchAll(userId uint) ([]*dborm.Taskline, error) {
7070

71-
var items []*dborm.TaskHistory
71+
var items []*dborm.Taskline
7272

7373
result := dborm.Db.
74-
Where(&dborm.TaskHistory{UserId: userId}).
74+
Where(&dborm.Taskline{UserId: userId}).
7575
Limit(50).Order("id DESC").
7676
Find(&items)
7777

@@ -81,11 +81,11 @@ func FetchAll(userId uint) ([]*dborm.TaskHistory, error) {
8181

8282
// 获取任务
8383

84-
func Fetch(id, userId uint) (*dborm.TaskHistory, error) {
84+
func Fetch(id, userId uint) (*dborm.Taskline, error) {
8585

86-
var item *dborm.TaskHistory
86+
var item *dborm.Taskline
8787

88-
result := dborm.Db.Where(&dborm.TaskHistory{Id: id, UserId: userId}).First(&item)
88+
result := dborm.Db.Where(&dborm.Taskline{Id: id, UserId: userId}).First(&item)
8989

9090
return item, result.Error
9191

@@ -95,9 +95,9 @@ func Fetch(id, userId uint) (*dborm.TaskHistory, error) {
9595

9696
func Delete(id, userId uint) error {
9797

98-
var item *dborm.TaskHistory
98+
var item *dborm.Taskline
9999

100-
result := dborm.Db.Where(&dborm.TaskHistory{Id: id, UserId: userId}).Delete(&item)
100+
result := dborm.Db.Where(&dborm.Taskline{Id: id, UserId: userId}).Delete(&item)
101101

102102
return result.Error
103103

module/migrator/v100000.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ func v100000() error {
1212
&dborm.Machine{},
1313
&dborm.Session{},
1414
&dborm.Sshkey{},
15-
&dborm.TaskHistory{},
16-
&dborm.TaskScript{},
15+
&dborm.Taskline{},
16+
&dborm.Script{},
1717
&dborm.User{},
1818
&dborm.Vendor{},
1919
)

0 commit comments

Comments
 (0)