forked from metalsoft-io/metalcloud-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_shell_completion.go
65 lines (56 loc) · 2.13 KB
/
cmd_shell_completion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"flag"
"fmt"
metalcloud "github.com/metalsoft-io/metal-cloud-sdk-go/v2"
)
var shellCompletionCmds = []Command{
{
Description: "Outputs bash/zsh autocompletion script",
Subject: "localshell",
AltSubject: "localshell",
Predicate: "autocomplete",
AltPredicate: "gen",
FlagSet: flag.NewFlagSet("shell get", flag.ExitOnError),
InitFunc: func(c *Command) {
c.Arguments = map[string]interface{}{
// "format": c.FlagSet.String("format", _nilDefaultStr, "The output format. Supported values are 'json','csv','yaml'. The default format is human readable."),
}
},
ExecuteFunc: shellCompletionCmd,
Endpoint: UserEndpoint,
},
}
func shellCompletionCmd(c *Command, client metalcloud.MetalCloudClient) (string, error) {
// if version == "" {
// return fmt.Sprintf("manual build\n"), nil
// }
script := `#/usr/bin/env bash
### This output should be redirected to /etc/bash_completion.d/metalcoud-cli
### or ~/.zshrc . Once done, reload the shell
_metalcloud-cli_completions()
{
local secondArg firstArg help thirdArg
firstArg="$(metalcloud-cli help|awk '{print $1}'|sort -u|egrep -v '^Accepted|^Syntax:|^$'|xargs)"
secondArg="$(metalcloud-cli help|grep "^\s\+${COMP_WORDS[1]}\ "|grep -oP '^\s+[\w\-]+\ [\w\-]+'|awk '{print $2}'|xargs)"
thirdArg="$(metalcloud-cli ${COMP_WORDS[1]} ${COMP_WORDS[2]} --help|grep -oP '\s+\-[\-\w]+'|awk '{print $1}'|xargs)"
helpArg="-help --help -h"
if [ "${#COMP_WORDS[@]}" -eq "4" ]; then
COMPREPLY=($(compgen -W "$helpArg $thirdArg" -- "${COMP_WORDS[3]}"))
fi
if [ "${#COMP_WORDS[@]}" -eq "3" ]; then
if [ "${COMP_WORDS[1]}" == 'apply' ] || [ "${COMP_WORDS[1]}" == 'delete' ];then
thirdArg="$(metalcloud-cli ${COMP_WORDS[1]} --help|grep -oP '\s+\-[\-\w]+'|awk '{print $1}'|xargs)"
COMPREPLY=($(compgen -W "$thirdArg" -- "${COMP_WORDS[2]}"))
else
COMPREPLY=($(compgen -W "$secondArg" -- "${COMP_WORDS[2]}"))
fi
fi
if [ "${#COMP_WORDS[@]}" -eq "2" ]; then
COMPREPLY=($(compgen -W "$firstArg" -- "${COMP_WORDS[1]}"))
fi
}
complete -F _metalcloud-cli_completions metalcloud-cli
`
return fmt.Sprintf(script), nil
}