gotest.tools/gotestsum@v1.11.0/cmd/flags.go (about) 1 package cmd 2 3 import ( 4 "encoding/csv" 5 "fmt" 6 "path" 7 "strings" 8 9 "github.com/dnephin/pflag" 10 "github.com/google/shlex" 11 "gotest.tools/gotestsum/internal/junitxml" 12 "gotest.tools/gotestsum/testjson" 13 ) 14 15 type hideSummaryValue struct { 16 value testjson.Summary 17 } 18 19 func newHideSummaryValue() *hideSummaryValue { 20 return &hideSummaryValue{value: testjson.SummarizeAll} 21 } 22 23 func readAsCSV(val string) ([]string, error) { 24 if val == "" { 25 return nil, nil 26 } 27 return csv.NewReader(strings.NewReader(val)).Read() 28 } 29 30 func (s *hideSummaryValue) Set(val string) error { 31 v, err := readAsCSV(val) 32 if err != nil { 33 return err 34 } 35 for _, item := range v { 36 summary, ok := testjson.NewSummary(item) 37 if !ok { 38 return fmt.Errorf("value must be one or more of: %s", 39 testjson.SummarizeAll.String()) 40 } 41 s.value -= summary 42 } 43 return nil 44 } 45 46 func (s *hideSummaryValue) Type() string { 47 return "summary" 48 } 49 50 func (s *hideSummaryValue) String() string { 51 // flip all the bits, since the flag value is the negative of what is stored 52 return (testjson.SummarizeAll ^ s.value).String() 53 } 54 55 var junitFieldFormatValues = "full, relative, short" 56 57 type junitFieldFormatValue struct { 58 value junitxml.FormatFunc 59 } 60 61 func (f *junitFieldFormatValue) Set(val string) error { 62 switch val { 63 case "full": 64 return nil 65 case "relative": 66 f.value = testjson.RelativePackagePath 67 return nil 68 case "short": 69 f.value = path.Base 70 return nil 71 } 72 return fmt.Errorf("invalid value: %v, must be one of: "+junitFieldFormatValues, val) 73 } 74 75 func (f *junitFieldFormatValue) Type() string { 76 return "field-format" 77 } 78 79 func (f *junitFieldFormatValue) String() string { 80 return "full" 81 } 82 83 func (f *junitFieldFormatValue) Value() junitxml.FormatFunc { 84 if f == nil { 85 return nil 86 } 87 return f.value 88 } 89 90 type commandValue struct { 91 original string 92 command []string 93 } 94 95 func (c *commandValue) String() string { 96 return c.original 97 } 98 99 func (c *commandValue) Set(raw string) error { 100 var err error 101 c.command, err = shlex.Split(raw) 102 c.original = raw 103 return err 104 } 105 106 func (c *commandValue) Type() string { 107 return "command" 108 } 109 110 func (c *commandValue) Value() []string { 111 if c == nil { 112 return nil 113 } 114 return c.command 115 } 116 117 var _ pflag.Value = (*stringSlice)(nil) 118 119 // stringSlice is a flag.Value which populates the string slice by splitting 120 // the raw flag value on whitespace. 121 type stringSlice []string 122 123 func (s *stringSlice) String() string { 124 return strings.Join(*s, " ") 125 } 126 127 func (s *stringSlice) Set(raw string) error { 128 *s = append(*s, strings.Fields(raw)...) 129 return nil 130 } 131 132 func (s *stringSlice) Type() string { 133 return "list" 134 } 135 136 func truthyFlag(s string) bool { 137 switch strings.ToLower(s) { 138 case "true", "yes", "1": 139 return true 140 } 141 return false 142 }