mirror of
https://github.com/docker/cli.git
synced 2026-11-03 12:13:26 +03:00
These utilities are used by CLI-plugins; separate them from the render code, which is used by teh CLI-plugin manager. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
48 lines
966 B
Go
48 lines
966 B
Go
package hooks
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"strings"
|
|
"text/template"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func ParseTemplate(hookTemplate string, cmd *cobra.Command) ([]string, error) {
|
|
tmpl := template.New("").Funcs(commandFunctions)
|
|
tmpl, err := tmpl.Parse(hookTemplate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
b := bytes.Buffer{}
|
|
err = tmpl.Execute(&b, cmd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return strings.Split(b.String(), "\n"), nil
|
|
}
|
|
|
|
var ErrHookTemplateParse = errors.New("failed to parse hook template")
|
|
|
|
var commandFunctions = template.FuncMap{
|
|
"flag": getFlagValue,
|
|
"arg": getArgValue,
|
|
}
|
|
|
|
func getFlagValue(cmd *cobra.Command, flag string) (string, error) {
|
|
cmdFlag := cmd.Flag(flag)
|
|
if cmdFlag == nil {
|
|
return "", ErrHookTemplateParse
|
|
}
|
|
return cmdFlag.Value.String(), nil
|
|
}
|
|
|
|
func getArgValue(cmd *cobra.Command, i int) (string, error) {
|
|
flags := cmd.Flags()
|
|
if flags == nil {
|
|
return "", ErrHookTemplateParse
|
|
}
|
|
return flags.Arg(i), nil
|
|
}
|