From 6acc16b6cc9421489ade3883d3654323cb4a7169 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 10 Feb 2024 20:40:27 +0100 Subject: [PATCH] only call run from the main function so we can have return codes on main --- cmd/nourybot/commands.go | 2 +- cmd/nourybot/download.go | 1 - cmd/nourybot/main.go | 17 +++++++++++++++++ internal/commands/ping.go | 6 ++---- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index 9eefaea..0163cb0 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -183,7 +183,7 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { reply, _ = commands.Phonetic(message.Message[11:len(message.Message)]) } case "ping": - reply = commands.Ping() + reply = commands.Ping(app.Environment) // ()bttv // ()weather diff --git a/cmd/nourybot/download.go b/cmd/nourybot/download.go index 6bc1e56..8383436 100644 --- a/cmd/nourybot/download.go +++ b/cmd/nourybot/download.go @@ -322,7 +322,6 @@ func (app *application) GofileDownload(target, link, identifier string, msg twit // time.Sleep(duration) go app.NewUpload("gofile", fileName, target, identifier, msg) - } func (app *application) CatboxDownload(target, link, identifier string, msg twitch.PrivateMessage) { diff --git a/cmd/nourybot/main.go b/cmd/nourybot/main.go index cb753ee..0efe710 100644 --- a/cmd/nourybot/main.go +++ b/cmd/nourybot/main.go @@ -4,7 +4,9 @@ import ( "context" "database/sql" "fmt" + "io" "os" + "os/signal" "time" "github.com/gempir/go-twitch-irc/v4" @@ -47,7 +49,20 @@ type application struct { // Rdb *redis.Client } +// main() only calls the run function so that we can go around the limitation +// that main() cannot return anything, like int for error codes in c for example. +// https://grafana.com/blog/2024/02/09/how-i-write-http-services-in-go-after-13-years/ func main() { + ctx := context.Background() + ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) + defer cancel() + if err := run(ctx, os.Stdout, os.Args); err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } +} + +func run(ctx context.Context, w io.Writer, args []string) error { var cfg config // Initialize a new sugared logger that we'll pass on // down through the application. @@ -216,6 +231,7 @@ func main() { panic(err) } + return nil } // openDB returns the sql.DB connection pool. @@ -249,4 +265,5 @@ func openDB(cfg config) (*sql.DB, error) { } return db, nil + } diff --git a/internal/commands/ping.go b/internal/commands/ping.go index 53e9e74..64dd4c1 100644 --- a/internal/commands/ping.go +++ b/internal/commands/ping.go @@ -7,12 +7,10 @@ import ( "github.com/lyx0/nourybot/pkg/humanize" ) -func Ping() string { +func Ping(env string) string { botUptime := humanize.Time(common.GetUptime()) commandsUsed := common.GetCommandsUsed() commit := common.GetVersion() - reply := fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v, Running on commit: %v", commandsUsed, botUptime, commit) - - return reply + return fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v, Running on commit: %v, Env: %v", commandsUsed, botUptime, commit, env) }