]>
Commit | Line | Data |
---|---|---|
1 | package main | |
2 | ||
3 | import ( | |
4 | "context" | |
5 | "flag" | |
6 | "fmt" | |
7 | "github.com/jhalter/mobius/hotline" | |
8 | "github.com/rivo/tview" | |
9 | "go.uber.org/zap" | |
10 | "go.uber.org/zap/zapcore" | |
11 | "log" | |
12 | "os" | |
13 | "os/signal" | |
14 | "runtime" | |
15 | "syscall" | |
16 | ) | |
17 | ||
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 | ||
24 | configDir := flag.String("config", defaultConfigPath(), "Path to config root") | |
25 | version := flag.Bool("version", false, "print version and exit") | |
26 | logLevel := flag.String("log-level", "info", "Log level") | |
27 | logFile := flag.String("log-file", "", "output logs to file") | |
28 | ||
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 | ||
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)) | |
61 | } | |
62 | ||
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 | ||
74 | client := hotline.NewUIClient(*configDir, logger) | |
75 | client.DebugBuf = db | |
76 | client.UI.Start() | |
77 | ||
78 | } | |
79 | ||
80 | func newZapCore(level zapcore.Level, syncer zapcore.WriteSyncer) zapcore.Core { | |
81 | encoderCfg := zap.NewProductionEncoderConfig() | |
82 | encoderCfg.TimeKey = "timestamp" | |
83 | encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder | |
84 | ||
85 | return zapcore.NewCore( | |
86 | zapcore.NewConsoleEncoder(encoderCfg), | |
87 | zapcore.Lock(syncer), | |
88 | level, | |
89 | ) | |
90 | } | |
91 | ||
92 | var zapLogLevel = map[string]zapcore.Level{ | |
93 | "debug": zap.DebugLevel, | |
94 | "info": zap.InfoLevel, | |
95 | "warn": zap.WarnLevel, | |
96 | "error": zap.ErrorLevel, | |
97 | } | |
98 | ||
99 | func defaultConfigPath() (cfgPath string) { | |
100 | switch runtime.GOOS { | |
101 | case "windows": | |
102 | cfgPath = "mobius-client-config.yaml" | |
103 | case "darwin": | |
104 | if _, err := os.Stat("/usr/local/etc/mobius-client-config.yaml"); err == nil { | |
105 | cfgPath = "/usr/local/etc/mobius-client-config.yaml" | |
106 | } else if _, err := os.Stat("/opt/homebrew/etc/mobius-client-config.yaml"); err == nil { | |
107 | cfgPath = "/opt/homebrew/etc/mobius-client-config.yaml" | |
108 | } | |
109 | case "linux": | |
110 | cfgPath = "/usr/local/etc/mobius-client-config.yaml" | |
111 | default: | |
112 | fmt.Printf("unsupported OS") | |
113 | } | |
114 | ||
115 | return cfgPath | |
116 | } |