github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/caps/caps.go (about) 1 package caps 2 3 import "strings" 4 5 type Capability int 6 7 const ( 8 Caching Capability = 1 << iota 9 Fmt 10 NoHeader 11 Wei 12 Ether 13 File 14 Output 15 Append 16 Verbose 17 Version 18 Noop 19 NoColor 20 Chain 21 Names 22 Default = Verbose | Fmt | Version | Noop | NoColor | Chain | NoHeader | File | Output | Append 23 ) 24 25 var allCaps = []Capability{ 26 Default, 27 Caching, 28 Fmt, 29 NoHeader, 30 Wei, 31 Ether, 32 File, 33 Output, 34 Append, 35 Verbose, 36 Version, 37 Noop, 38 NoColor, 39 Chain, 40 Names, 41 } 42 43 func (c Capability) Has(cap Capability) bool { 44 return c&cap != 0 45 } 46 47 func (c Capability) Add(cap Capability) Capability { 48 v := c | cap 49 return v 50 } 51 52 func (c Capability) Remove(cap Capability) Capability { 53 v := c &^ cap 54 if v == 0 { 55 v = Default 56 } 57 return v 58 } 59 60 func (c Capability) Text() string { 61 switch c { 62 case Caching: 63 return "cache,decache" 64 case Fmt: 65 return "fmt" 66 case NoHeader: 67 return "noHeader" 68 case Wei: 69 return "wei" 70 case Ether: 71 return "ether" 72 case File: 73 return "file" 74 case Output: 75 return "output" 76 case Append: 77 return "append" 78 case Verbose: 79 return "verbose" 80 case Version: 81 return "version" 82 case Noop: 83 return "noop" 84 case NoColor: 85 return "nocolor" 86 case Chain: 87 return "chain" 88 case Names: 89 return "names" 90 case Default: 91 return "default" 92 default: 93 return "unknown" 94 } 95 } 96 97 func (c Capability) HasKey(key string) bool { 98 if key == "fmt" || key == "file" || key == "testRunner" { 99 return true 100 } 101 102 if key == "cache" || key == "decache" { 103 return c.Has(Caching) 104 } 105 106 for _, cap := range allCaps { 107 if key == cap.Text() { 108 return c.Has(cap) 109 } 110 } 111 112 return false 113 } 114 115 func (c Capability) String() string { 116 ret := []string{} 117 for _, cap := range allCaps { 118 if c.Has(cap) { 119 ret = append(ret, cap.Text()) 120 } 121 } 122 return strings.Join(ret, ",") 123 } 124 125 func (c Capability) Show() string { 126 cc := c.Remove(Names) // Names has no --names flag. It's for internal use only. 127 ret := []string{} 128 for _, cap := range allCaps { 129 if cc.Has(cap) && !Default.Has(cap) { 130 ret = append(ret, cap.Text()) 131 } 132 } 133 for _, cap := range allCaps { 134 if !cc.Has(cap) && Default.Has(cap) { 135 ret = append(ret, "-"+cap.Text()) 136 } 137 } 138 return strings.Join(ret, ",") 139 }