Format main.go

This commit is contained in:
Noah 2024-03-14 11:50:53 -05:00
parent 72c5467ae2
commit bce75062cf

27
main.go
View File

@ -4,10 +4,10 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/akamensky/argparse"
"log" "log"
"net/http" "net/http"
"os" "os"
"github.com/akamensky/argparse"
) )
// Create a configuration struct // Create a configuration struct
@ -16,8 +16,12 @@ var configuration = Configuration{}
// fileExists returns whether the given file or directory exists. // fileExists returns whether the given file or directory exists.
func fileExists(path string) (bool, error) { func fileExists(path string) (bool, error) {
_, err := os.Stat(path) _, err := os.Stat(path)
if err == nil { return true, nil } if err == nil {
if os.IsNotExist(err) { return false, nil } return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err return false, err
} }
@ -32,21 +36,25 @@ func handler(data map[string]interface{}) {
site, exists, err := getSite(repo_name) site, exists, err := getSite(repo_name)
if err != nil { if err != nil {
deploy_error := newDeployError(1, "handler", fmt.Sprintf("Failed to check if site '%s' exists", repo_name), fmt.Sprint(err)) deploy_error := newDeployError(1, "handler", fmt.Sprintf("Failed to check if site '%s' exists", repo_name), fmt.Sprint(err))
deploy_error.SendOverMatrix(); return deploy_error.SendOverMatrix()
return
} }
if exists { if exists {
sendMessage(MatrixMessage{text: "⚪️ Updating repository..."}) sendMessage(MatrixMessage{text: "⚪️ Updating repository..."})
if deploy_error := site.Update(); deploy_error.code != 0 { if deploy_error := site.Update(); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return deploy_error.SendOverMatrix()
return
} }
sendMessage(MatrixMessage{text: "⚪️ Restarting server..."}) sendMessage(MatrixMessage{text: "⚪️ Restarting server..."})
if deploy_error := site.Restart(); deploy_error.code != 0 { if deploy_error := site.Restart(); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return deploy_error.SendOverMatrix()
return
} }
} else { } else {
sendMessage(MatrixMessage{text: "⚪️ Cloning repository..."}) sendMessage(MatrixMessage{text: "⚪️ Cloning repository..."})
if deploy_error := CloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 { if deploy_error := CloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return deploy_error.SendOverMatrix()
return
} }
sendMessage(MatrixMessage{text: "⚪️ Starting server..."}) sendMessage(MatrixMessage{text: "⚪️ Starting server..."})
if site, exists, err = getSite(repo_name); err != nil { if site, exists, err = getSite(repo_name); err != nil {
@ -54,7 +62,8 @@ func handler(data map[string]interface{}) {
deploy_error.SendOverMatrix() deploy_error.SendOverMatrix()
} }
if deploy_error := site.Start(); deploy_error.code != 0 { if deploy_error := site.Start(); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return deploy_error.SendOverMatrix()
return
} }
defer sendMessage(MatrixMessage{text: "🚀 Launched for the first time!"}) defer sendMessage(MatrixMessage{text: "🚀 Launched for the first time!"})
} }
@ -73,7 +82,7 @@ func main() {
parseConfig(*config_path, &configuration) // Parse JSON configuration file parseConfig(*config_path, &configuration) // Parse JSON configuration file
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){ // Webhook receiver http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver
var data map[string]interface{} var data map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&data) err := json.NewDecoder(r.Body).Decode(&data)
if err != nil { if err != nil {