diff --git a/main.go b/main.go index e6e48dc..392a478 100644 --- a/main.go +++ b/main.go @@ -27,15 +27,26 @@ func handler(data map[string]interface{}) { log.Default().Printf("Repo: %s", repository["full_name"]) sendMessage(MatrixMessage{text: fmt.Sprintf("⚪️ Handling webhook for `%s`...", repository["full_name"])}) - deploy_error := pullRepo(repository["ssh_url"].(string), repository["name"].(string)) - if deploy_error.code != 0 { - deploy_error.SendOverMatrix() - return + repo_name := repository["name"].(string) + + exists, err := fileExists(configuration.sites_dir+"/"+repo_name) + if err != nil { + newDeployError(1, "handler", "Failed to check if site '%s' exists", fmt.Sprint(err)) } - deploy_error = restartSite(repository["name"].(string)) - if deploy_error.code != 0 { - deploy_error.SendOverMatrix() - return + if exists { + if deploy_error := updateSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 { + deploy_error.SendOverMatrix(); return + } + if deploy_error := restartSite(repo_name); deploy_error.code != 0 { + deploy_error.SendOverMatrix(); return + } + } else { + if deploy_error := cloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 { + deploy_error.SendOverMatrix(); return + } + if deploy_error := startSite(repo_name); deploy_error.code != 0 { + deploy_error.SendOverMatrix(); return + } } sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"}) } diff --git a/site.go b/site.go index e6b7f44..29ade41 100644 --- a/site.go +++ b/site.go @@ -82,28 +82,24 @@ func restartSite(name string) DeployError { return DeployError{} } -// pullRepo attempts to pull or clone a repository using the given name and url -func pullRepo(url string, name string) DeployError { - exists, err := fileExists(configuration.sites_dir+"/"+name) +// updateSite attempts to pull or clone a repository using the given name and url +func updateSite(url string, name string) DeployError { + log.Printf("Pulling down repository %s...", name) + repo,err := git.Open(configuration.sites_dir+"/"+name) if err != nil { - return newDeployError(1, "fileExists", - fmt.Sprintf("Failed to check whether folder '%s' exists while pulling down repository '%s'!", name, name), "") + return newDeployError(1, "updateSite", fmt.Sprintf("Failed to open git repository '%s'", name), fmt.Sprint(err)) } - if !exists { - log.Printf("Cloning repository %s...", name) - if err = git.Clone(url, configuration.sites_dir+"/"+name); err != nil { - return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down repository '%s'", name), "") - } - if err := startSite(name); err.code != 0 { - return err - } - } else { - log.Printf("Pulling down repository %s...", name) - repo,err := git.Open(configuration.sites_dir+"/"+name) - if err != nil { - return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name), "") - } - repo.Pull() + if err = repo.Pull(); err != nil { + return newDeployError(1, "updateSite", fmt.Sprintf("Failed to pull down git repository '%s'", name), fmt.Sprint(err)) + } + return DeployError{} +} + +// cloneSite +func cloneSite(url string, name string) DeployError { + log.Printf("Cloning repository %s...", name) + if err := git.Clone(url, configuration.sites_dir+"/"+name); err != nil { + return newDeployError(1, "cloneSite", fmt.Sprintf("Failed to pull down repository '%s'", name), fmt.Sprint(err)) } return DeployError{} }