U Optimized code

This commit is contained in:
张臻 2020-08-19 10:22:43 +08:00
parent 9843bc4fd4
commit ea845df302
11 changed files with 44 additions and 99 deletions

View File

@ -283,19 +283,11 @@ func (crontab Crontab) Remove(w http.ResponseWriter, gp *core.Goploy) *core.Resp
}
}
err := model.Crontab{
ID: reqData.ID,
}.DeleteRow()
if err != nil {
if err := (model.Crontab{ID: reqData.ID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
err = model.CrontabServer{
CrontabID: reqData.ID,
}.DeleteByCrontabID()
if err != nil {
if err := (model.CrontabServer{CrontabID: reqData.ID}).DeleteByCrontabID(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -353,11 +345,7 @@ func (crontab Crontab) RemoveCrontabServer(w http.ResponseWriter, gp *core.Goplo
return &core.Response{Code: core.Error, Message: err.Error()}
}
err = model.CrontabServer{
ID: reqData.CrontabServerID,
}.DeleteRow()
if err != nil {
if err := (model.CrontabServer{ID: reqData.CrontabServerID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -394,9 +382,7 @@ func addCrontab(serverID int64, command string) {
}
func deleteCrontab(serverID int64, command string) {
server, err := model.Server{
ID: serverID,
}.GetData()
server, err := model.Server{ID: serverID}.GetData()
if err != nil {
core.Log(core.TRACE, "serverID:"+strconv.FormatUint(uint64(serverID), 10)+" get server fail, detail:"+err.Error())

View File

@ -2,7 +2,6 @@ package controller
import (
"database/sql"
"encoding/json"
"github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model"
"github.com/zhenorzz/goploy/service"
@ -37,7 +36,6 @@ func (deploy Deploy) GetList(w http.ResponseWriter, gp *core.Goploy) *core.Respo
// GetPreview deploy detail
func (deploy Deploy) GetPreview(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type RespData struct {
GitTraceList model.PublishTraces `json:"gitTraceList"`
Pagination model.Pagination `json:"pagination"`
@ -74,7 +72,6 @@ func (deploy Deploy) GetPreview(w http.ResponseWriter, gp *core.Goploy) *core.Re
// GetDetail deploy detail
func (deploy Deploy) GetDetail(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type RespData struct {
PublishTraceList model.PublishTraces `json:"publishTraceList"`
}
@ -131,11 +128,11 @@ func (deploy Deploy) GetCommitList(w http.ResponseWriter, gp *core.Goploy) *core
// Publish the project
func (deploy Deploy) Publish(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type ReqData struct {
ProjectID int64 `json:"projectId"`
ProjectID int64 `json:"projectId" validate:"gt=0"`
Commit string `json:"commit"`
}
var reqData ReqData
if err := json.Unmarshal(gp.Body, &reqData); err != nil {
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -178,11 +175,10 @@ func (deploy Deploy) Webhook(w http.ResponseWriter, gp *core.Goploy) *core.Respo
projectName := gp.URLQuery.Get("project_name")
// other event is blocked in deployMiddleware
type ReqData struct {
Ref string `json:"ref"`
Ref string `json:"ref" validate:"required"`
}
var reqData ReqData
if err := json.Unmarshal(gp.Body, &reqData); err != nil {
core.Log(core.ERROR, "json unmarshal error, err:"+err.Error())
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
branch := strings.Split(reqData.Ref, "/")[2]

View File

@ -86,9 +86,7 @@ func (namespace Namespace) Add(_ http.ResponseWriter, gp *core.Goploy) *core.Res
return &core.Response{Code: core.Error, Message: err.Error()}
}
err = model.NamespaceUser{NamespaceID: id}.AddAdminByNamespaceID()
if err != nil {
if err := (model.NamespaceUser{NamespaceID: id}).AddAdminByNamespaceID(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -161,11 +159,8 @@ func (namespace Namespace) RemoveUser(_ http.ResponseWriter, gp *core.Goploy) *c
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
err := model.NamespaceUser{
ID: reqData.NamespaceUserID,
}.DeleteRow()
if err != nil {
if err := (model.NamespaceUser{ID: reqData.NamespaceUserID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}

View File

@ -3,7 +3,6 @@ package controller
import (
"bytes"
"database/sql"
"encoding/json"
"github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model"
"github.com/zhenorzz/goploy/utils"
@ -113,12 +112,12 @@ func (project Project) GetBindUserList(w http.ResponseWriter, gp *core.Goploy) *
// Add project
func (project Project) Add(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type ReqData struct {
Name string `json:"name"`
URL string `json:"url"`
Path string `json:"path"`
Name string `json:"name" validate:"required"`
URL string `json:"url" validate:"required"`
Path string `json:"path" validate:"required"`
Environment string `json:"Environment" validate:"required"`
Branch string `json:"branch" validate:"required"`
SymlinkPath string `json:"symlinkPath"`
Environment string `json:"Environment"`
Branch string `json:"branch"`
AfterPullScriptMode string `json:"afterPullScriptMode"`
AfterPullScript string `json:"afterPullScript"`
AfterDeployScriptMode string `json:"afterDeployScriptMode"`
@ -131,8 +130,7 @@ func (project Project) Add(w http.ResponseWriter, gp *core.Goploy) *core.Respons
NotifyTarget string `json:"notifyTarget"`
}
var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData)
if err != nil {
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -140,7 +138,7 @@ func (project Project) Add(w http.ResponseWriter, gp *core.Goploy) *core.Respons
return &core.Response{Code: core.Error, Message: "Invalid rsync option format"}
}
_, err = model.Project{Name: reqData.Name}.GetDataByName()
_, err := model.Project{Name: reqData.Name}.GetDataByName()
if err != sql.ErrNoRows {
return &core.Response{Code: core.Error, Message: "The project name is already exist"}
}
@ -210,7 +208,7 @@ func (project Project) Add(w http.ResponseWriter, gp *core.Goploy) *core.Respons
// Edit project
func (project Project) Edit(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type ReqData struct {
ID int64 `json:"id"`
ID int64 `json:"id" validate:"gt=0"`
Name string `json:"name"`
URL string `json:"url"`
Path string `json:"path"`
@ -227,8 +225,7 @@ func (project Project) Edit(w http.ResponseWriter, gp *core.Goploy) *core.Respon
NotifyTarget string `json:"notifyTarget"`
}
var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData)
if err != nil {
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -319,11 +316,10 @@ func (project Project) Edit(w http.ResponseWriter, gp *core.Goploy) *core.Respon
// RemoveRow Project
func (project Project) Remove(w http.ResponseWriter, gp *core.Goploy) *core.Response {
type ReqData struct {
ID int64 `json:"id"`
ID int64 `json:"id" validate:"gt=0"`
}
var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData)
if err != nil {
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -332,11 +328,7 @@ func (project Project) Remove(w http.ResponseWriter, gp *core.Goploy) *core.Resp
return &core.Response{Code: core.Error, Message: err.Error()}
}
err = model.Project{
ID: reqData.ID,
}.RemoveRow()
if err != nil {
if err := (model.Project{ID: reqData.ID}).RemoveRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -355,8 +347,7 @@ func (project Project) AddServer(w http.ResponseWriter, gp *core.Goploy) *core.R
ServerIDs []int64 `json:"serverIds" validate:"required"`
}
var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData)
if err != nil {
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectID := reqData.ProjectID
@ -410,15 +401,11 @@ func (project Project) RemoveServer(w http.ResponseWriter, gp *core.Goploy) *cor
ProjectServerID int64 `json:"projectServerId" validate:"gt=0"`
}
var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData)
if err != nil {
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
err = model.ProjectServer{
ID: reqData.ProjectServerID,
}.DeleteRow()
if err != nil {
if err := (model.ProjectServer{ID: reqData.ProjectServerID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
@ -430,16 +417,12 @@ func (project Project) RemoveUser(w http.ResponseWriter, gp *core.Goploy) *core.
ProjectUserID int64 `json:"projectUserId" validate:"gt=0"`
}
var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData)
if err != nil {
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
err = model.ProjectUser{
ID: reqData.ProjectUserID,
}.DeleteRow()
if err != nil {
if err := (model.ProjectUser{ID: reqData.ProjectUserID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
@ -535,8 +518,7 @@ func (project Project) RemoveTask(w http.ResponseWriter, gp *core.Goploy) *core.
return &core.Response{Code: core.Error, Message: err.Error()}
}
err := model.ProjectTask{ID: reqData.ID}.RemoveRow()
if err != nil {
if err := (model.ProjectTask{ID: reqData.ID}).RemoveRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -547,13 +529,13 @@ func (project Project) RemoveTask(w http.ResponseWriter, gp *core.Goploy) *core.
func repoCreate(projectID int64) {
project, err := model.Project{ID: projectID}.GetData()
if err != nil {
core.Log(core.TRACE, "projectID:"+strconv.FormatInt(projectID, 10)+" 无此项目")
core.Log(core.TRACE, "The project does not exist, projectID:"+strconv.FormatInt(projectID, 10))
return
}
srcPath := core.RepositoryPath + project.Name
if _, err := os.Stat(srcPath); err != nil {
if err := os.RemoveAll(srcPath); err != nil {
core.Log(core.TRACE, "projectID:"+strconv.FormatInt(project.ID, 10)+" 项目移除失败")
core.Log(core.TRACE, "The project fail to remove, projectID:"+strconv.FormatInt(project.ID, 10))
return
}
repo := project.URL
@ -562,7 +544,7 @@ func repoCreate(projectID int64) {
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
core.Log(core.ERROR, "projectID:"+strconv.FormatInt(project.ID, 10)+" 项目初始化失败:"+err.Error())
core.Log(core.ERROR, "The project fail to initialize, projectID:"+strconv.FormatInt(project.ID, 10)+err.Error())
return
}
@ -579,7 +561,7 @@ func repoCreate(projectID int64) {
}
}
core.Log(core.TRACE, "projectID:"+strconv.FormatInt(project.ID, 10)+" 项目初始化成功")
core.Log(core.TRACE, "The project success to initialize, projectID:"+strconv.FormatInt(project.ID, 10))
}
return

View File

@ -106,8 +106,7 @@ func (server Server) Check(w http.ResponseWriter, gp *core.Goploy) *core.Respons
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
_, err := utils.ConnectSSH(reqData.Owner, "", reqData.IP, reqData.Port)
if err != nil {
if _, err := utils.ConnectSSH(reqData.Owner, "", reqData.IP, reqData.Port); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{Message: "Connected"}

View File

@ -1,7 +1,6 @@
package controller
import (
"encoding/json"
"net/http"
"github.com/zhenorzz/goploy/core"
@ -33,7 +32,6 @@ func (Template) GetTotal(_ http.ResponseWriter, gp *core.Goploy) *core.Response
Total int64 `json:"total"`
}
total, err := model.Template{}.GetTotal()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
@ -117,15 +115,11 @@ func (Template) Remove(w http.ResponseWriter, gp *core.Goploy) *core.Response {
ID int64 `json:"id" validate:"gt=0"`
}
var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData)
if err != nil {
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
err = model.Template{
ID: reqData.ID,
}.DeleteRow()
if err != nil {
if err := (model.Template{ID: reqData.ID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{Message: "删除成功"}

View File

@ -2,7 +2,6 @@ package controller
import (
"database/sql"
"encoding/json"
"github.com/patrickmn/go-cache"
"net/http"
"strconv"
@ -26,8 +25,7 @@ func (user User) Login(w http.ResponseWriter, gp *core.Goploy) *core.Response {
NamespaceList model.Namespaces `json:"namespaceList"`
}
var reqData ReqData
err := json.Unmarshal(gp.Body, &reqData)
if err != nil {
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
userData, err := model.User{Account: reqData.Account}.GetDataByAccount()
@ -233,12 +231,7 @@ func (user User) Remove(_ http.ResponseWriter, gp *core.Goploy) *core.Response {
if reqData.ID == 1 {
return &core.Response{Code: core.Error, Message: "Can not delete the super manager"}
}
err := model.User{
ID: reqData.ID,
}.RemoveRow()
if err != nil {
if err := (model.User{ID: reqData.ID}).RemoveRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
@ -263,7 +256,7 @@ func (user User) ChangePassword(_ http.ResponseWriter, gp *core.Goploy) *core.Re
return &core.Response{Code: core.Error, Message: err.Error()}
}
if err := (model.User{ID: gp.UserInfo.ID, Password: reqData.NewPassword}.UpdatePassword()); err != nil {
if err := (model.User{ID: gp.UserInfo.ID, Password: reqData.NewPassword}).UpdatePassword(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}

View File

@ -19,7 +19,7 @@ func HasPublishAuth(_ http.ResponseWriter, gp *core.Goploy) error {
return err
}
_, err := model.Project{ID: reqData.ProjectID}.GetUserProjectData(gp.UserInfo.ID)
_, err := model.Project{ID: reqData.ProjectID, UserID: gp.UserInfo.ID}.GetUserProjectData()
if err != nil {
return errors.New("no permission")
}

View File

@ -406,7 +406,7 @@ func (p Project) GetDataByName() (Project, error) {
}
// GetUserProjectData -
func (p Project) GetUserProjectData(userID int64) (Project, error) {
func (p Project) GetUserProjectData() (Project, error) {
var project Project
err := sq.
Select("project_id, project.name, publisher_id, publisher_name, project.environment, project.branch, project.last_publish_token, project.deploy_state").
@ -414,7 +414,7 @@ func (p Project) GetUserProjectData(userID int64) (Project, error) {
LeftJoin(projectTable+" ON project_user.project_id = project.id").
Where(sq.Eq{
"project_user.project_id": p.ID,
"project_user.user_id": userID,
"project_user.user_id": p.UserID,
"project.state": Enable,
}).
RunWith(DB).

View File

@ -160,7 +160,7 @@ func ConnectSSH(user, password, host string, port int) (*ssh.Session, error) {
},
}
// connet to ssh
// connect to ssh
addr = fmt.Sprintf("%s:%d", host, port)
if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {

View File

@ -27,6 +27,6 @@ const (
)
func (projectMessage ProjectMessage) canSendTo(client *Client) error {
_, err := model.Project{ID: projectMessage.ProjectID}.GetUserProjectData(client.UserInfo.ID)
_, err := model.Project{ID: projectMessage.ProjectID, UserID: client.UserInfo.ID}.GetUserProjectData()
return err
}