Files
linuxkit/vendor/github.com/docker/infrakit/pkg/template/help.go
Rolf Neugebauer 6a29d153f5 infrakit: Move the hyperkit instance plugin into the source directory
- The tools directory ideally should not contain source code
- Removes double vendoring of packagages
- Makes it easer to hook the build into the top-level Makefile

Eventually, the plugin should be moved to the infrakit repo.

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
2017-03-25 13:02:45 +01:00

84 lines
1.6 KiB
Go

package template
import (
"fmt"
"reflect"
"strings"
)
// UpdateDocumentation uses reflection to generate documentation on usage and function signature.
func UpdateDocumentation(in []Function) []Function {
out := []Function{}
for _, f := range in {
copy := f
copy.Function = functionSignature(f.Name, f.Func)
copy.Usage = functionUsage(f.Name, f.Func)
if len(f.Description) == 0 {
copy.Description = []string{"None"}
}
out = append(out, copy)
}
return out
}
func isFunc(f interface{}) (string, bool) {
if f == nil {
return "no-function", false
}
ft := reflect.TypeOf(f)
if ft.Kind() != reflect.Func {
return "not-a-function", false
}
return ft.String(), true
}
func functionSignature(name string, f interface{}) string {
s, is := isFunc(f)
if !is {
return s
}
return s
}
func functionUsage(name string, f interface{}) string {
if s, is := isFunc(f); !is {
return s
}
ft := reflect.TypeOf(f)
if ft.Kind() != reflect.Func {
return "not-a-function"
}
args := make([]string, ft.NumIn())
for i := 0; i < len(args); i++ {
t := ft.In(i)
v := ""
switch {
case t == reflect.TypeOf(""):
v = fmt.Sprintf("\"%s\"", t.Name())
case t.Kind() == reflect.Slice && i == len(args)-1:
tt := t.Elem().Name()
if t.Elem() == reflect.TypeOf("") {
tt = fmt.Sprintf("\"%s\"", t.Name())
}
v = fmt.Sprintf("[ %s ... ]", tt)
case t.String() == "interface {}":
v = "any"
default:
v = strings.Replace(t.String(), " ", "", -1)
}
args[i] = v
}
arglist := strings.Join(args, " ")
if len(arglist) > 0 {
arglist = arglist + " "
}
return fmt.Sprintf("{{ %s %s}}", name, arglist)
}