2013-09-03 22:54:51 +00:00
|
|
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Commands similar to git, go tools and other modern CLI tools
|
|
|
|
// inspired by go, go-Commander, gh and subcommand
|
|
|
|
|
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
2014-01-17 12:36:57 +00:00
|
|
|
"fmt"
|
2013-09-11 13:51:24 +00:00
|
|
|
"io"
|
2013-09-12 14:32:51 +00:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2013-09-03 22:54:51 +00:00
|
|
|
"strings"
|
2013-09-12 14:32:51 +00:00
|
|
|
"text/template"
|
2013-09-03 22:54:51 +00:00
|
|
|
)
|
|
|
|
|
2014-07-11 14:57:53 +00:00
|
|
|
var initializers []func()
|
2014-06-27 16:27:46 +00:00
|
|
|
|
2014-10-07 20:15:19 +00:00
|
|
|
// automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.
|
|
|
|
// Set this to true to enable it
|
|
|
|
var EnablePrefixMatching bool = false
|
|
|
|
|
2014-12-13 05:05:43 +00:00
|
|
|
//OnInitialize takes a series of func() arguments and appends them to a slice of func().
|
2014-07-11 14:57:53 +00:00
|
|
|
func OnInitialize(y ...func()) {
|
|
|
|
for _, x := range y {
|
|
|
|
initializers = append(initializers, x)
|
|
|
|
}
|
2014-06-27 16:27:46 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 05:05:43 +00:00
|
|
|
//Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,
|
|
|
|
//Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as
|
|
|
|
//ints and then compared.
|
2013-09-12 14:32:51 +00:00
|
|
|
func Gt(a interface{}, b interface{}) bool {
|
|
|
|
var left, right int64
|
|
|
|
av := reflect.ValueOf(a)
|
|
|
|
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
|
|
|
left = int64(av.Len())
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
left = av.Int()
|
|
|
|
case reflect.String:
|
|
|
|
left, _ = strconv.ParseInt(av.String(), 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
bv := reflect.ValueOf(b)
|
|
|
|
|
|
|
|
switch bv.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
|
|
|
right = int64(bv.Len())
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
right = bv.Int()
|
|
|
|
case reflect.String:
|
|
|
|
right, _ = strconv.ParseInt(bv.String(), 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
return left > right
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:05:43 +00:00
|
|
|
//Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.
|
2013-09-12 14:32:51 +00:00
|
|
|
func Eq(a interface{}, b interface{}) bool {
|
|
|
|
av := reflect.ValueOf(a)
|
|
|
|
bv := reflect.ValueOf(b)
|
|
|
|
|
|
|
|
switch av.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
|
|
|
panic("Eq called on unsupported type")
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
return av.Int() == bv.Int()
|
|
|
|
case reflect.String:
|
|
|
|
return av.String() == bv.String()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:05:43 +00:00
|
|
|
//rpad adds padding to the right of a string
|
2014-01-17 12:36:57 +00:00
|
|
|
func rpad(s string, padding int) string {
|
|
|
|
template := fmt.Sprintf("%%-%ds", padding)
|
|
|
|
return fmt.Sprintf(template, s)
|
|
|
|
}
|
|
|
|
|
2013-09-12 14:32:51 +00:00
|
|
|
// tmpl executes the given template text on data, writing the result to w.
|
|
|
|
func tmpl(w io.Writer, text string, data interface{}) error {
|
|
|
|
t := template.New("top")
|
|
|
|
t.Funcs(template.FuncMap{
|
|
|
|
"trim": strings.TrimSpace,
|
2014-01-17 12:36:57 +00:00
|
|
|
"rpad": rpad,
|
2013-09-12 14:32:51 +00:00
|
|
|
"gt": Gt,
|
|
|
|
"eq": Eq,
|
|
|
|
})
|
|
|
|
template.Must(t.Parse(text))
|
|
|
|
return t.Execute(w, data)
|
|
|
|
}
|