-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy path__init__.py
More file actions
executable file
·234 lines (200 loc) · 7.71 KB
/
__init__.py
File metadata and controls
executable file
·234 lines (200 loc) · 7.71 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
#!/usr/local/bin/python3
"""
Argument parser for the linode CLI
"""
import argparse
import os
import sys
from importlib.metadata import version
from sys import argv
from rich import print as rprint
from rich.table import Column, Table
from linodecli import plugins
from linodecli.exit_codes import ExitCodes
from .arg_helpers import register_args, register_plugin, remove_plugin
from .cli import CLI
from .completion import get_completions
from .configuration import ENV_TOKEN_NAME
from .help_pages import (
HELP_TOPICS,
print_help_action,
print_help_command_actions,
print_help_commands,
print_help_default,
print_help_env_vars,
print_help_plugins,
)
from .helpers import handle_url_overrides
from .output.output_handler import OutputMode
from .version import __version__
VERSION = __version__
BASE_URL = "https://api.linode.com/v4"
TEST_MODE = os.getenv("LINODE_CLI_TEST_MODE") == "1"
# if any of these arguments are given, we don't need to prompt for configuration
skip_config = (
any(
c in argv
for c in ["--skip-config", "--help", "--version", "completion"]
)
or TEST_MODE
)
cli = CLI(
VERSION,
handle_url_overrides(BASE_URL, override_path=True),
skip_config=skip_config,
)
def main(): # pylint: disable=too-many-branches,too-many-statements
"""
Handle incoming command arguments
"""
parser = argparse.ArgumentParser(
"linode-cli",
add_help=False,
description="The Linode Command Line Interface.\n\nAliases: lin, linode",
)
parsed, args = register_args(parser).parse_known_args()
cli.output_handler.configure(parsed, cli.suppress_warnings)
if parsed.all_rows:
cli.pagination = False
cli.defaults = not parsed.no_defaults
cli.retry_count = 0
cli.no_retry = parsed.no_retry
cli.suppress_warnings = parsed.suppress_warnings
cli.page = parsed.page
cli.page_size = parsed.page_size
cli.debug_request = parsed.debug
if parsed.as_user and not skip_config:
cli.config.set_user(parsed.as_user)
if parsed.version:
if not parsed.command:
# print version info and exit - but only if no command was given
print(f"linode-cli {VERSION}")
print(f"Built from spec version {cli.spec_version}")
sys.exit(ExitCodes.SUCCESS)
else:
# something else might want to parse version
# find where it was originally, as it was removed from args
index = argv.index("--version") - 3 # executable command action
args = args[:index] + ["--version"] + args[index:]
# handle a bake - this is used to parse a spec and bake it as a pickle
if parsed.command == "bake":
if parsed.action is None:
print("No spec provided, cannot bake", file=sys.stderr)
sys.exit(ExitCodes.ARGUMENT_ERROR)
cli.bake(parsed.action)
sys.exit(ExitCodes.SUCCESS)
elif cli.ops is None:
# if not spec was found and we weren't baking, we're doomed
sys.exit(ExitCodes.ARGUMENT_ERROR)
if parsed.command == "register-plugin":
if parsed.action is None:
print("register-plugin requires a module name!", file=sys.stderr)
sys.exit(ExitCodes.ARGUMENT_ERROR)
msg, code = register_plugin(parsed.action, cli.config, cli.ops)
print(msg)
sys.exit(code)
if parsed.command == "remove-plugin":
if parsed.action is None:
print(
"remove-plugin requires a plugin name to remove!",
file=sys.stderr,
)
sys.exit(ExitCodes.ARGUMENT_ERROR)
msg, code = remove_plugin(parsed.action, cli.config)
print(msg)
sys.exit(code)
if parsed.command == "completion":
print(get_completions(cli.ops, parsed.help, parsed.action))
sys.exit(ExitCodes.SUCCESS)
# handle a help for the CLI
if parsed.command is None or (parsed.command is None and parsed.help):
parser.print_help()
print_help_default()
sys.exit(ExitCodes.SUCCESS)
if parsed.command == "env-vars":
print_help_env_vars()
sys.exit(ExitCodes.SUCCESS)
if parsed.command == "commands":
print_help_commands(cli.ops)
sys.exit(ExitCodes.SUCCESS)
if parsed.command == "plugins":
print_help_plugins(cli.config)
sys.exit(ExitCodes.SUCCESS)
# configure
if parsed.command == "configure":
if parsed.help:
print(
"linode-cli configure\n\n"
"Configured the Linode CLI. This command can be used to change\n"
"defaults selected for the current user, or to configure additional users."
)
else:
cli.configure()
sys.exit(ExitCodes.SUCCESS)
# block of commands for user-focused operations
if parsed.command == "set-user":
if parsed.help or not parsed.action:
print(
"linode-cli set-user [USER]\n\n"
"Sets the active user for the CLI out of users you have configured.\n"
"To configure a new user, see `linode-cli configure`"
)
else:
cli.config.set_default_user(parsed.action)
sys.exit(ExitCodes.SUCCESS)
if parsed.command == "show-users":
if parsed.help:
print(
"linode-cli show-users\n\n"
"Lists configured users. Configured users can be set as the\n"
"active user (used for all commands going forward) with the\n"
"`set-user` command, or used for a single command with the\n"
"`--as-user` flag. New users can be added with `linode-cli configure`.\n"
"The user that is currently active is indicated with a `*`"
)
else:
cli.config.print_users()
sys.exit(ExitCodes.SUCCESS)
if parsed.command == "remove-user":
if parsed.help or not parsed.action:
print(
"linode-cli remove-user [USER]\n\n"
"Removes a user the CLI was configured with. This does not change\n"
"your Linode account, only this CLI installation. Once removed,\n"
"the user may not be set as active or used for commands unless\n"
"configured again."
)
else:
cli.config.remove_user(parsed.action)
sys.exit(ExitCodes.SUCCESS)
# check for plugin invocation
if parsed.command not in cli.ops and parsed.command in plugins.available(
cli.config
):
context = plugins.PluginContext(cli.config.get_token(), cli)
# reconstruct arguments to send to the plugin
plugin_args = argv[1:] # don't include the program name
plugin_args.remove(parsed.command) # don't include the plugin name
plugins.invoke(parsed.command, plugin_args, context)
sys.exit(ExitCodes.SUCCESS)
# unknown commands
if (
parsed.command not in cli.ops
and parsed.command not in plugins.available(cli.config)
and parsed.command not in HELP_TOPICS
):
print(f"Unrecognized command {parsed.command}", file=sys.stderr)
sys.exit(ExitCodes.UNRECOGNIZED_COMMAND)
# handle a help for a command - either --help or no action triggers this
if (
parsed.command is not None
and parsed.action is None
and parsed.command in cli.ops
):
print_help_command_actions(cli.ops, parsed.command)
sys.exit(ExitCodes.SUCCESS)
if parsed.command is not None and parsed.action is not None:
if parsed.help:
print_help_action(cli, parsed.command, parsed.action)
sys.exit(ExitCodes.SUCCESS)
cli.handle_command(parsed.command, parsed.action, args)