mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
// 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 (
|
|
"io"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"text/template"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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,
|
|
"gt": Gt,
|
|
"eq": Eq,
|
|
})
|
|
template.Must(t.Parse(text))
|
|
return t.Execute(w, data)
|
|
}
|