diff --git a/deploy_error.go b/deploy_error.go index 2cf4fcb..5523859 100644 --- a/deploy_error.go +++ b/deploy_error.go @@ -7,21 +7,20 @@ import ( // DeployError contains important information about an error if something went wrong. type DeployError struct { - code int - where string - details string + code int + where string + details string command_output string } // SendOverMatrix provides an easy way to send the contents of a DeployError over Matrix func (deploy_error *DeployError) SendOverMatrix() { - sendMessage(MatrixMessage{text: fmt.Sprintf("🔴 **Error in **`%s`**!**\n- *%s*\n- Code: %d", - deploy_error.where, deploy_error.details, deploy_error.code), + sendMessage(MatrixMessage{text: fmt.Sprintf("%s**Error in **`%s`**!**\n- *%s*\n- Code: %d", + Circles["red"], deploy_error.where, deploy_error.details, deploy_error.code), }) } -// newDeployError creates a DeployError +// newDeployError creates a DeployError func newDeployError(code int, where string, details string, command_output string) DeployError { - return DeployError{ code: code, where: where, details: details, command_output: command_output } + return DeployError{code: code, where: where, details: details, command_output: command_output} } - diff --git a/main.go b/main.go index 3524849..0851c44 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,15 @@ import ( "os" ) +// Circle emoji storage. +var Circles = map[string]string{ + "white": "⚪️ ", + "green": "🟢 ", + "yellow": "🟡 ", + "red": "🔴 ", + "orange": "🟠 ", +} + // Create a configuration struct var configuration = Configuration{} @@ -29,7 +38,8 @@ func fileExists(path string) (bool, error) { func handler(data map[string]interface{}) { repository := data["repository"].(map[string]interface{}) log.Default().Printf("Repo: %s", repository["full_name"]) - sendMessage(MatrixMessage{text: fmt.Sprintf("⚪️ Handling webhook for `%s`...", repository["full_name"])}) + sendMessage(MatrixMessage{text: fmt.Sprintf("%sHandling webhook for `%s`...", + Circles["white"], repository["full_name"])}) repo_name := repository["name"].(string) @@ -40,7 +50,7 @@ func handler(data map[string]interface{}) { return } if exists { - sendMessage(MatrixMessage{text: "⚪️ Updating repository..."}) + sendMessage(MatrixMessage{text: Circles["white"] + "Updating repository..."}) if deploy_error := site.Update(); deploy_error.code != 0 { deploy_error.SendOverMatrix() return @@ -50,18 +60,18 @@ func handler(data map[string]interface{}) { newDeployError(1, "main", "Failed to update site data on a site we just created!", fmt.Sprint(err)) } site = new_site - sendMessage(MatrixMessage{text: "⚪️ Restarting server..."}) + sendMessage(MatrixMessage{text: Circles["white"] + "Restarting server..."}) if deploy_error := site.Restart(); deploy_error.code != 0 { deploy_error.SendOverMatrix() return } } else { - sendMessage(MatrixMessage{text: "⚪️ Cloning repository..."}) + sendMessage(MatrixMessage{text: Circles["white"] + "Cloning repository..."}) if deploy_error := CloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 { deploy_error.SendOverMatrix() return } - sendMessage(MatrixMessage{text: "⚪️ Starting server..."}) + sendMessage(MatrixMessage{text: Circles["white"] + "Starting server..."}) if site, exists, err = getSite(repo_name); err != nil { deploy_error := newDeployError(1, "handler", fmt.Sprintf("Failed to get site '%s' after creation!", repo_name), fmt.Sprint(err)) @@ -73,7 +83,7 @@ func handler(data map[string]interface{}) { } defer sendMessage(MatrixMessage{text: "🚀 Launched for the first time!"}) } - sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"}) + sendMessage(MatrixMessage{text: Circles["green"] + "Deployed successfully!"}) } // main is the starting point of the program diff --git a/matrix.go b/matrix.go index 69a2e37..885f1e7 100644 --- a/matrix.go +++ b/matrix.go @@ -15,7 +15,8 @@ import ( // MatrixMessage stores information about a matrix message. type MatrixMessage struct { - text string + text string + level int } // MatrixCommandCallback is a function that gets called when the bot is issued a command over Matrix @@ -87,7 +88,7 @@ func initMatrix() { } } if !found_command { - client.SendText(ctx, evt.RoomID, "🔴 Command not found") + client.SendText(ctx, evt.RoomID, Circles["red"]+" Command not found") } break } diff --git a/matrix_commands.go b/matrix_commands.go index 2ca2d87..6860759 100644 --- a/matrix_commands.go +++ b/matrix_commands.go @@ -25,10 +25,10 @@ func registerChatCommands() { cmd: "sites", description: "Display all available sites", callback: func(ctx context.Context, evt *event.Event, args []string) string { - text := "⚪️ Getting sites...\n" + text := Circles["white"] + "Getting sites...\n" sites, err := getAllSites() if err.code != 0 { - return text + "🔴 Failed to get sites!" + return text + Circles["red"] + "Failed to get sites!" } for _, site := range sites { text = fmt.Sprintf("%s- %s\n", text, site.getName()) @@ -44,21 +44,21 @@ func registerChatCommands() { description: "Restart a site.", callback: func(ctx context.Context, evt *event.Event, args []string) string { if len(args) < 2 { - return "🔴 Invalid site." + return Circles["red"] + "Invalid site." } site, found, err := getSite(args[1]) if err != nil { - return "🔴 Failed to get site " + args[2] + "!" + return Circles["red"] + "Failed to get site " + args[2] + "!" } else if !found { - return "🔴 Invalid site." + return Circles["red"] + "Invalid site." } - text := "⚪️ Restarting server..." + text := Circles["white"] + "Restarting server..." derr := site.Restart() if derr.code != 0 { derr.SendOverMatrix() - return text + "\n🔴 Failed to restart site!" + return text + "\n" + Circles["red"] + "Failed to restart site!" } - return text + "\n🟢 Successfully restarted site." + return text + "\n" + Circles["green"] + "Successfully restarted site." }, }, { @@ -66,21 +66,21 @@ func registerChatCommands() { description: "Start a site.", callback: func(ctx context.Context, evt *event.Event, args []string) string { if len(args) < 2 { - return "🔴 Invalid site." + return Circles["red"] + "Invalid site." } site, found, err := getSite(args[1]) if err != nil { - return "🔴 Failed to get site " + args[2] + "!" + return Circles["red"] + "Failed to get site " + args[2] + "!" } else if !found { - return "🔴 Invalid site." + return Circles["red"] + "Invalid site." } - text := "⚪️ Starting server..." + text := Circles["white"] + "Starting server..." if derr := site.Start(); derr.code != 0 { derr.SendOverMatrix() - return text + "\n🔴 Failed to stop site!" + return text + "\n" + Circles["red"] + "Failed to stop site!" } - return text + "\n🟢 Successfully started site." + return text + "\n" + Circles["green"] + "Successfully started site." }, }, { @@ -88,20 +88,20 @@ func registerChatCommands() { description: "Stop a site.", callback: func(ctx context.Context, evt *event.Event, args []string) string { if len(args) < 2 { - return "🔴 Invalid site." + return Circles["red"] + "Invalid site." } site, found, err := getSite(args[1]) if err != nil { - return "🔴 Failed to get site " + args[2] + "!" + return Circles["red"] + "Failed to get site " + args[2] + "!" } else if !found { - return "🔴 Invalid site." + return Circles["red"] + "Invalid site." } - text := "⚪️ Stopping server..." + text := Circles["white"] + "Stopping server..." if derr := site.Stop(); derr.code != 0 { derr.SendOverMatrix() - return text + "\n🔴 Failed to stop site!" + return text + "\n" + Circles["red"] + "Failed to stop site!" } - return text + "\n🟢 Successfully stopped site." + return text + "\n" + Circles["green"] + "Successfully stopped site." }, }, } diff --git a/site.go b/site.go index 4f87c8e..92b3a4f 100644 --- a/site.go +++ b/site.go @@ -63,7 +63,7 @@ func (site *Site) getConfig() (bool, error) { config := SiteConfig{} log.Printf("Loading per-site config %s", path) - warning := fmt.Sprintf("🟠 %s : Site config warning : ", site.getName()) + warning := fmt.Sprintf("%s%s : Site config warning : ", Circles["orange"], site.getName()) if _, err := getStringFromTable(L, table, "name", &config.name); err != nil { // Name sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)}) @@ -77,8 +77,6 @@ func (site *Site) getConfig() (bool, error) { } else if _, ok := raw_gitt.(*lua.LNilType); !ok { // If 'git' is neither a table or nil (undefined), sendMessage(MatrixMessage{text: warning + "'git' is neither a table or nil!"}) } - log.Print(config) - site.config = config return true, nil