-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.go
60 lines (50 loc) · 1.47 KB
/
cli.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
package main
import (
"bytes"
"encoding/base64"
"errors"
"flag"
"strings"
)
type flags struct {
proto string
addrs string
addrsSlice []string
deadlineMS uint
delayMS uint
breakMS uint
debug bool
packetBase64 string
packetBytes []byte
}
var (
ErrFlagsNotSet = errors.New("addrs are not set")
)
func getFlags(binaryName string, args []string) (*flags, string, error) {
flagSet := flag.NewFlagSet(binaryName, flag.ContinueOnError)
var buf bytes.Buffer
flagSet.SetOutput(&buf)
var conf = &flags{}
flagSet.StringVar(&conf.proto, "proto", "tcp", "tcp")
flagSet.StringVar(&conf.addrs, "addrs", "", "address:port(,address:port,address:port,...)")
flagSet.UintVar(&conf.deadlineMS, "deadline", 10000, "deadline in milliseconds")
flagSet.UintVar(&conf.delayMS, "wait", 100, "delay of single request in milliseconds")
flagSet.UintVar(&conf.breakMS, "delay", 50, "break between requests in milliseconds")
flagSet.BoolVar(&conf.debug, "debug", false, "debug messages toggler")
flagSet.StringVar(&conf.packetBase64, "packet", "", "UDP packet to be sent")
err := flagSet.Parse(args)
if err != nil {
return nil, buf.String(), err
}
conf.addrsSlice = strings.FieldsFunc(conf.addrs, func(c rune) bool {
return c == ','
})
if len(conf.addrsSlice) == 0 {
return nil, "", ErrFlagsNotSet
}
conf.packetBytes, err = base64.StdEncoding.DecodeString(conf.packetBase64)
if err != nil {
return nil, "", err
}
return conf, buf.String(), nil
}