Skip to content
Myr

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

1 file+74 added−5 removed

modified:cobra.go+74 added-5 removed
17 unmodified lines
1819import (20 "fmt"21 "io"22 "reflect"23 "strconv"24 "strings"25 "text/template"26 "unicode"27)2829var 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}3839var initializers []func()40
24 unmodified lines
65func OnInitialize(y ...func()) {66 initializers = append(initializers, y...)67}6869// FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.7071// 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.74func Gt(a interface{}, b interface{}) bool {75 var left, right int6476 av := reflect.ValueOf(a)7778 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 }8687 bv := reflect.ValueOf(b)8889 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 }9798 return left > right99}100101// FIXME Eq is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.102103// Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.104func Eq(a interface{}, b interface{}) bool {105 av := reflect.ValueOf(a)106 bv := reflect.ValueOf(b)107108 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}11862func trimTrailingWhitespaces(s string) string {119func trimRightSpace(s string) string {120 return strings.TrimRightFunc(s, unicode.IsSpace)121}122123// FIXME appendIfNotPresent is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.124125// appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s.126func appendIfNotPresent(s, stringToAppend string) string {127 if strings.Contains(s, stringToAppend) {128 return s129 }130 return s + " " + stringToAppend131}132133// rpad adds padding to the right of a string.134func rpad(s string, padding int) string {135 template := fmt.Sprintf("%%-%ds", padding)136 return fmt.Sprintf(template, s)137}138139// tmpl executes the given template text on data, writing the result to w.140func 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}146147// ld compares two strings and returns the levenshtein distance between them.148func ld(s, t string, ignoreCase bool) int {
33 unmodified lines