|
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import argparse |
| 7 | +import logging |
7 | 8 | import os |
8 | 9 | import sys |
9 | 10 | from importlib.metadata import version |
|
38 | 39 |
|
39 | 40 | TEST_MODE = os.getenv("LINODE_CLI_TEST_MODE") == "1" |
40 | 41 |
|
| 42 | +# Configure the `logging` package log level depending on the --debug flag. |
| 43 | +logging.basicConfig( |
| 44 | + level=logging.DEBUG if "--debug" in argv else logging.WARNING, |
| 45 | +) |
| 46 | + |
41 | 47 | # if any of these arguments are given, we don't need to prompt for configuration |
42 | 48 | skip_config = ( |
43 | | - any( |
44 | | - c in argv |
45 | | - for c in ["--skip-config", "--help", "--version", "completion"] |
46 | | - ) |
| 49 | + any(c in argv for c in ["--skip-config", "--version", "completion"]) |
47 | 50 | or TEST_MODE |
48 | 51 | ) |
49 | 52 |
|
@@ -104,6 +107,43 @@ def main(): # pylint: disable=too-many-branches,too-many-statements |
104 | 107 | # if not spec was found and we weren't baking, we're doomed |
105 | 108 | sys.exit(ExitCodes.ARGUMENT_ERROR) |
106 | 109 |
|
| 110 | + if parsed.command in ("set-custom-alias", "remove-custom-alias"): |
| 111 | + if not parsed.alias_command or not parsed.alias: |
| 112 | + print( |
| 113 | + "Both --alias-command and --alias must be provided.", |
| 114 | + file=sys.stderr, |
| 115 | + ) |
| 116 | + sys.exit(ExitCodes.ARGUMENT_ERROR) |
| 117 | + |
| 118 | + command = parsed.alias_command |
| 119 | + alias = parsed.alias |
| 120 | + |
| 121 | + if command not in cli.ops: |
| 122 | + print( |
| 123 | + f"Error: '{command}' is not a valid command.", file=sys.stderr |
| 124 | + ) |
| 125 | + sys.exit(ExitCodes.ARGUMENT_ERROR) |
| 126 | + |
| 127 | + if parsed.command == "set-custom-alias": |
| 128 | + if (alias, command) not in cli.config.get_custom_aliases().items(): |
| 129 | + cli.config.set_custom_alias(alias, command) |
| 130 | + print(f"Custom alias '{alias}' set for command '{command}'") |
| 131 | + else: |
| 132 | + print( |
| 133 | + f"Custom alias '{alias}' already set for command '{command}'" |
| 134 | + ) |
| 135 | + |
| 136 | + if parsed.command == "remove-custom-alias": |
| 137 | + if (alias, command) in cli.config.get_custom_aliases().items(): |
| 138 | + cli.config.remove_custom_alias(alias, command) |
| 139 | + print(f"Custom alias '{alias}' removed for command '{command}'") |
| 140 | + else: |
| 141 | + print( |
| 142 | + f"Custom alias '{alias}' does not exist for command '{command}'" |
| 143 | + ) |
| 144 | + |
| 145 | + sys.exit(ExitCodes.SUCCESS) |
| 146 | + |
107 | 147 | if parsed.command == "register-plugin": |
108 | 148 | if parsed.action is None: |
109 | 149 | print("register-plugin requires a module name!", file=sys.stderr) |
@@ -207,28 +247,36 @@ def main(): # pylint: disable=too-many-branches,too-many-statements |
207 | 247 | plugin_args.remove(parsed.command) # don't include the plugin name |
208 | 248 | plugins.invoke(parsed.command, plugin_args, context) |
209 | 249 | sys.exit(ExitCodes.SUCCESS) |
210 | | - |
211 | 250 | # unknown commands |
212 | 251 | if ( |
213 | 252 | parsed.command not in cli.ops |
214 | 253 | and parsed.command not in plugins.available(cli.config) |
215 | 254 | and parsed.command not in HELP_TOPICS |
| 255 | + and parsed.command not in cli.config.get_custom_aliases().keys() |
216 | 256 | ): |
217 | 257 | print(f"Unrecognized command {parsed.command}", file=sys.stderr) |
218 | 258 | sys.exit(ExitCodes.UNRECOGNIZED_COMMAND) |
219 | 259 |
|
220 | 260 | # handle a help for a command - either --help or no action triggers this |
221 | | - if ( |
222 | | - parsed.command is not None |
223 | | - and parsed.action is None |
224 | | - and parsed.command in cli.ops |
225 | | - ): |
226 | | - print_help_command_actions(cli.ops, parsed.command) |
227 | | - sys.exit(ExitCodes.SUCCESS) |
| 261 | + if parsed.command is not None and parsed.action is None: |
| 262 | + if parsed.command in cli.ops: |
| 263 | + print_help_command_actions(cli.ops, parsed.command) |
| 264 | + sys.exit(ExitCodes.SUCCESS) |
| 265 | + if parsed.command in cli.config.get_custom_aliases().keys(): |
| 266 | + print_help_command_actions( |
| 267 | + cli.ops, cli.config.get_custom_aliases()[parsed.command] |
| 268 | + ) |
228 | 269 |
|
229 | 270 | if parsed.command is not None and parsed.action is not None: |
230 | 271 | if parsed.help: |
231 | | - print_help_action(cli, parsed.command, parsed.action) |
| 272 | + if parsed.command in cli.config.get_custom_aliases().keys(): |
| 273 | + print_help_action( |
| 274 | + cli, |
| 275 | + cli.config.get_custom_aliases()[parsed.command], |
| 276 | + parsed.action, |
| 277 | + ) |
| 278 | + else: |
| 279 | + print_help_action(cli, parsed.command, parsed.action) |
232 | 280 | sys.exit(ExitCodes.SUCCESS) |
233 | 281 |
|
234 | 282 | cli.handle_command(parsed.command, parsed.action, args) |
0 commit comments