-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
57 lines (47 loc) · 1.47 KB
/
config.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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
)
const (
validTimeUnits = "Valid time units: ns, us (or µs), ms, s, m, h."
SchemeHTTP = "http"
SchemeHTTPS = "https"
SchemeTCP = "tcp"
)
type config struct {
timeout *time.Duration
maxAttempts *int
retryDelay *time.Duration
attemptTimeout *time.Duration
}
func (c *config) addflags(s *flag.FlagSet) {
timeout := s.Duration("timeout", -1, "Time after which the attempt(s) will be interrupted. "+validTimeUnits)
s.DurationVar(timeout, "t", -1, "Shortcut for --timeout")
c.timeout = timeout
maxAttempts := s.Int("max-attempts", 1, "Maximum number of attempts to make (unlimitted: -1)")
s.IntVar(maxAttempts, "a", 1, "Shortcut for --max-attempts")
c.maxAttempts = maxAttempts
delay := s.Duration("retry-delay", 1*time.Second, "Delay between attempts. "+validTimeUnits)
s.DurationVar(delay, "d", 1*time.Second, "Shortcut for --retry-delay")
c.retryDelay = delay
attemptTimeout := s.Duration("attempt-timeout", -1, "Time after which each individual attempt will be interrupted. "+validTimeUnits)
s.DurationVar(attemptTimeout, "at", -1, "Shortcut for --attempt-timeout")
c.attemptTimeout = attemptTimeout
}
func usage(s *flag.FlagSet) func() {
return func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <address>\n", filepath.Base(os.Args[0]))
s.PrintDefaults()
}
}
func parseFlags(s *flag.FlagSet) *config {
c := &config{}
c.addflags(s)
s.Usage = usage(s)
s.Parse(os.Args[1:])
return c
}