-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathcompletion.py
More file actions
170 lines (143 loc) · 4.36 KB
/
completion.py
File metadata and controls
170 lines (143 loc) · 4.36 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/local/bin/python3
"""
Contains any code relevant to generating/updating shell completions for linode-cli
"""
from string import Template
from openapi3 import OpenAPI
def get_completions(ops, help_flag, action):
"""
Handle shell completions based on `linode-cli completion ____`
"""
if help_flag or not action:
return (
"linode-cli completion [SHELL]\n\n"
"Prints shell completions for the requested shell to stdout.\n"
"Currently, only completions for bash, fish, and zsh are available."
)
if action == "bash":
return get_bash_completions(ops)
if action == "fish":
return get_fish_completions(ops)
if action == "zsh":
return get_zsh_completions(ops)
return (
"Completions are only available for bash, fish, and zsh at this time.\n\n"
"To retrieve these, please invoke as\n"
"`linode-cli completion bash`, `linode-cli completion fish`, or `linode-cli completion zsh`"
)
def get_zsh_completions(ops):
"""
Generates and returns Zsh shell completions based on the baked spec
"""
completion_template = Template(
"""#compdef linode-cli linode lin
# This is a generated file by Linode-CLI! Do not modify!
local -a subcommands
subcommands=(
'$subcommands --help'
)
local -a command
local -a opts
_arguments -C \\
"1: :($subcommands)" \\
'*:: :->subcmds' && return 0
if (( CURRENT == 2 )); then
case $words[1] in
$command_items
esac
fi
"""
)
command_template = Template(
"""$command)
command=(
'$actions --help'
)
_describe -t commands "$command command" command
;;"""
)
command_blocks = [
command_template.safe_substitute(
command=op, actions=" ".join(list(actions.keys()))
)
for op, actions in ops.items()
]
rendered = completion_template.safe_substitute(
subcommands=" ".join(ops.keys()),
command_items="\n".join(command_blocks),
)
return rendered
def get_fish_completions(ops):
"""
Generates and returns fish shell completions based on the baked spec
"""
completion_template = Template(
"""# This is a generated file by Linode-CLI! Do not modify!
complete -c linode-cli -n "not __fish_seen_subcommand_from $subcommands" -x -a '$subcommands --help'
complete -c linode -n "not __fish_seen_subcommand_from $subcommands" -x -a '$subcommands --help'
complete -c lin -n "not __fish_seen_subcommand_from $subcommands" -x -a '$subcommands --help'
$command_items"""
)
command_template = Template(
"""complete -c linode-cli -n "__fish_seen_subcommand_from $command" \
-x -a '$actions --help'
complete -c linode -n "__fish_seen_subcommand_from $command" \
-x -a '$actions --help'
complete -c lin -n "__fish_seen_subcommand_from $command" \
-x -a '$actions --help'"""
)
command_blocks = [
command_template.safe_substitute(
command=op, actions=" ".join(list(actions.keys()))
)
for op, actions in ops.items()
]
rendered = completion_template.safe_substitute(
subcommands=" ".join(ops.keys()),
command_items="\n".join(command_blocks),
)
return rendered
def get_bash_completions(ops):
"""
Generates and returns bash shell completions based on the baked spec
"""
completion_template = Template(
"""# This is a generated file by Linode-CLI! Do not modify!
_linode_cli()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
case "${prev}" in
linode-cli | linode | lin)
COMPREPLY=( $(compgen -W "$actions --help" -- ${cur}) )
return 0
;;
$command_items
*)
;;
esac
}
complete -F _linode_cli linode-cli
complete -F _linode_cli linode
complete -F _linode_cli lin"""
)
command_template = Template(
"""$command)
COMPREPLY=( $(compgen -W "$actions --help" -- ${cur}) )
return 0
;;"""
)
command_blocks = [
command_template.safe_substitute(
command=op, actions=" ".join(list(actions.keys()))
)
for op, actions in ops.items()
if not isinstance(actions, OpenAPI)
]
rendered = completion_template.safe_substitute(
actions=" ".join(ops.keys()),
command_items="\n ".join(command_blocks),
)
return rendered