]>
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 | ||
e130e0a2 | 74 | client := hotline.NewClient(*configDir, logger) |
6988a057 JH |
75 | client.DebugBuf = db |
76 | client.UI.Start() | |
77 | ||
78 | } | |
79 | ||
12ace83f | 80 | func newZapCore(level zapcore.Level, syncer zapcore.WriteSyncer) zapcore.Core { |
6988a057 JH |
81 | encoderCfg := zap.NewProductionEncoderConfig() |
82 | encoderCfg.TimeKey = "timestamp" | |
83 | encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder | |
84 | ||
85 | return zapcore.NewCore( | |
86 | zapcore.NewConsoleEncoder(encoderCfg), | |
12ace83f | 87 | zapcore.Lock(syncer), |
6988a057 JH |
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 | } | |
95753255 JH |
98 | |
99 | func defaultConfigPath() (cfgPath string) { | |
f4a69647 | 100 | switch runtime.GOOS { |
95753255 JH |
101 | case "windows": |
102 | cfgPath = "mobius-client-config.yaml" | |
103 | case "darwin": | |
f4a69647 JH |
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 | } | |
95753255 JH |
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 | } |