-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
56 lines (49 loc) · 1.19 KB
/
conf.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
package main
import (
"errors"
"github.com/irealing/argsparser"
"github.com/qiniu/log"
)
const (
defaultConcurrent uint = 10
emptyString = ""
defaultPortFile = "ports.csv"
errorInfo = "参数异常"
)
var (
DefaultConfig = &AppConfig{}
)
func init() {
ap := argsparser.New(DefaultConfig)
ap.Init()
if err := ap.Parse(); err != nil {
ap.PrintHelp()
log.Fatal(err)
}
}
type AppConfig struct {
Go uint `param:"go" usage:"并发数"`
Input string `param:"if" usage:"URL地址列表文件"`
Output string `param:"of" usage:"输出文件"`
Log string `param:"log" usage:"日志级别"`
Port string `param:"port" usage:"端口列表文件"`
TTL int `param:"ttl" usage:"请求超时时间"`
}
func (ac *AppConfig) Validate() (err error) {
if ac.Go == 0 {
ac.Go = defaultConcurrent
}
if ac.Port == emptyString {
ac.Port = defaultPortFile
}
if ac.Input == emptyString || ac.Output == emptyString {
err = errors.New(errorInfo)
}
logLevel := map[string]int{"debug": log.Ldebug, "info": log.Linfo, "warn": log.Lwarn, "error": log.Lerror}
level, ok := logLevel[ac.Log]
if !ok {
level = log.Linfo
}
log.SetOutputLevel(level)
return
}