forked from gotestyourself/gotestsum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.go
142 lines (117 loc) · 2.64 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
package cmd
import (
"encoding/csv"
"fmt"
"path"
"strings"
"github.com/dnephin/pflag"
"github.com/google/shlex"
"gotest.tools/gotestsum/internal/junitxml"
"gotest.tools/gotestsum/testjson"
)
type hideSummaryValue struct {
value testjson.Summary
}
func newHideSummaryValue() *hideSummaryValue {
return &hideSummaryValue{value: testjson.SummarizeAll}
}
func readAsCSV(val string) ([]string, error) {
if val == "" {
return nil, nil
}
return csv.NewReader(strings.NewReader(val)).Read()
}
func (s *hideSummaryValue) Set(val string) error {
v, err := readAsCSV(val)
if err != nil {
return err
}
for _, item := range v {
summary, ok := testjson.NewSummary(item)
if !ok {
return fmt.Errorf("value must be one or more of: %s",
testjson.SummarizeAll.String())
}
s.value -= summary
}
return nil
}
func (s *hideSummaryValue) Type() string {
return "summary"
}
func (s *hideSummaryValue) String() string {
// flip all the bits, since the flag value is the negative of what is stored
return (testjson.SummarizeAll ^ s.value).String()
}
var junitFieldFormatValues = "full, relative, short"
type junitFieldFormatValue struct {
value junitxml.FormatFunc
}
func (f *junitFieldFormatValue) Set(val string) error {
switch val {
case "full":
return nil
case "relative":
f.value = testjson.RelativePackagePath
return nil
case "short":
f.value = path.Base
return nil
}
return fmt.Errorf("invalid value: %v, must be one of: "+junitFieldFormatValues, val)
}
func (f *junitFieldFormatValue) Type() string {
return "field-format"
}
func (f *junitFieldFormatValue) String() string {
return "full"
}
func (f *junitFieldFormatValue) Value() junitxml.FormatFunc {
if f == nil {
return nil
}
return f.value
}
type commandValue struct {
original string
command []string
}
func (c *commandValue) String() string {
return c.original
}
func (c *commandValue) Set(raw string) error {
var err error
c.command, err = shlex.Split(raw)
c.original = raw
return err
}
func (c *commandValue) Type() string {
return "command"
}
func (c *commandValue) Value() []string {
if c == nil {
return nil
}
return c.command
}
var _ pflag.Value = (*stringSlice)(nil)
// stringSlice is a flag.Value which populates the string slice by splitting
// the raw flag value on whitespace.
type stringSlice []string
func (s *stringSlice) String() string {
return strings.Join(*s, " ")
}
func (s *stringSlice) Set(raw string) error {
*s = append(*s, strings.Fields(raw)...)
return nil
}
func (s *stringSlice) Type() string {
return "list"
}
func truthyFlag(s string) bool {
switch strings.ToLower(s) {
case "true", "yes", "1":
return true
}
return false
}