This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.go
144 lines (127 loc) · 3.57 KB
/
parser.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"flag"
"fmt"
"io"
"regexp"
"runtime"
"time"
"github.com/kamilsk/retry/v4"
"github.com/kamilsk/retry/v4/backoff"
"github.com/kamilsk/retry/v4/jitter"
"github.com/kamilsk/retry/v4/strategy"
"github.com/pkg/errors"
)
// Metadata holds meta information.
type Metadata struct {
BinName string
Commit, BuildDate, Version string
Compiler, Platform, GoVersion string
}
// Command holds command configuration.
type Command struct {
Timeout time.Duration
Debug bool
Notify bool
Args []string
Strategies retry.How
}
var (
re = regexp.MustCompile(`^(\w+)(?::((?:[\w.]+,?)+))?$`)
compliance map[string]struct {
cursor interface{}
usage string
handler func(*flag.Flag) (strategy.Strategy, error)
}
algorithms map[string]func(args string) (backoff.Algorithm, error)
transforms map[string]func(args string) (jitter.Transformation, error)
usage func(output io.Writer, metadata Metadata) func()
)
func parse(output io.Writer, binary string, arguments ...string) (Command, error) {
cmd := Command{}
cl := flag.NewFlagSet(binary, flag.ContinueOnError)
cl.SetOutput(output)
cl.Usage = usage(output, Metadata{
BinName: binary,
Commit: commit, BuildDate: date, Version: version,
Compiler: runtime.Compiler, Platform: runtime.GOOS + "/" + runtime.GOARCH, GoVersion: runtime.Version(),
})
for name, cfg := range compliance {
switch cursor := cfg.cursor.(type) {
case *string:
cl.StringVar(cursor, name, "", cfg.usage)
case *bool:
cl.BoolVar(cursor, name, false, cfg.usage)
default:
return cmd, fmt.Errorf("init: an unsupported cursor type %T", cursor)
}
}
cl.DurationVar(&cmd.Timeout, "timeout", time.Minute, "Timeout for task execution")
cl.BoolVar(&cmd.Debug, "debug", false, "show error stack trace")
cl.BoolVar(&cmd.Notify, "notify", false, "show notification at the end (not implemented yet)")
if err := cl.Parse(arguments); err != nil {
if err == flag.ErrHelp {
return cmd, err
}
return cmd, errors.Wrap(err, "parse")
}
{
var err error
if cmd.Strategies, err = handle(func() []*flag.Flag {
flags := make([]*flag.Flag, 0, cl.NFlag())
cl.Visit(func(f *flag.Flag) {
flags = append(flags, f)
})
return flags
}()); err != nil {
return cmd, errors.Wrap(err, "parse")
}
}
if cmd.Args = cl.Args(); len(cmd.Args) == 0 {
return cmd, errors.New("please provide a command to retry")
}
return cmd, nil
}
func handle(flags []*flag.Flag) ([]func(attempt uint, err error) bool, error) {
strategies := make([]func(attempt uint, err error) bool, 0, len(flags))
for _, f := range flags {
if c, ok := compliance[f.Name]; ok {
s, err := c.handler(f)
if err != nil {
return nil, errors.Wrap(err, "handle")
}
strategies = append(strategies, s)
}
}
return strategies, nil
}
func parseAlgorithm(args string) (backoff.Algorithm, error) {
m := re.FindStringSubmatch(args)
if len(m) < 2 {
return nil, errors.Errorf("parse algorithm: invalid argument %s", args)
}
algorithm, ok := algorithms[m[1]]
if !ok {
return nil, errors.Errorf("parse algorithm: unknown algorithm %s", m[1])
}
args = ""
if len(m) == 3 {
args = m[2]
}
return algorithm(args)
}
func parseTransform(args string) (jitter.Transformation, error) {
m := re.FindStringSubmatch(args)
if len(m) < 2 {
return nil, errors.Errorf("parse transformation: invalid argument %s", args)
}
transformation, ok := transforms[m[1]]
if !ok {
return nil, errors.Errorf("parse transformation: unknown transformation %s", m[1])
}
args = ""
if len(m) == 3 {
args = m[2]
}
return transformation(args)
}