-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathhelp_pages.py
More file actions
356 lines (278 loc) · 10.2 KB
/
help_pages.py
File metadata and controls
356 lines (278 loc) · 10.2 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"""
This module contains various helper functions related to outputting
help pages.
"""
import sys
import textwrap
from collections import defaultdict
from typing import Dict, List, Optional
from rich import box
from rich import print as rprint
from rich.console import Console
from rich.padding import Padding
from rich.table import Column, Table
from rich.text import Text
from linodecli.baked import OpenAPIOperation
from linodecli.baked.request import OpenAPIRequestArg
from linodecli.exit_codes import ExitCodes
from linodecli.plugins import plugins
HELP_ENV_VARS = {
"LINODE_CLI_TOKEN": "A Linode Personal Access Token for the CLI to make requests with. "
"If specified, the configuration step will be skipped.",
"LINODE_CLI_CA": "The path to a custom Certificate Authority file to verify "
"API requests against.",
"LINODE_CLI_API_HOST": "Overrides the target host for API requests. "
"(e.g. 'api.linode.com')",
"LINODE_CLI_API_VERSION": "Overrides the target Linode API version for API requests. "
"(e.g. 'v4beta')",
"LINODE_CLI_API_SCHEME": "Overrides the target scheme used for API requests. "
"(e.g. 'https')",
"LINODE_CLI_CONFIG": "Overrides the default configuration file path. "
"(e.g '~/.linode/my-cli-config')",
}
HELP_TOPICS = {
"env-vars": "Environment variables that can be used",
"commands": "Learn about all available commands with linode-cli",
"plugins": " Learn about all available plugins registered to linode-cli",
}
def print_help_env_vars():
"""
Print Environment variables overrides. Usage:
linode-cli env-vars
"""
rprint("\n[bold cyan]Environment variables:")
table = Table(show_header=True, header_style="bold", box=box.SQUARE)
table.add_column("Name")
table.add_column("Description")
for k, v in HELP_ENV_VARS.items():
table.add_row(k, v)
rprint(table)
def print_help_commands(ops):
"""
Prints available commands. Usage:
linode-cli commands
"""
# commands to manage CLI users (don't call out to API)
rprint("\n[bold cyan]CLI user management commands:")
um_commands = [["configure", "set-user", "show-users"], ["remove-user"]]
table = Table(show_header=False)
for cmd in um_commands:
table.add_row(*cmd)
rprint(table)
# commands to manage plugins (don't call out to API)
rprint("\n[bold cyan]CLI Plugin management commands:")
pm_commands = [["register-plugin", "remove-plugin"]]
table = Table(show_header=False)
for cmd in pm_commands:
table.add_row(*cmd)
rprint(table)
# other CLI commands
rprint("\n[bold cyan]Other CLI commands:")
other_commands = [["completion"]]
table = Table(show_header=False)
for cmd in other_commands:
table.add_row(*cmd)
rprint(table)
# commands generated from the spec (call the API directly)
rprint("\n[bold cyan]Available commands:")
content = list(sorted(ops.keys()))
proc = []
for i in range(0, len(content), 3):
proc.append(content[i : i + 3])
if content[i + 3 :]:
proc.append(content[i + 3 :])
table = Table(show_header=False)
for cmd in proc:
table.add_row(*cmd)
rprint(table)
def print_help_plugins(config):
"""
Print available plugins registered to the CLI (do arbitrary things). Usage:
linode-cli plugins
"""
if plugins.available(config):
# only show this if there are any available plugins
rprint("\n[bold cyan]Available plugins:")
plugin_content = list(plugins.available(config))
plugin_proc = []
for i in range(0, len(plugin_content), 3):
plugin_proc.append(plugin_content[i : i + 3])
if plugin_content[i + 3 :]:
plugin_proc.append(plugin_content[i + 3 :])
plugin_table = Table(show_header=False)
for plugin in plugin_proc:
plugin_table.add_row(*plugin)
rprint(plugin_table)
def print_help_default():
"""
Prints help output with options from the API spec
"""
rprint("\n[bold cyan]Help Topics")
for k, v in HELP_TOPICS.items():
print(" " + k + ": " + v)
rprint("\n[bold]To reconfigure[/], call `linode-cli configure`")
print(
"For comprehensive documentation, "
"visit https://www.linode.com/docs/api/"
)
def print_help_command_actions(
ops: Dict[str, Dict[str, OpenAPIOperation]],
command: Optional[str],
file=sys.stdout,
):
"""
Prints the help page for a single command, including all actions
under the given command.
:param ops: A dictionary mapping CLI commands -> actions -> operations.
:param command: The command to print the help page for.
"""
print(f"linode-cli {command} [ACTION]\n\nAvailable actions: ", file=file)
content = [
[", ".join([action, *op.action_aliases]), op.summary]
for action, op in sorted(ops[command].items(), key=lambda v: v[0])
]
table = Table(
Column(header="action", no_wrap=True),
Column(header="summary", style="cyan"),
)
for row in content:
table.add_row(*row)
rprint(table, file=file)
def print_help_action(
cli: "CLI", command: Optional[str], action: Optional[str]
):
"""
Prints help relevant to the command and action
"""
try:
op = cli.find_operation(command, action)
except ValueError as exc:
print(exc, file=sys.stderr)
sys.exit(ExitCodes.UNRECOGNIZED_ACTION)
console = Console(highlight=False)
console.print(f"[bold]linode-cli {command} {action}[/]", end="")
for param in op.params:
pname = param.name.upper()
console.print(f" [{pname}]", end="")
console.print()
console.print(f"[cyan]{op.summary}[/]")
if op.docs_url:
console.print(
f"[bold]API Documentation[/]: [link={op.docs_url}]{op.docs_url}[/link]"
)
if len(op.samples) > 0:
console.print()
console.print(
f"[bold]Example Usage{'s' if len(op.samples) > 1 else ''}: [/]"
)
console.print(
*[
# Indent all samples for readability; strip and trailing newlines
textwrap.indent(v.get("source").rstrip(), " ")
for v in op.samples
],
sep="\n\n",
highlight=True,
)
console.print()
if op.method == "get" and op.response_model.is_paginated:
_help_action_print_filter_args(console, op)
return
if len(op.arg_routes) > 0:
# This operation uses oneOf so we need to render routes
# instead of the operation-level argument list.
for title, option in op.arg_routes.items():
_help_action_print_body_args(console, op, option, title=title)
elif op.args:
_help_action_print_body_args(console, op, op.args)
def _help_action_print_filter_args(console: Console, op: OpenAPIOperation):
"""
Pretty-prints all the filter (GET) arguments for this operation.
"""
filterable_attrs = [
attr for attr in op.response_model.attrs if attr.filterable
]
if filterable_attrs:
console.print("[bold]You may filter results with:[/]")
for attr in filterable_attrs:
console.print(f" [bold green]--{attr.name}[/]")
console.print(
"\nAdditionally, you may order results using --order-by and --order."
)
def _help_action_print_body_args(
console: Console,
op: OpenAPIOperation,
args: List[OpenAPIRequestArg],
title: Optional[str] = None,
):
"""
Pretty-prints all the body (POST/PUT) arguments for this operation.
"""
console.print(f"[bold]Arguments{f' ({title})' if title else ''}:[/]")
for group in _help_group_arguments(args):
for arg in group:
metadata = []
if op.method in {"post", "put"} and arg.required:
metadata.append("required")
if arg.format == "json":
metadata.append("JSON")
if arg.nullable:
metadata.append("nullable")
if arg.is_parent:
metadata.append("conflicts with children")
prefix = f" ({', '.join(metadata)})" if len(metadata) > 0 else ""
arg_text = Text.from_markup(
f"[bold green]--{arg.path}[/][bold]{prefix}:[/] {arg.description_rich}"
)
console.print(
Padding.indent(arg_text, (arg.depth * 2) + 2),
)
console.print()
def _help_group_arguments(
args: List[OpenAPIRequestArg],
) -> List[List[OpenAPIRequestArg]]:
"""
Returns help page groupings for a list of POST/PUT arguments.
"""
args = [arg for arg in args if not arg.read_only]
args_sorted = sorted(args, key=lambda a: a.path)
paths = {tuple(arg.path.split(".")) for arg in args_sorted}
path_to_args = defaultdict(list)
for arg in args_sorted:
arg_path = tuple(arg.path.split("."))
if not arg.is_parent:
# Parent arguments are grouped in with their children
arg_path = arg_path[:-1]
# Find first common parent
while len(arg_path) > 1 and arg_path not in paths:
arg_path = arg_path[:-1]
path_to_args[arg_path].append(arg)
group_required = []
groups = []
ungrouped = []
for k, group in sorted(
path_to_args.items(), key=lambda a: (len(a[0]), a[0], len(a[1]))
):
if len(k) > 0 and len(group) > 1:
# This is a named subgroup
groups.append(
# Args should be ordered by least depth -> required -> path
sorted(group, key=lambda v: (v.depth, not v.required, v.path)),
)
continue
# If the group's argument is required,
# add it to the top-level required group
for arg in group:
if arg.required:
group_required.append(arg)
continue
# Add ungrouped arguments (single value groups) to the
# "ungrouped" group.
ungrouped.append(arg)
result = []
if len(group_required) > 0:
result.append(sorted(group_required, key=lambda v: v.path))
if len(ungrouped) > 0:
result.append(sorted(ungrouped, key=lambda v: v.path))
result += groups
return result