2017-07-30 08:42:35 +00:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
2018-03-23 10:09:56 +00:00
|
|
|
"encoding/json"
|
2017-07-30 08:42:35 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-09-05 16:32:32 +00:00
|
|
|
"os"
|
2018-03-23 10:09:56 +00:00
|
|
|
"sort"
|
2018-02-28 15:04:17 +00:00
|
|
|
"strings"
|
2018-02-24 16:53:13 +00:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2018-03-23 10:09:56 +00:00
|
|
|
const (
|
|
|
|
zshCompArgumentAnnotation = "cobra_annotations_zsh_completion_argument_annotation"
|
|
|
|
zshCompArgumentFilenameComp = "cobra_annotations_zsh_completion_argument_file_completion"
|
|
|
|
zshCompArgumentWordComp = "cobra_annotations_zsh_completion_argument_word_completion"
|
|
|
|
)
|
|
|
|
|
2018-02-24 16:53:13 +00:00
|
|
|
var (
|
2018-03-22 17:58:25 +00:00
|
|
|
zshCompFuncMap = template.FuncMap{
|
|
|
|
"genZshFuncName": zshCompGenFuncName,
|
|
|
|
"extractFlags": zshCompExtractFlag,
|
|
|
|
"genFlagEntryForZshArguments": zshCompGenFlagEntryForArguments,
|
2018-03-23 10:09:56 +00:00
|
|
|
"extractArgsCompletions": zshCompExtractArgumentCompletionHintsForRendering,
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
|
|
|
zshCompletionText = `
|
|
|
|
{{/* should accept Command (that contains subcommands) as parameter */}}
|
|
|
|
{{define "argumentsC" -}}
|
2018-02-28 10:49:53 +00:00
|
|
|
{{ $cmdPath := genZshFuncName .}}
|
|
|
|
function {{$cmdPath}} {
|
2018-02-26 20:31:06 +00:00
|
|
|
local -a commands
|
2018-02-24 16:53:13 +00:00
|
|
|
|
2018-02-26 20:31:06 +00:00
|
|
|
_arguments -C \{{- range extractFlags .}}
|
2018-02-28 15:04:17 +00:00
|
|
|
{{genFlagEntryForZshArguments .}} \{{- end}}
|
2018-02-26 20:31:06 +00:00
|
|
|
"1: :->cmnds" \
|
2018-02-24 16:53:13 +00:00
|
|
|
"*::arg:->args"
|
|
|
|
|
2018-02-26 20:31:06 +00:00
|
|
|
case $state in
|
|
|
|
cmnds)
|
|
|
|
commands=({{range .Commands}}{{if not .Hidden}}
|
2018-02-28 10:49:53 +00:00
|
|
|
"{{.Name}}:{{.Short}}"{{end}}{{end}}
|
2018-02-26 20:31:06 +00:00
|
|
|
)
|
|
|
|
_describe "command" commands
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
case "$words[1]" in {{- range .Commands}}{{if not .Hidden}}
|
2018-02-28 10:49:53 +00:00
|
|
|
{{.Name}})
|
|
|
|
{{$cmdPath}}_{{.Name}}
|
2018-02-26 20:31:06 +00:00
|
|
|
;;{{end}}{{end}}
|
|
|
|
esac
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
2018-02-26 20:31:06 +00:00
|
|
|
{{range .Commands}}{{if not .Hidden}}
|
2018-02-24 16:53:13 +00:00
|
|
|
{{template "selectCmdTemplate" .}}
|
2018-02-26 20:31:06 +00:00
|
|
|
{{- end}}{{end}}
|
2018-02-24 16:53:13 +00:00
|
|
|
{{- end}}
|
|
|
|
|
|
|
|
{{/* should accept Command without subcommands as parameter */}}
|
|
|
|
{{define "arguments" -}}
|
2018-02-28 10:49:53 +00:00
|
|
|
function {{genZshFuncName .}} {
|
2018-02-26 20:31:06 +00:00
|
|
|
{{" _arguments"}}{{range extractFlags .}} \
|
2018-02-28 15:04:17 +00:00
|
|
|
{{genFlagEntryForZshArguments . -}}
|
2018-03-23 10:09:56 +00:00
|
|
|
{{end}}{{range extractArgsCompletions .}} \
|
|
|
|
{{.}}{{end}}
|
2018-02-24 16:53:13 +00:00
|
|
|
}
|
2018-02-26 20:31:06 +00:00
|
|
|
{{end}}
|
2018-02-24 16:53:13 +00:00
|
|
|
|
2018-02-26 20:31:06 +00:00
|
|
|
{{/* dispatcher for commands with or without subcommands */}}
|
2018-02-24 16:53:13 +00:00
|
|
|
{{define "selectCmdTemplate" -}}
|
2018-02-25 12:12:58 +00:00
|
|
|
{{if .Hidden}}{{/* ignore hidden*/}}{{else -}}
|
2018-02-24 16:53:13 +00:00
|
|
|
{{if .Commands}}{{template "argumentsC" .}}{{else}}{{template "arguments" .}}{{end}}
|
|
|
|
{{- end}}
|
2018-02-25 12:12:58 +00:00
|
|
|
{{- end}}
|
2018-02-24 16:53:13 +00:00
|
|
|
|
2018-02-26 20:31:06 +00:00
|
|
|
{{/* template entry point */}}
|
2018-02-24 16:53:13 +00:00
|
|
|
{{define "Main" -}}
|
2018-02-28 10:49:53 +00:00
|
|
|
#compdef _{{.Name}} {{.Name}}
|
2018-02-24 16:53:13 +00:00
|
|
|
|
|
|
|
{{template "selectCmdTemplate" .}}
|
|
|
|
{{end}}
|
|
|
|
`
|
2017-07-30 08:42:35 +00:00
|
|
|
)
|
|
|
|
|
2018-03-23 10:09:56 +00:00
|
|
|
// zshCompArgsAnnotation is used to encode/decode zsh completion for
|
|
|
|
// arguments to/from Command.Annotations.
|
|
|
|
type zshCompArgsAnnotation map[int]zshCompArgHint
|
|
|
|
|
|
|
|
type zshCompArgHint struct {
|
|
|
|
// Indicates the type of the completion to use. One of:
|
|
|
|
// zshCompArgumentFilenameComp or zshCompArgumentWordComp
|
|
|
|
Tipe string `json:"type"`
|
|
|
|
|
|
|
|
// A value for the type above (globs for file completion or words)
|
|
|
|
Options []string `json:"options"`
|
|
|
|
}
|
|
|
|
|
2017-09-05 16:32:32 +00:00
|
|
|
// GenZshCompletionFile generates zsh completion file.
|
2017-09-05 17:20:51 +00:00
|
|
|
func (c *Command) GenZshCompletionFile(filename string) error {
|
2017-09-05 16:32:32 +00:00
|
|
|
outFile, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
2017-09-05 17:20:51 +00:00
|
|
|
return c.GenZshCompletion(outFile)
|
2017-09-05 16:32:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 21:56:31 +00:00
|
|
|
// GenZshCompletion generates a zsh completion file and writes to the passed
|
|
|
|
// writer. The completion always run on the root command regardless of the
|
|
|
|
// command it was called from.
|
2017-09-05 17:20:51 +00:00
|
|
|
func (c *Command) GenZshCompletion(w io.Writer) error {
|
2018-03-22 17:58:25 +00:00
|
|
|
tmpl, err := template.New("Main").Funcs(zshCompFuncMap).Parse(zshCompletionText)
|
2018-02-24 16:53:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating zsh completion template: %v", err)
|
|
|
|
}
|
2018-03-04 21:56:31 +00:00
|
|
|
return tmpl.Execute(w, c.Root())
|
2017-07-30 08:42:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 10:09:56 +00:00
|
|
|
// MarkZshCompPositionalArgumentFile marks the specified argument (first
|
|
|
|
// argument is 1) as completed by file selection. patterns (e.g. "*.txt") are
|
|
|
|
// optional - if not provided the completion will search for all files.
|
|
|
|
func (c *Command) MarkZshCompPositionalArgumentFile(argPosition int, patterns ...string) error {
|
|
|
|
if argPosition < 1 {
|
|
|
|
return fmt.Errorf("Invalid argument position (%d)", argPosition)
|
|
|
|
}
|
|
|
|
annotation, err := c.zshCompGetArgsAnnotations()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.zshcompArgsAnnotationnIsDuplicatePosition(annotation, argPosition) {
|
|
|
|
return fmt.Errorf("Duplicate annotation for positional argument at index %d", argPosition)
|
|
|
|
}
|
|
|
|
annotation[argPosition] = zshCompArgHint{
|
|
|
|
Tipe: zshCompArgumentFilenameComp,
|
|
|
|
Options: patterns,
|
|
|
|
}
|
|
|
|
return c.zshCompSetArgsAnnotations(annotation)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkZshCompPositionalArgumentWords marks the specified positional argument
|
|
|
|
// (first argument is 1) as completed by the provided words. At east one word
|
|
|
|
// must be provided, spaces within words will be offered completion with
|
|
|
|
// "word\ word".
|
|
|
|
func (c *Command) MarkZshCompPositionalArgumentWords(argPosition int, words ...string) error {
|
|
|
|
if argPosition < 1 {
|
|
|
|
return fmt.Errorf("Invalid argument position (%d)", argPosition)
|
|
|
|
}
|
|
|
|
if len(words) == 0 {
|
|
|
|
return fmt.Errorf("Trying to set empty word list for positional argument %d", argPosition)
|
|
|
|
}
|
|
|
|
annotation, err := c.zshCompGetArgsAnnotations()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.zshcompArgsAnnotationnIsDuplicatePosition(annotation, argPosition) {
|
|
|
|
return fmt.Errorf("Duplicate annotation for positional argument at index %d", argPosition)
|
|
|
|
}
|
|
|
|
annotation[argPosition] = zshCompArgHint{
|
|
|
|
Tipe: zshCompArgumentWordComp,
|
|
|
|
Options: words,
|
|
|
|
}
|
|
|
|
return c.zshCompSetArgsAnnotations(annotation)
|
|
|
|
}
|
|
|
|
|
|
|
|
func zshCompExtractArgumentCompletionHintsForRendering(c *Command) ([]string, error) {
|
|
|
|
var result []string
|
|
|
|
annotation, err := c.zshCompGetArgsAnnotations()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range annotation {
|
|
|
|
s, err := zshCompRenderZshCompArgHint(k, v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, s)
|
|
|
|
}
|
|
|
|
if len(c.ValidArgs) > 0 {
|
|
|
|
if _, positionOneExists := annotation[1]; !positionOneExists {
|
|
|
|
s, err := zshCompRenderZshCompArgHint(1, zshCompArgHint{
|
|
|
|
Tipe: zshCompArgumentWordComp,
|
|
|
|
Options: c.ValidArgs,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(result)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func zshCompRenderZshCompArgHint(i int, z zshCompArgHint) (string, error) {
|
|
|
|
switch t := z.Tipe; t {
|
|
|
|
case zshCompArgumentFilenameComp:
|
|
|
|
var globs []string
|
|
|
|
for _, g := range z.Options {
|
|
|
|
globs = append(globs, fmt.Sprintf(`-g "%s"`, g))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(`'%d: :_files %s'`, i, strings.Join(globs, " ")), nil
|
|
|
|
case zshCompArgumentWordComp:
|
|
|
|
var words []string
|
|
|
|
for _, w := range z.Options {
|
|
|
|
words = append(words, fmt.Sprintf("%q", w))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(`'%d: :(%s)'`, i, strings.Join(words, " ")), nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("Invalid zsh argument completion annotation: %s", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) zshcompArgsAnnotationnIsDuplicatePosition(annotation zshCompArgsAnnotation, position int) bool {
|
|
|
|
_, dup := annotation[position]
|
|
|
|
return dup
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) zshCompGetArgsAnnotations() (zshCompArgsAnnotation, error) {
|
|
|
|
annotation := make(zshCompArgsAnnotation)
|
|
|
|
annotationString, ok := c.Annotations[zshCompArgumentAnnotation]
|
|
|
|
if !ok {
|
|
|
|
return annotation, nil
|
|
|
|
}
|
|
|
|
err := json.Unmarshal([]byte(annotationString), &annotation)
|
|
|
|
if err != nil {
|
|
|
|
return annotation, fmt.Errorf("Error unmarshaling zsh argument annotation: %v", err)
|
|
|
|
}
|
|
|
|
return annotation, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) zshCompSetArgsAnnotations(annotation zshCompArgsAnnotation) error {
|
|
|
|
jsn, err := json.Marshal(annotation)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error marshaling zsh argument annotation: %v", err)
|
|
|
|
}
|
|
|
|
if c.Annotations == nil {
|
|
|
|
c.Annotations = make(map[string]string)
|
|
|
|
}
|
|
|
|
c.Annotations[zshCompArgumentAnnotation] = string(jsn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
func zshCompGenFuncName(c *Command) string {
|
2018-02-28 10:49:53 +00:00
|
|
|
if c.HasParent() {
|
2018-03-22 17:58:25 +00:00
|
|
|
return zshCompGenFuncName(c.Parent()) + "_" + c.Name()
|
2017-07-30 08:42:35 +00:00
|
|
|
}
|
2018-02-28 10:49:53 +00:00
|
|
|
return "_" + c.Name()
|
2017-07-30 08:42:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
func zshCompExtractFlag(c *Command) []*pflag.Flag {
|
2018-02-24 16:53:13 +00:00
|
|
|
var flags []*pflag.Flag
|
|
|
|
c.LocalFlags().VisitAll(func(f *pflag.Flag) {
|
2018-02-25 12:12:58 +00:00
|
|
|
if !f.Hidden {
|
|
|
|
flags = append(flags, f)
|
|
|
|
}
|
2018-02-24 16:53:13 +00:00
|
|
|
})
|
|
|
|
c.InheritedFlags().VisitAll(func(f *pflag.Flag) {
|
2018-02-25 12:12:58 +00:00
|
|
|
if !f.Hidden {
|
|
|
|
flags = append(flags, f)
|
|
|
|
}
|
2018-02-24 16:53:13 +00:00
|
|
|
})
|
|
|
|
return flags
|
2017-07-30 08:42:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
// zshCompGenFlagEntryForArguments returns an entry that matches _arguments
|
2018-02-28 15:04:17 +00:00
|
|
|
// zsh-completion parameters. It's too complicated to generate in a template.
|
2018-03-22 17:58:25 +00:00
|
|
|
func zshCompGenFlagEntryForArguments(f *pflag.Flag) string {
|
2018-02-28 15:04:17 +00:00
|
|
|
if f.Name == "" || f.Shorthand == "" {
|
2018-03-22 17:58:25 +00:00
|
|
|
return zshCompGenFlagEntryForSingleOptionFlag(f)
|
2018-02-28 15:04:17 +00:00
|
|
|
}
|
2018-03-22 17:58:25 +00:00
|
|
|
return zshCompGenFlagEntryForMultiOptionFlag(f)
|
2018-02-28 15:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
func zshCompGenFlagEntryForSingleOptionFlag(f *pflag.Flag) string {
|
2018-02-28 15:04:17 +00:00
|
|
|
var option, multiMark, extras string
|
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
if zshCompFlagCouldBeSpecifiedMoreThenOnce(f) {
|
2018-02-28 15:04:17 +00:00
|
|
|
multiMark = "*"
|
|
|
|
}
|
|
|
|
|
|
|
|
option = "--" + f.Name
|
|
|
|
if option == "--" {
|
|
|
|
option = "-" + f.Shorthand
|
|
|
|
}
|
2018-03-22 17:58:25 +00:00
|
|
|
extras = zshCompGenFlagEntryExtras(f)
|
2018-02-28 15:04:17 +00:00
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
return fmt.Sprintf(`'%s%s[%s]%s'`, multiMark, option, zshCompQuoteFlagDescription(f.Usage), extras)
|
2018-02-28 15:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
func zshCompGenFlagEntryForMultiOptionFlag(f *pflag.Flag) string {
|
2018-02-28 15:04:17 +00:00
|
|
|
var options, parenMultiMark, curlyMultiMark, extras string
|
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
if zshCompFlagCouldBeSpecifiedMoreThenOnce(f) {
|
2018-02-28 15:04:17 +00:00
|
|
|
parenMultiMark = "*"
|
|
|
|
curlyMultiMark = "\\*"
|
|
|
|
}
|
|
|
|
|
2018-03-02 06:42:52 +00:00
|
|
|
options = fmt.Sprintf(`'(%s-%s %s--%s)'{%s-%s,%s--%s}`,
|
2018-02-28 15:04:17 +00:00
|
|
|
parenMultiMark, f.Shorthand, parenMultiMark, f.Name, curlyMultiMark, f.Shorthand, curlyMultiMark, f.Name)
|
2018-03-22 17:58:25 +00:00
|
|
|
extras = zshCompGenFlagEntryExtras(f)
|
2018-02-28 15:04:17 +00:00
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
return fmt.Sprintf(`%s'[%s]%s'`, options, zshCompQuoteFlagDescription(f.Usage), extras)
|
2018-02-28 15:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
func zshCompGenFlagEntryExtras(f *pflag.Flag) string {
|
2018-02-28 15:04:17 +00:00
|
|
|
var extras string
|
|
|
|
|
2018-03-02 06:42:52 +00:00
|
|
|
globs, pathSpecified := f.Annotations[BashCompFilenameExt]
|
2018-02-28 15:04:17 +00:00
|
|
|
if pathSpecified {
|
|
|
|
extras = ":filename:_files"
|
2018-03-02 06:42:52 +00:00
|
|
|
for _, g := range globs {
|
|
|
|
extras = extras + fmt.Sprintf(` -g "%s"`, g)
|
|
|
|
}
|
2018-02-28 19:52:35 +00:00
|
|
|
} else if f.NoOptDefVal == "" {
|
2018-02-28 15:04:17 +00:00
|
|
|
extras = ":" // allow option variable without assisting
|
|
|
|
}
|
|
|
|
|
|
|
|
return extras
|
|
|
|
}
|
2018-02-28 19:52:35 +00:00
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
func zshCompFlagCouldBeSpecifiedMoreThenOnce(f *pflag.Flag) bool {
|
2018-02-28 19:52:35 +00:00
|
|
|
return strings.Contains(f.Value.Type(), "Slice") ||
|
|
|
|
strings.Contains(f.Value.Type(), "Array")
|
|
|
|
}
|
2018-03-17 18:55:27 +00:00
|
|
|
|
2018-03-22 17:58:25 +00:00
|
|
|
func zshCompQuoteFlagDescription(s string) string {
|
2018-03-17 18:55:27 +00:00
|
|
|
return strings.Replace(s, "'", `'\''`, -1)
|
|
|
|
}
|