]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package main |
2 | ||
3 | import ( | |
4 | "context" | |
5 | "flag" | |
6 | "fmt" | |
22c599ab | 7 | "github.com/jhalter/mobius/hotline" |
6988a057 JH |
8 | "github.com/rivo/tview" |
9 | "go.uber.org/zap" | |
10 | "go.uber.org/zap/zapcore" | |
12ace83f | 11 | "log" |
6988a057 JH |
12 | "os" |
13 | "os/signal" | |
95753255 | 14 | "runtime" |
6988a057 | 15 | "syscall" |
6988a057 JH |
16 | ) |
17 | ||
6988a057 JH |
18 | func main() { |
19 | _, cancelRoot := context.WithCancel(context.Background()) | |
20 | ||
21 | sigChan := make(chan os.Signal, 1) | |
22 | signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, os.Interrupt) | |
23 | ||
e130e0a2 | 24 | configDir := flag.String("config", defaultConfigPath(), "Path to config root") |
6988a057 JH |
25 | version := flag.Bool("version", false, "print version and exit") |
26 | logLevel := flag.String("log-level", "info", "Log level") | |
12ace83f JH |
27 | logFile := flag.String("log-file", "", "output logs to file") |
28 | ||
6988a057 JH |
29 | flag.Parse() |
30 | ||
31 | if *version { | |
32 | fmt.Printf("v%s\n", hotline.VERSION) | |
33 | os.Exit(0) | |
34 | } | |
35 | ||
36 | zapLvl, ok := zapLogLevel[*logLevel] | |
37 | if !ok { | |
38 | fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) | |
39 | os.Exit(0) | |
40 | } | |
41 | ||
42 | // init DebugBuffer | |
43 | db := &hotline.DebugBuffer{ | |
44 | TextView: tview.NewTextView(), | |
45 | } | |
46 | ||
12ace83f JH |
47 | cores := []zapcore.Core{newZapCore(zapLvl, db)} |
48 | ||
49 | // Add file logger if optional log-file flag was passed | |
50 | if *logFile != "" { | |
51 | f, err := os.OpenFile(*logFile, | |
52 | os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | |
53 | if err != nil { | |
54 | log.Println(err) | |
55 | } | |
56 | defer f.Close() | |
57 | if err != nil { | |
58 | panic(err) | |
59 | } | |
60 | cores = append(cores, newZapCore(zapLvl, f)) | |
6988a057 | 61 | } |
12ace83f | 62 | |
6988a057 JH |
63 | l := zap.New(zapcore.NewTee(cores...)) |
64 | defer func() { _ = l.Sync() }() | |
65 | logger := l.Sugar() | |
66 | logger.Infow("Started Mobius client", "Version", hotline.VERSION) | |
67 | ||
68 | go func() { | |
69 | sig := <-sigChan | |
70 | logger.Infow("Stopping client", "signal", sig.String()) | |
71 | cancelRoot() | |
72 | }() | |
73 | ||
d005ef04 | 74 | client := hotline.NewUIClient(*configDir, logger) |
6988a057 JH |
75 | client.DebugBuf = db |
76 | client.UI.Start() | |
6988a057 JH |
77 | } |
78 | ||
12ace83f | 79 | func newZapCore(level zapcore.Level, syncer zapcore.WriteSyncer) zapcore.Core { |
6988a057 JH |
80 | encoderCfg := zap.NewProductionEncoderConfig() |
81 | encoderCfg.TimeKey = "timestamp" | |
82 | encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder | |
83 | ||
84 | return zapcore.NewCore( | |
85 | zapcore.NewConsoleEncoder(encoderCfg), | |
12ace83f | 86 | zapcore.Lock(syncer), |
6988a057 JH |
87 | level, |
88 | ) | |
89 | } | |
90 | ||
91 | var zapLogLevel = map[string]zapcore.Level{ | |
92 | "debug": zap.DebugLevel, | |
93 | "info": zap.InfoLevel, | |
94 | "warn": zap.WarnLevel, | |
95 | "error": zap.ErrorLevel, | |
96 | } | |
95753255 JH |
97 | |
98 | func defaultConfigPath() (cfgPath string) { | |
f4a69647 | 99 | switch runtime.GOOS { |
95753255 JH |
100 | case "windows": |
101 | cfgPath = "mobius-client-config.yaml" | |
102 | case "darwin": | |
f4a69647 JH |
103 | if _, err := os.Stat("/usr/local/etc/mobius-client-config.yaml"); err == nil { |
104 | cfgPath = "/usr/local/etc/mobius-client-config.yaml" | |
105 | } else if _, err := os.Stat("/opt/homebrew/etc/mobius-client-config.yaml"); err == nil { | |
106 | cfgPath = "/opt/homebrew/etc/mobius-client-config.yaml" | |
107 | } | |
95753255 JH |
108 | case "linux": |
109 | cfgPath = "/usr/local/etc/mobius-client-config.yaml" | |
110 | default: | |
111 | fmt.Printf("unsupported OS") | |
112 | } | |
113 | ||
114 | return cfgPath | |
115 | } |