1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-30 09:56:23 -07:00

Refactor command line initialization and logging error handling

This commit is contained in:
Eggbertx 2025-05-08 16:34:11 -07:00
parent d923c73000
commit 7a4d28d3d9
3 changed files with 38 additions and 21 deletions

View file

@ -60,6 +60,22 @@ func printInfoAndLog(msg string, infoEv ...*zerolog.Event) {
}
}
func initCommandLine() *zerolog.Event {
var err error
if err = config.InitConfig(); err != nil {
fmt.Fprintln(os.Stderr, "Error initializing config:", err)
os.Exit(1)
}
systemCritical := config.GetSystemCriticalConfig()
if err = gcutil.InitLogs(systemCritical.LogDir, &gcutil.LogOptions{FileOnly: true}); err != nil {
os.Exit(1)
}
fatalEv := gcutil.LogFatal()
initDB(fatalEv)
return fatalEv
}
func fatalAndLog(msg string, err error, fatalEv *zerolog.Event) {
fmt.Fprintln(os.Stderr, msg, err)
fatalEv.Err(err).Caller(1).Msg(msg)
@ -68,7 +84,6 @@ func fatalAndLog(msg string, err error, fatalEv *zerolog.Event) {
func parseCommandLine() {
var newstaff string
var delstaff string
// var rebuild string
var password string
var rank int
var err error
@ -79,28 +94,12 @@ func parseCommandLine() {
cmd := os.Args[1]
var fatalEv *zerolog.Event
var systemCritical *config.SystemCriticalConfig
if cmd == "newstaff" || cmd == "delstaff" || cmd == "rebuild" {
if err = config.InitConfig(); err != nil {
fmt.Fprintln(os.Stderr, "Error initializing config:", err)
os.Exit(1)
}
systemCritical = config.GetSystemCriticalConfig()
if err = gcutil.InitLogs(systemCritical.LogDir, &gcutil.LogOptions{FileOnly: true}); err != nil {
fmt.Fprintln(os.Stderr, "Error initializing logs:", err)
os.Exit(1)
}
fatalEv = gcutil.LogFatal()
defer fatalEv.Discard()
initDB(fatalEv)
}
switch cmd {
case "version":
case "version", "-v", "-version":
fmt.Println(config.GochanVersion)
return
case "help", "-h", "--help":
case "help", "-h", "-help":
fmt.Println("Usage: gochan [command] [options]")
fmt.Println("Commands:")
fmt.Println(" version Show the version of gochan")
@ -143,6 +142,7 @@ func parseCommandLine() {
os.Exit(1)
}
}
fatalEv = initCommandLine()
staff, err := gcsql.NewStaff(newstaff, password, rank)
if err != nil {
@ -175,6 +175,7 @@ func parseCommandLine() {
return
}
}
fatalEv = initCommandLine()
if err = gcsql.DeactivateStaff(delstaff); err != nil {
fatalAndLog("Error deleting staff account:", err, fatalEv.Str("source", "commandLine").Str("username", delstaff))
}
@ -208,6 +209,7 @@ func parseCommandLine() {
flagSet.Usage()
os.Exit(1)
}
fatalEv = initCommandLine()
startupRebuild(rebuildFlag, fatalEv)
default:
fmt.Fprintln(os.Stderr, "Unknown command:", cmd)

View file

@ -1,6 +1,7 @@
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
@ -110,9 +111,12 @@ func main() {
<-sc
}
func initDB(fatalEv *zerolog.Event) {
func initDB(fatalEv *zerolog.Event, commandLine ...bool) {
systemCritical := config.GetSystemCriticalConfig()
if err := gcsql.ConnectToDB(&systemCritical.SQLConfig); err != nil {
if len(commandLine) > 0 && commandLine[0] {
fmt.Fprintln(os.Stderr, "Failed to connect to the database:", err)
}
fatalEv.Err(err).Msg("Failed to connect to the database")
}
events.TriggerEvent("db-connected")
@ -123,10 +127,16 @@ func initDB(fatalEv *zerolog.Event) {
if err := gcsql.CheckAndInitializeDatabase(systemCritical.DBtype); err != nil {
cleanup()
if len(commandLine) > 0 && commandLine[0] {
fmt.Fprintln(os.Stderr, "Failed to initialize the database:", err)
}
gcutil.LogFatal().Err(err).Msg("Failed to initialize the database")
}
events.TriggerEvent("db-initialized")
if err := gcsql.ResetViews(); err != nil {
if len(commandLine) > 0 && commandLine[0] {
fmt.Fprintln(os.Stderr, "Failed resetting SQL views:", err)
}
fatalEv.Err(err).Caller().Msg("Failed resetting SQL views")
}
events.TriggerEvent("db-views-reset")

View file

@ -1,6 +1,7 @@
package gcutil
import (
"fmt"
"io/fs"
"net/http"
"os"
@ -110,7 +111,11 @@ func initLog(logPath string, level zerolog.Level, noConsole bool) (err error) {
}
logFile, err = os.OpenFile(logPath, logFlags, logFileMode) // skipcq: GSC-G302
if err != nil {
logger.Err(err).Msg("Unable to open log file")
if noConsole {
fmt.Fprintln(os.Stderr, "Unable to open log file:", err)
} else {
logger.Err(err).Msg("Unable to open log file")
}
return err
}