@@ -43,92 +43,7 @@ func main() {
43
43
44
44
// config can be specified, which reads options from there, but other command line flags have to override config values
45
45
// therefore, we must do it manually instead of using a lib
46
- {
47
- var printHelp func ()
48
- var configFilename * string
49
- var bindHost * string
50
- var bindPort * int
51
- var opts = []struct {
52
- longName string
53
- shortName string
54
- description string
55
- callbackWithValue func (value string )
56
- callbackNoValue func ()
57
- }{
58
- {"config" , "c" , "path to config file" , func (value string ) { configFilename = & value }, nil },
59
- {"host" , "h" , "host address to bind HTTP server on" , func (value string ) { bindHost = & value }, nil },
60
- {"port" , "p" , "port to serve HTTP pages on" , func (value string ) {
61
- v , err := strconv .Atoi (value )
62
- if err != nil {
63
- panic ("Got port that is not a number" )
64
- }
65
- bindPort = & v
66
- }, nil },
67
- {"verbose" , "v" , "enable verbose output" , nil , func () { log .Verbose = true }},
68
- {"help" , "h" , "print this help" , nil , func () { printHelp (); os .Exit (64 ) }},
69
- }
70
- printHelp = func () {
71
- fmt .Printf ("Usage:\n \n " )
72
- fmt .Printf ("%s [options]\n \n " , os .Args [0 ])
73
- fmt .Printf ("Options:\n " )
74
- for _ , opt := range opts {
75
- fmt .Printf (" -%s, %-30s %s\n " , opt .shortName , "--" + opt .longName , opt .description )
76
- }
77
- }
78
- for i := 1 ; i < len (os .Args ); i ++ {
79
- v := os .Args [i ]
80
- knownParam := false
81
- for _ , opt := range opts {
82
- if v == "--" + opt .longName || v == "-" + opt .shortName {
83
- if opt .callbackWithValue != nil {
84
- if i + 1 > len (os .Args ) {
85
- log .Printf ("ERROR: Got %s without argument\n " , v )
86
- os .Exit (64 )
87
- }
88
- i ++
89
- opt .callbackWithValue (os .Args [i ])
90
- } else if opt .callbackNoValue != nil {
91
- opt .callbackNoValue ()
92
- }
93
- knownParam = true
94
- break
95
- }
96
- }
97
- if ! knownParam {
98
- log .Printf ("ERROR: unknown option %v\n " , v )
99
- printHelp ()
100
- os .Exit (64 )
101
- }
102
- }
103
- if configFilename != nil {
104
- config .ourConfigFilename = * configFilename
105
- }
106
-
107
- err := askUsernamePasswordIfPossible ()
108
- if err != nil {
109
- log .Fatal (err )
110
- }
111
-
112
- // Do the upgrade if necessary
113
- err = upgradeConfig ()
114
- if err != nil {
115
- log .Fatal (err )
116
- }
117
-
118
- // parse from config file
119
- err = parseConfig ()
120
- if err != nil {
121
- log .Fatal (err )
122
- }
123
-
124
- // override bind host/port from the console
125
- if bindHost != nil {
126
- config .BindHost = * bindHost
127
- }
128
- if bindPort != nil {
129
- config .BindPort = * bindPort
130
- }
131
- }
46
+ loadOptions ()
132
47
133
48
// Load filters from the disk
134
49
// And if any filter has zero ID, assign a new one
@@ -150,7 +65,7 @@ func main() {
150
65
151
66
// Update filters we've just loaded right away, don't wait for periodic update timer
152
67
go func () {
153
- refreshFiltersIfNeccessary (false )
68
+ refreshFiltersIfNecessary (false )
154
69
// Save the updated config
155
70
err := config .write ()
156
71
if err != nil {
@@ -209,6 +124,94 @@ func getInput() (string, error) {
209
124
return text , err
210
125
}
211
126
127
+ // loadOptions reads command line arguments and initializes configuration
128
+ func loadOptions () {
129
+ var printHelp func ()
130
+ var configFilename * string
131
+ var bindHost * string
132
+ var bindPort * int
133
+ var opts = []struct {
134
+ longName string
135
+ shortName string
136
+ description string
137
+ callbackWithValue func (value string )
138
+ callbackNoValue func ()
139
+ }{
140
+ {"config" , "c" , "path to config file" , func (value string ) { configFilename = & value }, nil },
141
+ {"host" , "h" , "host address to bind HTTP server on" , func (value string ) { bindHost = & value }, nil },
142
+ {"port" , "p" , "port to serve HTTP pages on" , func (value string ) {
143
+ v , err := strconv .Atoi (value )
144
+ if err != nil {
145
+ panic ("Got port that is not a number" )
146
+ }
147
+ bindPort = & v
148
+ }, nil },
149
+ {"verbose" , "v" , "enable verbose output" , nil , func () { log .Verbose = true }},
150
+ {"help" , "h" , "print this help" , nil , func () { printHelp (); os .Exit (64 ) }},
151
+ }
152
+ printHelp = func () {
153
+ fmt .Printf ("Usage:\n \n " )
154
+ fmt .Printf ("%s [options]\n \n " , os .Args [0 ])
155
+ fmt .Printf ("Options:\n " )
156
+ for _ , opt := range opts {
157
+ fmt .Printf (" -%s, %-30s %s\n " , opt .shortName , "--" + opt .longName , opt .description )
158
+ }
159
+ }
160
+ for i := 1 ; i < len (os .Args ); i ++ {
161
+ v := os .Args [i ]
162
+ knownParam := false
163
+ for _ , opt := range opts {
164
+ if v == "--" + opt .longName || v == "-" + opt .shortName {
165
+ if opt .callbackWithValue != nil {
166
+ if i + 1 > len (os .Args ) {
167
+ log .Printf ("ERROR: Got %s without argument\n " , v )
168
+ os .Exit (64 )
169
+ }
170
+ i ++
171
+ opt .callbackWithValue (os .Args [i ])
172
+ } else if opt .callbackNoValue != nil {
173
+ opt .callbackNoValue ()
174
+ }
175
+ knownParam = true
176
+ break
177
+ }
178
+ }
179
+ if ! knownParam {
180
+ log .Printf ("ERROR: unknown option %v\n " , v )
181
+ printHelp ()
182
+ os .Exit (64 )
183
+ }
184
+ }
185
+ if configFilename != nil {
186
+ config .ourConfigFilename = * configFilename
187
+ }
188
+
189
+ err := askUsernamePasswordIfPossible ()
190
+ if err != nil {
191
+ log .Fatal (err )
192
+ }
193
+
194
+ // Do the upgrade if necessary
195
+ err = upgradeConfig ()
196
+ if err != nil {
197
+ log .Fatal (err )
198
+ }
199
+
200
+ // parse from config file
201
+ err = parseConfig ()
202
+ if err != nil {
203
+ log .Fatal (err )
204
+ }
205
+
206
+ // override bind host/port from the console
207
+ if bindHost != nil {
208
+ config .BindHost = * bindHost
209
+ }
210
+ if bindPort != nil {
211
+ config .BindPort = * bindPort
212
+ }
213
+ }
214
+
212
215
func promptAndGet (prompt string ) (string , error ) {
213
216
for {
214
217
fmt .Print (prompt )
0 commit comments