-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
80 lines (65 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
)
type options struct {
configPath string
logPath string
port string
}
var opts options
func init() {
c := flag.String("c", "", "specify bashRPC config file")
p := flag.String("p", "", "specify bashRPC port")
l := flag.String("log", "bashrpc.log", "specify log file")
flag.Parse()
opts = options{
configPath: *c,
logPath: *l,
port: *p,
}
}
func main() {
if opts.configPath == "" {
fmt.Println("config file argument is required")
flag.Usage()
os.Exit(1)
}
var (
logFile *os.File
cfg config
err error
rtr router
)
if cfg, err = loadConfig(opts.configPath); err != nil {
panic(err)
}
overrideConfigWithOptions(&cfg, opts)
if err = validateConfig(&cfg); err != nil {
panic(err)
}
if logFile, err = initLog(opts.logPath); err != nil {
panic(err)
}
defer logFile.Close()
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
log.Println("logging to", opts.logPath)
if rtr, err = newRouter(cfg); err != nil {
panic(err)
}
if err = rtr.listen(); err != nil {
panic(err)
}
}
func overrideConfigWithOptions(cfg *config, opts options) {
if opts.logPath != "" {
cfg.Log = opts.logPath
}
if opts.port != "" {
cfg.Port = opts.port
}
}