From e3e87a98c869671374e95b12e4c470a0a282a93a Mon Sep 17 00:00:00 2001 From: zhenorzz Date: Mon, 12 Dec 2022 09:30:49 +0800 Subject: [PATCH] F index out of range --- cmd/server/api/deploy.go | 2 +- cmd/server/api/project.go | 2 +- cmd/server/main.go | 2 +- cmd/server/task/deploy.go | 24 ++++++++++++++++++++---- config/toml.go | 3 ++- internal/repo/git.go | 26 ++++++++++++-------------- 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/cmd/server/api/deploy.go b/cmd/server/api/deploy.go index 4811d1c..7d6d858 100644 --- a/cmd/server/api/deploy.go +++ b/cmd/server/api/deploy.go @@ -478,7 +478,7 @@ func (Deploy) Rebuild(gp *server.Goploy) server.Response { scriptName := fmt.Sprintf("goploy-after-deploy-p%d-s%d.%s", project.ID, projectServer.ServerID, pkg.GetScriptExt(project.AfterDeployScriptMode)) scriptContent := project.ReplaceVars(project.AfterDeployScript) scriptContent = projectServer.ReplaceVars(scriptContent) - ioutil.WriteFile(path.Join(config.GetProjectPath(project.ID), scriptName), []byte(project.ReplaceVars(project.AfterDeployScript)), 0755) + os.WriteFile(path.Join(config.GetProjectPath(project.ID), scriptName), []byte(project.ReplaceVars(project.AfterDeployScript)), 0755) afterDeployScriptPath := path.Join(project.Path, scriptName) afterDeployCommands = append(afterDeployCommands, cmdEntity.Script(project.AfterDeployScriptMode, afterDeployScriptPath)) afterDeployCommands = append(afterDeployCommands, cmdEntity.Remove(afterDeployScriptPath)) diff --git a/cmd/server/api/project.go b/cmd/server/api/project.go index f43fcd9..76d424e 100644 --- a/cmd/server/api/project.go +++ b/cmd/server/api/project.go @@ -567,7 +567,7 @@ func (Project) UploadFile(gp *server.Goploy) server.Response { return response.JSON{Code: response.Error, Message: err.Error()} } - if err := ioutil.WriteFile(filePath, fileBytes, 0755); err != nil { + if err := os.WriteFile(filePath, fileBytes, 0755); err != nil { return response.JSON{Code: response.Error, Message: err.Error()} } diff --git a/cmd/server/main.go b/cmd/server/main.go index 58f7d07..2f5468d 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -78,7 +78,7 @@ func main() { println(err.Error()) } pid := strconv.Itoa(os.Getpid()) - _ = ioutil.WriteFile(config.GetPidFile(), []byte(pid), 0755) + _ = os.WriteFile(config.GetPidFile(), []byte(pid), 0755) println("Start at " + time.Now().String()) println("goploy -h for more help") println("Current pid: " + pid) diff --git a/cmd/server/task/deploy.go b/cmd/server/task/deploy.go index 4caa40f..7b63a10 100644 --- a/cmd/server/task/deploy.go +++ b/cmd/server/task/deploy.go @@ -165,7 +165,23 @@ func (gsync Gsync) Exec() { return } - commitList, _ := r.CommitLog(gsync.Project.ID, 1) + commitList, err := r.CommitLog(gsync.Project.ID, 1) + if err != nil { + log.Errorf(projectLogFormat, gsync.Project.ID, err) + ws.GetHub().Data <- &ws.Data{ + Type: ws.TypeProject, + Message: deployMessage{ProjectID: gsync.Project.ID, ProjectName: gsync.Project.Name, State: DeployFail, Message: err.Error()}, + } + _ = gsync.Project.DeployFail() + publishTraceModel.Detail = err.Error() + publishTraceModel.State = model.Fail + if _, err := publishTraceModel.AddRow(); err != nil { + log.Errorf(projectLogFormat, gsync.Project.ID, err) + } + gsync.notify(model.ProjectFail, err.Error()) + return + } + gsync.CommitInfo = commitList[0] if gsync.Branch != "" { gsync.CommitInfo.Branch = gsync.Branch @@ -276,7 +292,7 @@ func (gsync Gsync) runAfterPullScript() (string, error) { scriptMode = project.AfterPullScriptMode } scriptText := project.ReplaceVars(commitInfo.ReplaceVars(project.AfterPullScript)) - _ = ioutil.WriteFile(scriptFullName, []byte(scriptText), 0755) + _ = os.WriteFile(scriptFullName, []byte(scriptText), 0755) var commandOptions []string if project.AfterPullScriptMode == "cmd" { commandOptions = append(commandOptions, "/C") @@ -311,7 +327,7 @@ func (gsync Gsync) remoteSync(msgChIn chan<- syncMessage) { if len(project.AfterDeployScript) != 0 { scriptContent := project.ReplaceVars(project.AfterDeployScript) scriptContent = projectServer.ReplaceVars(scriptContent) - _ = ioutil.WriteFile(path.Join(config.GetProjectPath(project.ID), scriptName), []byte(scriptContent), 0755) + _ = os.WriteFile(path.Join(config.GetProjectPath(project.ID), scriptName), []byte(scriptContent), 0755) } transmitterEntity := transmitter.New(project, projectServer) @@ -617,7 +633,7 @@ func (gsync Gsync) notify(deployState int, detail string) { } } -//keep the latest 10 project +// keep the latest 10 project func (gsync Gsync) removeExpiredBackup() { var wg sync.WaitGroup for _, projectServer := range gsync.ProjectServers { diff --git a/config/toml.go b/config/toml.go index ea4b102..884b223 100644 --- a/config/toml.go +++ b/config/toml.go @@ -7,6 +7,7 @@ package config import ( "github.com/pelletier/go-toml/v2" "io/ioutil" + "os" "time" ) @@ -107,7 +108,7 @@ func Write(cfg Config) error { return err } - err = ioutil.WriteFile(GetConfigFile(), yamlData, 0644) + err = os.WriteFile(GetConfigFile(), yamlData, 0644) if err != nil { return err } diff --git a/internal/repo/git.go b/internal/repo/git.go index db76ad0..6da22da 100644 --- a/internal/repo/git.go +++ b/internal/repo/git.go @@ -22,7 +22,6 @@ func (GitRepo) CanRollback() bool { return true } -// Ping - func (GitRepo) Ping(url string) error { git := pkg.GIT{} if err := git.LsRemote("-h", url); err != nil { @@ -32,7 +31,6 @@ func (GitRepo) Ping(url string) error { return nil } -// Create - func (GitRepo) Create(projectID int64) error { srcPath := config.GetProjectPath(projectID) if _, err := os.Stat(srcPath); err == nil { @@ -79,13 +77,13 @@ func (gitRepo GitRepo) Follow(project model.Project, target string) error { log.Trace("projectID:" + strconv.FormatInt(project.ID, 10) + " git add .") if err := git.Add("."); err != nil { log.Error(err.Error() + ", detail: " + git.Err.String()) - return errors.New(git.Err.String()) + return err } log.Trace("projectID:" + strconv.FormatInt(project.ID, 10) + " git reset --hard") if err := git.Reset("--hard"); err != nil { log.Error(err.Error() + ", detail: " + git.Err.String()) - return errors.New(git.Err.String()) + return err } // the length of commit id is 40 @@ -93,14 +91,14 @@ func (gitRepo GitRepo) Follow(project model.Project, target string) error { log.Trace("projectID:" + strconv.FormatInt(project.ID, 10) + " git fetch") if err := git.Fetch(); err != nil { log.Error(err.Error() + ", detail: " + git.Err.String()) - return errors.New(git.Err.String()) + return err } } log.Trace("projectID:" + strconv.FormatInt(project.ID, 10) + " git checkout -B goploy " + target) if err := git.Checkout("-B", "goploy", target); err != nil { log.Error(err.Error() + ", detail: " + git.Err.String()) - return errors.New(git.Err.String()) + return err } return nil } @@ -108,7 +106,7 @@ func (gitRepo GitRepo) Follow(project model.Project, target string) error { func (GitRepo) RemoteBranchList(url string) ([]string, error) { git := pkg.GIT{} if err := git.LsRemote("-h", url); err != nil { - return []string{}, errors.New(git.Err.String()) + return []string{}, err } var list []string @@ -131,7 +129,7 @@ func (GitRepo) BranchList(projectID int64) ([]string, error) { } if err := git.Branch("-r", "--sort=-committerdate"); err != nil { - return []string{}, errors.New(err.Error() + " detail: " + git.Err.String()) + return []string{}, err } rawBranchList := strings.Split(git.Output.String(), "\n") @@ -150,7 +148,7 @@ func (GitRepo) CommitLog(projectID int64, rows int) ([]CommitInfo, error) { git := pkg.GIT{Dir: config.GetProjectPath(projectID)} if err := git.Log("--stat", "--pretty=format:`start`%H`%an`%at`%s`%d`", "-n", strconv.Itoa(rows)); err != nil { - return []CommitInfo{}, errors.New(git.Err.String()) + return []CommitInfo{}, err } list := parseGITLog(git.Output.String()) @@ -161,7 +159,7 @@ func (GitRepo) BranchLog(projectID int64, branch string, rows int) ([]CommitInfo git := pkg.GIT{Dir: config.GetProjectPath(projectID)} if err := git.Log(branch, "--stat", "--pretty=format:`start`%H`%an`%at`%s`%d`", "-n", strconv.Itoa(rows)); err != nil { - return []CommitInfo{}, errors.New(git.Err.String()) + return []CommitInfo{}, err } list := parseGITLog(git.Output.String()) @@ -171,19 +169,19 @@ func (GitRepo) BranchLog(projectID int64, branch string, rows int) ([]CommitInfo func (GitRepo) TagLog(projectID int64, rows int) ([]CommitInfo, error) { git := pkg.GIT{Dir: config.GetProjectPath(projectID)} if err := git.Add("."); err != nil { - return []CommitInfo{}, errors.New(git.Err.String()) + return []CommitInfo{}, err } if err := git.Reset("--hard"); err != nil { - return []CommitInfo{}, errors.New(git.Err.String()) + return []CommitInfo{}, err } if err := git.Pull(); err != nil { - return []CommitInfo{}, errors.New(git.Err.String()) + return []CommitInfo{}, err } if err := git.Log("--tags", "-n", strconv.Itoa(rows), "--no-walk", "--stat", "--pretty=format:`start`%H`%an`%at`%s`%d`"); err != nil { - return []CommitInfo{}, errors.New(git.Err.String()) + return []CommitInfo{}, err } list := parseGITLog(git.Output.String())