X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/0c0b2680077c1103609c87a652bcc2263460b1d7..9cf66aeafbcbb9237fedc2efc97cc2856eb60f7f:/cmd/mobius-hotline-client/main.go diff --git a/cmd/mobius-hotline-client/main.go b/cmd/mobius-hotline-client/main.go index ec6f895..20bd7f2 100644 --- a/cmd/mobius-hotline-client/main.go +++ b/cmd/mobius-hotline-client/main.go @@ -6,21 +6,27 @@ import ( "fmt" "github.com/jhalter/mobius/hotline" "github.com/rivo/tview" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "log" + "log/slog" "os" "os/signal" "runtime" "syscall" ) +var logLevels = map[string]slog.Level{ + "debug": slog.LevelDebug, + "info": slog.LevelInfo, + "warn": slog.LevelWarn, + "error": slog.LevelError, +} + func main() { _, cancelRoot := context.WithCancel(context.Background()) sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, os.Interrupt) + configDir := flag.String("config", defaultConfigPath(), "Path to config root") version := flag.Bool("version", false, "print version and exit") logLevel := flag.String("log-level", "info", "Log level") logFile := flag.String("log-file", "", "output logs to file") @@ -32,78 +38,45 @@ func main() { os.Exit(0) } - zapLvl, ok := zapLogLevel[*logLevel] - if !ok { - fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) - os.Exit(0) - } - // init DebugBuffer db := &hotline.DebugBuffer{ TextView: tview.NewTextView(), } - cores := []zapcore.Core{newZapCore(zapLvl, db)} - // Add file logger if optional log-file flag was passed if *logFile != "" { f, err := os.OpenFile(*logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - log.Println(err) - } - defer f.Close() + defer func() { _ = f.Close() }() if err != nil { panic(err) } - cores = append(cores, newZapCore(zapLvl, f)) } - l := zap.New(zapcore.NewTee(cores...)) - defer func() { _ = l.Sync() }() - logger := l.Sugar() - logger.Infow("Started Mobius client", "Version", hotline.VERSION) + logger := slog.New(slog.NewTextHandler(db, &slog.HandlerOptions{Level: logLevels[*logLevel]})) + logger.Info("Started Mobius client", "Version", hotline.VERSION) go func() { sig := <-sigChan - logger.Infow("Stopping client", "signal", sig.String()) + logger.Info("Stopping client", "signal", sig.String()) cancelRoot() }() - cfgPath := defaultConfigPath() - - client := hotline.NewClient(cfgPath, logger) + client := hotline.NewUIClient(*configDir, logger) client.DebugBuf = db client.UI.Start() - -} - -func newZapCore(level zapcore.Level, syncer zapcore.WriteSyncer) zapcore.Core { - encoderCfg := zap.NewProductionEncoderConfig() - encoderCfg.TimeKey = "timestamp" - encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder - - return zapcore.NewCore( - zapcore.NewConsoleEncoder(encoderCfg), - zapcore.Lock(syncer), - level, - ) -} - -var zapLogLevel = map[string]zapcore.Level{ - "debug": zap.DebugLevel, - "info": zap.InfoLevel, - "warn": zap.WarnLevel, - "error": zap.ErrorLevel, } func defaultConfigPath() (cfgPath string) { - os := runtime.GOOS - switch os { + switch runtime.GOOS { case "windows": cfgPath = "mobius-client-config.yaml" case "darwin": - cfgPath = "/usr/local/etc/mobius-client-config.yaml" + if _, err := os.Stat("/usr/local/etc/mobius-client-config.yaml"); err == nil { + cfgPath = "/usr/local/etc/mobius-client-config.yaml" + } else if _, err := os.Stat("/opt/homebrew/etc/mobius-client-config.yaml"); err == nil { + cfgPath = "/opt/homebrew/etc/mobius-client-config.yaml" + } case "linux": cfgPath = "/usr/local/etc/mobius-client-config.yaml" default: