2018-06-26 20:28:31 -05:00
|
|
|
package main
|
2017-07-30 10:42:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-06-27 10:53:11 -05:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-07-30 10:42:35 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-09-05 17:32:32 +01:00
|
|
|
// GenZshCompletionFile generates zsh completion file.
|
2017-09-05 13:20:51 -04:00
|
|
|
func (c *Command) GenZshCompletionFile(filename string) error {
|
2017-09-05 17:32:32 +01:00
|
|
|
outFile, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
2017-09-05 13:20:51 -04:00
|
|
|
return c.GenZshCompletion(outFile)
|
2017-09-05 17:32:32 +01:00
|
|
|
}
|
|
|
|
|
2017-07-30 10:42:35 +02:00
|
|
|
// GenZshCompletion generates a zsh completion file and writes to the passed writer.
|
2017-09-05 13:20:51 -04:00
|
|
|
func (c *Command) GenZshCompletion(w io.Writer) error {
|
2017-07-30 10:42:35 +02:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
2017-09-05 13:20:51 -04:00
|
|
|
writeHeader(buf, c)
|
|
|
|
maxDepth := maxDepth(c)
|
2017-07-30 10:42:35 +02:00
|
|
|
writeLevelMapping(buf, maxDepth)
|
2017-09-05 13:20:51 -04:00
|
|
|
writeLevelCases(buf, maxDepth, c)
|
2017-07-30 10:42:35 +02:00
|
|
|
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeHeader(w io.Writer, cmd *Command) {
|
|
|
|
fmt.Fprintf(w, "#compdef %s\n\n", cmd.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
func maxDepth(c *Command) int {
|
|
|
|
if len(c.Commands()) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
maxDepthSub := 0
|
|
|
|
for _, s := range c.Commands() {
|
|
|
|
subDepth := maxDepth(s)
|
|
|
|
if subDepth > maxDepthSub {
|
|
|
|
maxDepthSub = subDepth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1 + maxDepthSub
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeLevelMapping(w io.Writer, numLevels int) {
|
2018-06-27 10:53:11 -05:00
|
|
|
fmt.Fprintln(w, "local -a cmd_options")
|
2018-06-27 10:48:09 -05:00
|
|
|
fmt.Fprintln(w, `_arguments -C \`)
|
|
|
|
fmt.Fprintln(w, ` $jamf_pro_options \`)
|
2017-07-30 10:42:35 +02:00
|
|
|
for i := 1; i <= numLevels; i++ {
|
|
|
|
fmt.Fprintf(w, ` '%d: :->level%d' \`, i, i)
|
|
|
|
fmt.Fprintln(w)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, ` '%d: :%s'`, numLevels+1, "_files")
|
|
|
|
fmt.Fprintln(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeLevelCases(w io.Writer, maxDepth int, root *Command) {
|
|
|
|
fmt.Fprintln(w, "case $state in")
|
|
|
|
for i := 1; i <= maxDepth; i++ {
|
|
|
|
writeLevel(w, root, i)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, " *)")
|
|
|
|
fmt.Fprintln(w, " _arguments '*: :_files'")
|
|
|
|
fmt.Fprintln(w, " ;;")
|
2018-06-27 10:48:09 -05:00
|
|
|
fmt.Fprintln(w, "esac")
|
2017-07-30 10:42:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 10:48:09 -05:00
|
|
|
func writeLevel(w io.Writer, root *Command, level int) {
|
|
|
|
fmt.Fprintf(w, " level%d)\n", level)
|
|
|
|
fmt.Fprintf(w, " case $words[%d] in\n", level)
|
|
|
|
for _, c := range filterByLevel(root, level) {
|
|
|
|
writeCommandArgsBlock(w, c)
|
2017-07-30 10:42:35 +02:00
|
|
|
}
|
|
|
|
fmt.Fprintln(w, " *)")
|
|
|
|
fmt.Fprintln(w, " _arguments '*: :_files'")
|
|
|
|
fmt.Fprintln(w, " ;;")
|
2018-06-27 10:48:09 -05:00
|
|
|
fmt.Fprintln(w, " esac")
|
|
|
|
fmt.Fprintln(w, " ;;")
|
2017-07-30 10:42:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 10:48:09 -05:00
|
|
|
func writeCommandArgsBlock(w io.Writer, c *Command) {
|
|
|
|
names := commandNames(c)
|
|
|
|
flags := commandFlags(c)
|
|
|
|
if len(names) > 0 || len(flags) > 0 {
|
|
|
|
fmt.Fprintf(w, " %s)\n", c.Name())
|
|
|
|
defer fmt.Fprintln(w, " ;;")
|
2017-07-30 10:42:35 +02:00
|
|
|
}
|
2018-06-27 10:48:09 -05:00
|
|
|
if len(flags) > 0 {
|
2018-06-27 10:53:11 -05:00
|
|
|
fmt.Fprintln(w, " cmd_options=(")
|
2018-06-27 10:48:09 -05:00
|
|
|
for _, flag := range flags {
|
|
|
|
fmt.Fprintf(w, " %s\n", flag)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "\n )")
|
|
|
|
}
|
|
|
|
if len(names) > 0 {
|
|
|
|
fmt.Fprintf(w, " _values 'command' '%s'\n", strings.Join(names, "' '"))
|
2017-07-30 10:42:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 10:48:09 -05:00
|
|
|
func filterByLevel(c *Command, l int) []*Command {
|
|
|
|
commands := []*Command{c}
|
|
|
|
for i := 1; i < l; i++ {
|
|
|
|
var nextLevel []*Command
|
|
|
|
for _, c := range commands {
|
|
|
|
if c.HasSubCommands() {
|
|
|
|
nextLevel = append(nextLevel, c.Commands()...)
|
|
|
|
}
|
2017-07-30 10:42:35 +02:00
|
|
|
}
|
2018-06-27 10:48:09 -05:00
|
|
|
commands = nextLevel
|
2017-07-30 10:42:35 +02:00
|
|
|
}
|
2018-06-27 10:48:09 -05:00
|
|
|
|
|
|
|
return commands
|
2017-07-30 10:42:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 10:48:09 -05:00
|
|
|
func commandNames(command *Command) []string {
|
|
|
|
commands := command.Commands()
|
2017-07-30 10:42:35 +02:00
|
|
|
ns := make([]string, len(commands))
|
|
|
|
for i, c := range commands {
|
2018-06-26 20:28:31 -05:00
|
|
|
ns[i] = fmt.Sprintf("%s[%s]", c.Name(), c.Short)
|
|
|
|
}
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
2018-06-27 10:48:09 -05:00
|
|
|
func commandFlags(command *Command) []string {
|
2018-06-26 20:28:31 -05:00
|
|
|
flags := command.Flags()
|
2018-06-27 10:48:09 -05:00
|
|
|
ns := make([]string, 0)
|
|
|
|
flags.VisitAll(func(flag *pflag.Flag) {
|
|
|
|
if len(flag.Shorthand) > 0 {
|
|
|
|
ns = append(ns, fmt.Sprintf("{-%s,--%s}'[%s]'", flag.Shorthand, flag.Name, flag.Usage))
|
|
|
|
} else {
|
|
|
|
ns = append(ns, fmt.Sprintf("--%s'[%s]'", flag.Name, flag.Usage))
|
|
|
|
}
|
|
|
|
})
|
2017-07-30 10:42:35 +02:00
|
|
|
return ns
|
|
|
|
}
|