New ๐ŸŒŠ wave for @helm

Commit bb3ccda6

bb3ccda64c2dMerged

zhilyaev <[email protected]> ยท 5 years ago ยท 3 files ยท +1 added ยท ~2 modified

Description

add completion

Details

Commit
bb3ccda64c2d
Parent
1193b2472931

cmd/helmwave/completion.goaddedView file

1+package main
2+
3+import (
4+ "errors"
5+ "fmt"
6+ "github.com/urfave/cli/v2"
7+)
8+
9+const (
10+ bash = `
11+#! /bin/bash
12+
13+: ${PROG:=helmwave}
14+
15+_cli_bash_autocomplete() {
16+ if [[ "${COMP_WORDS[0]}" != "source" ]]; then
17+ local cur opts base
18+ COMPREPLY=()
19+ cur="${COMP_WORDS[COMP_CWORD]}"
20+ if [[ "$cur" == "-"* ]]; then
21+ opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
22+ else
23+ opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
24+ fi
25+ COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
26+ return 0
27+ fi
28+}
29+
30+complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
31+unset PROG
32+`
33+
34+ zsh = `
35+#compdef helmwave
36+
37+_cli_zsh_autocomplete() {
38+
39+ local -a opts
40+ local cur
41+ cur=${words[-1]}
42+ if [[ "$cur" == "-"* ]]; then
43+ opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
44+ else
45+ opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
46+ fi
47+
48+ if [[ "${opts[1]}" != "" ]]; then
49+ _describe 'values' opts
50+ else
51+ _files
52+ fi
53+
54+ return
55+}
56+
57+compdef _cli_zsh_autocomplete helmwave
58+`
59+)
60+
61+var (
62+ ErrWrongShell = errors.New("wrong shell")
63+ ErrNotChose = errors.New("you did not specify a shell")
64+)
65+
66+func completion() *cli.Command {
67+ return &cli.Command{
68+ Name: "completion",
69+ Usage: "Generate completion script",
70+ Description: `
71+ echo "source <(helmwave completion bash)" >> ~/.bashrc
72+ echo "source <(helmwave completion zsh)" >> ~/.zshrc"
73+ `,
74+ Action: func(c *cli.Context) error {
75+ if 0 == c.Args().Len() {
76+ return ErrNotChose
77+ }
78+
79+ switch c.Args().First() {
80+ case "bash":
81+ fmt.Print(bash)
82+ return nil
83+ case "zsh":
84+ fmt.Print(zsh)
85+ return nil
86+ default:
87+ return ErrWrongShell
88+ }
89+ },
90+ }
91+}