Skip to content

Commit d078851

Browse files
ameshkovhmage
authored andcommitted
gometalinter
1 parent c9d627e commit d078851

21 files changed

+454
-409
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
/scripts/translations/oneskyapp.json
1313

1414
# Test output
15-
dnsfilter/dnsfilter.TestLotsOfRules*.pprof
16-
tests/top-1m.csv
15+
dnsfilter/tests/top-1m.csv
16+
dnsfilter/tests/dnsfilter.TestLotsOfRules*.pprof

.gometalinter.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"Vendor": true,
3+
"Test": true,
4+
"Deadline": "2m",
5+
"Sort": ["linter", "severity", "path", "line"],
6+
"Exclude": [
7+
".*generated.*",
8+
"dnsfilter/rule_to_regexp.go"
9+
],
10+
"EnableGC": true,
11+
"Linters": {
12+
"nakedret": {
13+
"Command": "nakedret",
14+
"Pattern": "^(?P<path>.*?\\.go):(?P<line>\\d+)\\s*(?P<message>.*)$"
15+
}
16+
},
17+
"WarnUnmatchedDirective": true,
18+
19+
"DisableAll": true,
20+
"Enable": [
21+
"deadcode",
22+
"gocyclo",
23+
"gofmt",
24+
"goimports",
25+
"golint",
26+
"gosimple",
27+
"ineffassign",
28+
"interfacer",
29+
"lll",
30+
"misspell",
31+
"nakedret",
32+
"unconvert",
33+
"unparam",
34+
"unused",
35+
"vet"
36+
],
37+
38+
"Cyclo": 20,
39+
"LineLength": 200
40+
}

app.go

Lines changed: 90 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -43,92 +43,7 @@ func main() {
4343

4444
// config can be specified, which reads options from there, but other command line flags have to override config values
4545
// 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()
13247

13348
// Load filters from the disk
13449
// And if any filter has zero ID, assign a new one
@@ -150,7 +65,7 @@ func main() {
15065

15166
// Update filters we've just loaded right away, don't wait for periodic update timer
15267
go func() {
153-
refreshFiltersIfNeccessary(false)
68+
refreshFiltersIfNecessary(false)
15469
// Save the updated config
15570
err := config.write()
15671
if err != nil {
@@ -209,6 +124,94 @@ func getInput() (string, error) {
209124
return text, err
210125
}
211126

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+
212215
func promptAndGet(prompt string) (string, error) {
213216
for {
214217
fmt.Print(prompt)

config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
// configuration is loaded from YAML
2222
// field ordering is important -- yaml fields will mirror ordering from here
2323
type configuration struct {
24-
ourConfigFilename string // Config filename (can be overriden via the command line arguments)
24+
ourConfigFilename string // Config filename (can be overridden via the command line arguments)
2525
ourBinaryDir string // Location of our directory, used to protect against CWD being somewhere else
2626

2727
BindHost string `yaml:"bind_host"`
@@ -41,8 +41,8 @@ type configuration struct {
4141

4242
// field ordering is important -- yaml fields will mirror ordering from here
4343
type dnsConfig struct {
44-
BindHost string `yaml:"bind_host"`
45-
Port int `yaml:"port"`
44+
BindHost string `yaml:"bind_host"`
45+
Port int `yaml:"port"`
4646

4747
dnsforward.FilteringConfig `yaml:",inline"`
4848

0 commit comments

Comments
 (0)