-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
146 lines (122 loc) · 3.18 KB
/
flags.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"flag"
"fmt"
"os"
"strconv"
)
type Flags struct {
ConfigFileName string
LogFileName string
SplitLogFiles bool
UI bool
MaxLines int
Realtime bool
Replay bool
Listen bool
Connect bool
Addr string
Port int
}
type FlagError struct {
err string
}
func (e FlagError) Error() string {
return e.err
}
func parseFlags() Flags {
var f Flags
var printversion bool
flag.StringVar(&f.ConfigFileName, "config", "./logunify.yaml", `Config file to use. Only required when recording,
not for replaying.`)
flag.StringVar(&f.LogFileName, "logfile", "", "Log file to write to.")
flag.BoolVar(&f.SplitLogFiles, "splitlog", false, "Split log output to separate logfiles (one per command).")
flag.BoolVar(&f.Replay, "replay", false, "Replay log data from a file or remote.")
flag.BoolVar(&f.Realtime, "realtime", false, "Replay in real time (including pauses).")
flag.BoolVar(&f.Listen, "listen", false, "Listen for incoming connections.")
flag.IntVar(&f.MaxLines, "maxlines", 500, "Maximum lines in UI buffer. 0 for unlimited scrollback.")
flag.BoolVar(&f.UI, "ui", false, "Enable UI. Implied if neither a remote, nor a logfile is given.")
flag.BoolVar(&f.Connect, "connect", false, "Connect to a remote host.")
flag.StringVar(&f.Addr, "address", "", "Address to connect/bind to.")
flag.IntVar(&f.Port, "port", 20000, "Port to use when logging over TCP.")
flag.BoolVar(&printversion, "version", false, "Print the program version.")
flag.Parse()
if printversion {
fmt.Println("logunify version: ", version)
os.Exit(0)
}
if f.MaxLines < 0 {
f.MaxLines = 0
}
if !(f.logfile() || f.remoteConnection()) {
f.UI = true
}
return f
}
func (flags *Flags) update(flagmap map[string]string) {
for k, v := range flagmap {
switch k {
case "address":
flags.Addr = v
case "logfile":
flags.LogFileName = v
case "ui":
flags.UI = v == "true"
case "replay":
flags.Replay = v == "true"
case "realtime":
flags.Realtime = v == "true"
case "listen":
flags.Listen = v == "true"
case "connect":
flags.Connect = v == "true"
case "splitlog":
flags.SplitLogFiles = v == "true"
case "addr":
flags.Addr = v
case "port":
p, err := strconv.Atoi(v)
if err == nil {
flags.Port = p
}
case "maxLines":
m, err := strconv.Atoi(v)
if err == nil {
flags.MaxLines = m
}
}
}
}
func (flags Flags) checkError() error {
if flags.Listen && flags.Connect {
return FlagError{"Can only -listen or -connect"}
}
if flags.Port <= 0 {
return FlagError{"Invalid port"}
}
if flags.MaxLines < 0 {
return FlagError{"Invalid maxlines"}
}
if flags.SplitLogFiles && !flags.logfile() {
return FlagError{"Logfile required when splitting log"}
}
return nil
}
func (f Flags) writeLogFile() bool {
return !f.Replay || f.remoteConnection()
}
func (f Flags) writeLogRemote() bool {
return !f.Replay && f.remoteConnection()
}
func (f Flags) remoteAddr() string {
if f.remoteConnection() {
return f.Addr + ":" + fmt.Sprint(f.Port)
}
return ""
}
func (f Flags) remoteConnection() bool {
return f.Connect || f.Listen
}
func (f Flags) logfile() bool {
return len(f.LogFileName) > 0
}