-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
122 lines (99 loc) · 1.92 KB
/
option.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
package options
import (
"io"
"io/ioutil"
stats_options "github.com/tkuchiki/alp/options"
"gopkg.in/yaml.v2"
)
type Options struct {
StatsOptions *stats_options.Options `yaml:"stats_options"`
Snaplen int `yaml:snaplen`
Iface string `yaml:iface`
Port int `yaml:port`
Pcap string `yaml:pcap`
Lazy bool `yaml:lazy`
Body bool `yaml:body`
Gunzip bool `yaml:gunzip`
}
type Option func(*Options)
func Snaplen(i int) Option {
return func(opts *Options) {
if i > 0 {
opts.Snaplen = i
}
}
}
func Iface(s string) Option {
return func(opts *Options) {
if s != "" {
opts.Iface = s
}
}
}
func Port(i int) Option {
return func(opts *Options) {
if i > 0 {
opts.Port = i
}
}
}
func Pcap(s string) Option {
return func(opts *Options) {
if s != "" {
opts.Pcap = s
}
}
}
func Lazy(b bool) Option {
return func(opts *Options) {
if b {
opts.Lazy = b
}
}
}
func Body(b bool) Option {
return func(opts *Options) {
if b {
opts.Body = b
}
}
}
func Gunzip(b bool) Option {
return func(opts *Options) {
if b {
opts.Gunzip = b
}
}
}
func NewOptions(opt ...Option) *Options {
options := &Options{
Snaplen: 65536,
}
for _, o := range opt {
o(options)
}
statsOptions := stats_options.NewOptions()
options.StatsOptions = statsOptions
return options
}
func SetOptions(options *Options, opt ...Option) *Options {
for _, o := range opt {
o(options)
}
return options
}
func LoadOptionsFromReader(r io.Reader) (*Options, error) {
opts := NewOptions()
buf, err := ioutil.ReadAll(r)
if err != nil {
return opts, err
}
err = yaml.Unmarshal(buf, opts)
return opts, err
}
func (o *Options) Int32Snaplen() int32 {
return int32(o.Snaplen)
}
func (o *Options) Uint32Snaplen() uint32 {
return uint32(o.Snaplen)
}