/
flags.go
146 lines (123 loc) · 2.84 KB
/
flags.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
145
146
package cmd
import (
"errors"
"flag"
"fmt"
"os"
"strconv"
"time"
"github.com/hnakamur/whispertool"
)
var errEmptyRateOutOfBounds = errors.New("emptyRate must be 0 <= r <= 1")
var errFromIsAfterUntil = errors.New("from time must not be after until time")
var errNonEmptyDestRelPathForSrcRelPathWithMeta = errors.New("dest must be empty when src contains glob meta character(s)")
type Command interface {
Parse(fs *flag.FlagSet, args []string) error
Execute() error
}
type timestampValue struct {
t *whispertool.Timestamp
}
func (t timestampValue) String() string {
if t.t == nil {
return ""
}
return t.t.ToStdTime().Format(whispertool.UTCTimeLayout)
}
func (t timestampValue) Set(s string) error {
t2, err := time.Parse(whispertool.UTCTimeLayout, s)
if err != nil {
return err
}
*t.t = whispertool.TimestampFromStdTime(t2)
return nil
}
type fileModeValue struct {
m *os.FileMode
}
func (v fileModeValue) String() string {
if v.m == nil {
return ""
}
return strconv.FormatUint(uint64(*v.m), 8)
}
func (v fileModeValue) Set(s string) error {
m, err := strconv.ParseUint(s, 8, 32)
if err != nil {
return err
}
*v.m = os.FileMode(m)
return nil
}
type aggregationMethodValue struct {
m *whispertool.AggregationMethod
}
func (v aggregationMethodValue) String() string {
if v.m == nil {
return ""
}
return v.m.String()
}
func (v aggregationMethodValue) Set(s string) error {
m, err := whispertool.AggregationMethodString(s)
if err != nil {
return err
}
switch m {
case whispertool.Average, whispertool.Sum, whispertool.Last, whispertool.Max, whispertool.Min, whispertool.First:
*v.m = m
return nil
default:
return errors.New(`aggregation method must be one of "average", "sum", "last", "max", "min", or "first"`)
}
}
type xFilesFactorValue struct {
f *float32
}
func (v xFilesFactorValue) String() string {
if v.f == nil {
return ""
}
return strconv.FormatFloat(float64(*v.f), 'f', -1, 32)
}
func (v xFilesFactorValue) Set(s string) error {
f, err := strconv.ParseFloat(s, 32)
if err != nil {
return err
}
if f < 0 || 1 < f {
return errors.New("xFilesFactor must be between 0.0 and 1.0")
}
*v.f = float32(f)
return nil
}
type archiveInfoListValue struct {
l *whispertool.ArchiveInfoList
}
func (v archiveInfoListValue) String() string {
if v.l == nil {
return ""
}
return v.l.String()
}
func (v archiveInfoListValue) Set(s string) error {
l, err := whispertool.ParseArchiveInfoList(s)
if err != nil {
return err
}
*v.l = l
return nil
}
type RequiredOptionError struct {
fs *flag.FlagSet
option string
}
func newRequiredOptionError(fs *flag.FlagSet, option string) *RequiredOptionError {
return &RequiredOptionError{fs: fs, option: option}
}
func (e *RequiredOptionError) Error() string {
return fmt.Sprintf("option -%s is required.", e.option)
}
func (e *RequiredOptionError) Usage() {
e.fs.Usage()
}