Partial revert of 4d2c4af 'Improve template mechanism' (#439)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
There were template functions which we defined and others started using. Although we no longer want those functions, since others use them, deleting them breaks our API. Putting those (unused) functions back.
authored by Eric Paris · committed by GitHub · parent eceb483 · Browse files · 1 file·+74 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+74 added−5 removed
- other:+74 added−5 removed
Provenance
- Commit
347767f- Tree
acbdc1a- Author
Eric Paris <eparis@redhat.com>- Committer
GitHub <noreply@github.com>- Parent
eceb483
1 file changed+74 added−5 removed
modified:cobra.go+74 added-5 removed
17 unmodified lines18 19 import (20 "fmt"21 "io"22+ "reflect"23+ "strconv"24 "strings"25 "text/template"26 "unicode"27 )28 29 var templateFuncs = template.FuncMap{28- "trimTrailingWhitespaces": trimTrailingWhitespaces,29- "rpad": rpad,30+ "trim": strings.TrimSpace,31+ "trimRightSpace": trimRightSpace,32+ "trimTrailingWhitespaces": trimRightSpace,33+ "appendIfNotPresent": appendIfNotPresent,34+ "rpad": rpad,35+ "gt": Gt,36+ "eq": Eq,37 }38 39 var initializers []func()40 24 unmodified lines65 func OnInitialize(y ...func()) {66 initializers = append(initializers, y...)67 }68 69+// FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.70+71+// Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,72+// Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as73+// ints and then compared.74+func Gt(a interface{}, b interface{}) bool {75+ var left, right int6476+ av := reflect.ValueOf(a)77+78+ switch av.Kind() {79+ case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:80+ left = int64(av.Len())81+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:82+ left = av.Int()83+ case reflect.String:84+ left, _ = strconv.ParseInt(av.String(), 10, 64)85+ }86+87+ bv := reflect.ValueOf(b)88+89+ switch bv.Kind() {90+ case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:91+ right = int64(bv.Len())92+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:93+ right = bv.Int()94+ case reflect.String:95+ right, _ = strconv.ParseInt(bv.String(), 10, 64)96+ }97+98+ return left > right99+}100+101+// FIXME Eq is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.102+103+// Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.104+func Eq(a interface{}, b interface{}) bool {105+ av := reflect.ValueOf(a)106+ bv := reflect.ValueOf(b)107+108+ switch av.Kind() {109+ case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:110+ panic("Eq called on unsupported type")111+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:112+ return av.Int() == bv.Int()113+ case reflect.String:114+ return av.String() == bv.String()115+ }116+ return false117+}118+62-func trimTrailingWhitespaces(s string) string {119+func trimRightSpace(s string) string {120 return strings.TrimRightFunc(s, unicode.IsSpace)121 }122 123+// FIXME appendIfNotPresent is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.124+125+// appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s.126+func appendIfNotPresent(s, stringToAppend string) string {127+ if strings.Contains(s, stringToAppend) {128+ return s129+ }130+ return s + " " + stringToAppend131+}132+133 // rpad adds padding to the right of a string.134 func rpad(s string, padding int) string {135 template := fmt.Sprintf("%%-%ds", padding)136 return fmt.Sprintf(template, s)137 }138 139 // tmpl executes the given template text on data, writing the result to w.140 func tmpl(w io.Writer, text string, data interface{}) error {74- t := template.New("top").Funcs(templateFuncs)75- return template.Must(t.Parse(text)).Execute(w, data)141+ t := template.New("top")142+ t.Funcs(templateFuncs)143+ template.Must(t.Parse(text))144+ return t.Execute(w, data)145 }146 147 // ld compares two strings and returns the levenshtein distance between them.148 func ld(s, t string, ignoreCase bool) int {33 unmodified lines