]>
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 | ||
24 | version := flag.Bool("version", false, "print version and exit") | |
25 | logLevel := flag.String("log-level", "info", "Log level") | |
12ace83f JH |
26 | logFile := flag.String("log-file", "", "output logs to file") |
27 | ||
6988a057 JH |
28 | flag.Parse() |
29 | ||
30 | if *version { | |
31 | fmt.Printf("v%s\n", hotline.VERSION) | |
32 | os.Exit(0) | |
33 | } | |
34 | ||
35 | zapLvl, ok := zapLogLevel[*logLevel] | |
36 | if !ok { | |
37 | fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) | |
38 | os.Exit(0) | |
39 | } | |
40 | ||
41 | // init DebugBuffer | |
42 | db := &hotline.DebugBuffer{ | |
43 | TextView: tview.NewTextView(), | |
44 | } | |
45 | ||
12ace83f JH |
46 | cores := []zapcore.Core{newZapCore(zapLvl, db)} |
47 | ||
48 | // Add file logger if optional log-file flag was passed | |
49 | if *logFile != "" { | |
50 | f, err := os.OpenFile(*logFile, | |
51 | os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | |
52 | if err != nil { | |
53 | log.Println(err) | |
54 | } | |
55 | defer f.Close() | |
56 | if err != nil { | |
57 | panic(err) | |
58 | } | |
59 | cores = append(cores, newZapCore(zapLvl, f)) | |
6988a057 | 60 | } |
12ace83f | 61 | |
6988a057 JH |
62 | l := zap.New(zapcore.NewTee(cores...)) |
63 | defer func() { _ = l.Sync() }() | |
64 | logger := l.Sugar() | |
65 | logger.Infow("Started Mobius client", "Version", hotline.VERSION) | |
66 | ||
67 | go func() { | |
68 | sig := <-sigChan | |
69 | logger.Infow("Stopping client", "signal", sig.String()) | |
70 | cancelRoot() | |
71 | }() | |
72 | ||
95753255 JH |
73 | cfgPath := defaultConfigPath() |
74 | ||
75 | client := hotline.NewClient(cfgPath, logger) | |
6988a057 JH |
76 | client.DebugBuf = db |
77 | client.UI.Start() | |
78 | ||
79 | } | |
80 | ||
12ace83f | 81 | func newZapCore(level zapcore.Level, syncer zapcore.WriteSyncer) zapcore.Core { |
6988a057 JH |
82 | encoderCfg := zap.NewProductionEncoderConfig() |
83 | encoderCfg.TimeKey = "timestamp" | |
84 | encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder | |
85 | ||
86 | return zapcore.NewCore( | |
87 | zapcore.NewConsoleEncoder(encoderCfg), | |
12ace83f | 88 | zapcore.Lock(syncer), |
6988a057 JH |
89 | level, |
90 | ) | |
91 | } | |
92 | ||
93 | var zapLogLevel = map[string]zapcore.Level{ | |
94 | "debug": zap.DebugLevel, | |
95 | "info": zap.InfoLevel, | |
96 | "warn": zap.WarnLevel, | |
97 | "error": zap.ErrorLevel, | |
98 | } | |
95753255 JH |
99 | |
100 | func defaultConfigPath() (cfgPath string) { | |
101 | os := runtime.GOOS | |
102 | switch os { | |
103 | case "windows": | |
104 | cfgPath = "mobius-client-config.yaml" | |
105 | case "darwin": | |
106 | cfgPath = "/usr/local/etc/mobius-client-config.yaml" | |
107 | case "linux": | |
108 | cfgPath = "/usr/local/etc/mobius-client-config.yaml" | |
109 | default: | |
110 | fmt.Printf("unsupported OS") | |
111 | } | |
112 | ||
113 | return cfgPath | |
114 | } |