]> git.r.bdr.sh - rbdr/mobius/blob - client/main.go
patch: Fix ACLs and brew install issues
[rbdr/mobius] / client / main.go
1 package main
2
3 import (
4 "context"
5 "flag"
6 "fmt"
7 hotline "github.com/jhalter/mobius"
8 "github.com/rivo/tview"
9 "go.uber.org/zap"
10 "go.uber.org/zap/zapcore"
11 "log"
12 "os"
13 "os/signal"
14 "syscall"
15 )
16
17 func main() {
18 _, cancelRoot := context.WithCancel(context.Background())
19
20 sigChan := make(chan os.Signal, 1)
21 signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, os.Interrupt)
22
23 version := flag.Bool("version", false, "print version and exit")
24 logLevel := flag.String("log-level", "info", "Log level")
25 logFile := flag.String("log-file", "", "output logs to file")
26
27 flag.Parse()
28
29 if *version {
30 fmt.Printf("v%s\n", hotline.VERSION)
31 os.Exit(0)
32 }
33
34 zapLvl, ok := zapLogLevel[*logLevel]
35 if !ok {
36 fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel)
37 os.Exit(0)
38 }
39
40 // init DebugBuffer
41 db := &hotline.DebugBuffer{
42 TextView: tview.NewTextView(),
43 }
44
45 cores := []zapcore.Core{newZapCore(zapLvl, db)}
46
47 // Add file logger if optional log-file flag was passed
48 if *logFile != "" {
49 f, err := os.OpenFile(*logFile,
50 os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
51 if err != nil {
52 log.Println(err)
53 }
54 defer f.Close()
55 if err != nil {
56 panic(err)
57 }
58 cores = append(cores, newZapCore(zapLvl, f))
59 }
60
61 l := zap.New(zapcore.NewTee(cores...))
62 defer func() { _ = l.Sync() }()
63 logger := l.Sugar()
64 logger.Infow("Started Mobius client", "Version", hotline.VERSION)
65
66 go func() {
67 sig := <-sigChan
68 logger.Infow("Stopping client", "signal", sig.String())
69 cancelRoot()
70 }()
71
72 client := hotline.NewClient("", logger)
73 client.DebugBuf = db
74 client.UI.Start()
75
76 }
77
78 func newZapCore(level zapcore.Level, syncer zapcore.WriteSyncer) zapcore.Core {
79 encoderCfg := zap.NewProductionEncoderConfig()
80 encoderCfg.TimeKey = "timestamp"
81 encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
82
83 return zapcore.NewCore(
84 zapcore.NewConsoleEncoder(encoderCfg),
85 zapcore.Lock(syncer),
86 level,
87 )
88 }
89
90 var zapLogLevel = map[string]zapcore.Level{
91 "debug": zap.DebugLevel,
92 "info": zap.InfoLevel,
93 "warn": zap.WarnLevel,
94 "error": zap.ErrorLevel,
95 }