1package fs 2 3import ( 4 "fmt" 5 "strings" 6 7 "github.com/pkg/errors" 8) 9 10// DumpFlags describes the Dump options in force 11type DumpFlags int 12 13// DumpFlags definitions 14const ( 15 DumpHeaders DumpFlags = 1 << iota 16 DumpBodies 17 DumpRequests 18 DumpResponses 19 DumpAuth 20 DumpFilters 21 DumpGoRoutines 22 DumpOpenFiles 23) 24 25var dumpFlags = []struct { 26 flag DumpFlags 27 name string 28}{ 29 {DumpHeaders, "headers"}, 30 {DumpBodies, "bodies"}, 31 {DumpRequests, "requests"}, 32 {DumpResponses, "responses"}, 33 {DumpAuth, "auth"}, 34 {DumpFilters, "filters"}, 35 {DumpGoRoutines, "goroutines"}, 36 {DumpOpenFiles, "openfiles"}, 37} 38 39// DumpFlagsList is a list of dump flags used in the help 40var DumpFlagsList string 41 42func init() { 43 // calculate the dump flags list 44 var out []string 45 for _, info := range dumpFlags { 46 out = append(out, info.name) 47 } 48 DumpFlagsList = strings.Join(out, ",") 49} 50 51// String turns a DumpFlags into a string 52func (f DumpFlags) String() string { 53 var out []string 54 for _, info := range dumpFlags { 55 if f&info.flag != 0 { 56 out = append(out, info.name) 57 f &^= info.flag 58 } 59 } 60 if f != 0 { 61 out = append(out, fmt.Sprintf("Unknown-0x%X", int(f))) 62 } 63 return strings.Join(out, ",") 64} 65 66// Set a DumpFlags as a comma separated list of flags 67func (f *DumpFlags) Set(s string) error { 68 var flags DumpFlags 69 parts := strings.Split(s, ",") 70 for _, part := range parts { 71 found := false 72 part = strings.ToLower(strings.TrimSpace(part)) 73 if part == "" { 74 continue 75 } 76 for _, info := range dumpFlags { 77 if part == info.name { 78 found = true 79 flags |= info.flag 80 } 81 } 82 if !found { 83 return errors.Errorf("Unknown dump flag %q", part) 84 } 85 } 86 *f = flags 87 return nil 88} 89 90// Type of the value 91func (f *DumpFlags) Type() string { 92 return "DumpFlags" 93} 94 95// UnmarshalJSON makes sure the value can be parsed as a string or integer in JSON 96func (f *DumpFlags) UnmarshalJSON(in []byte) error { 97 return UnmarshalJSONFlag(in, f, func(i int64) error { 98 *f = (DumpFlags)(i) 99 return nil 100 }) 101} 102