Skip to content

Commit 8fe0e70

Browse files
Added set-alias and remove-alias commands
1 parent 2fd248f commit 8fe0e70

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

linodecli/__init__.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,53 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
110110
# if not spec was found and we weren't baking, we're doomed
111111
sys.exit(ExitCodes.ARGUMENT_ERROR)
112112

113+
if parsed.command == "set-custom-alias" or parsed.command == "remove-custom-alias":
114+
# Ensure both --command and --alias are provided
115+
# TODO: Can we use argparse for these
116+
if len(args) == 4:
117+
if args[0] != "--command" or args[2] != "--alias":
118+
print("Usage: linode-cli set-custom-alias --command [COMMAND] --alias [ALIAS]", file=sys.stderr)
119+
sys.exit(ExitCodes.ARGUMENT_ERROR)
120+
121+
# Get the indexes for --command and --alias
122+
try:
123+
command_index = args.index("--command") + 1
124+
alias_index = args.index("--alias") + 1
125+
except ValueError:
126+
print("Both --command and --alias arguments are required.", file=sys.stderr)
127+
sys.exit(ExitCodes.ARGUMENT_ERROR)
128+
129+
if command_index >= len(args) or alias_index >= len(args):
130+
print("Both --command and --alias arguments must have valid values.", file=sys.stderr)
131+
sys.exit(ExitCodes.ARGUMENT_ERROR)
132+
133+
# Retrieve the command and alias from arguments
134+
command = args[command_index]
135+
alias = args[alias_index]
136+
137+
# Check if the command is valid
138+
if command not in cli.ops:
139+
print(f"Error: '{command}' is not a valid command.", file=sys.stderr)
140+
sys.exit(ExitCodes.ARGUMENT_ERROR)
141+
142+
# Set the alias if it does not already exist
143+
if parsed.command == "set-custom-alias":
144+
if (alias, command) not in cli.config.get_custom_aliases().items():
145+
cli.config.set_custom_alias(alias, command)
146+
print(f"Custom alias '{alias}' set for command '{command}'")
147+
else:
148+
print(f"Custom alias '{alias}' already set for command '{command}'")
149+
150+
# Remove the alias if it already exists
151+
if parsed.command == "remove-custom-alias":
152+
if (alias, command) in cli.config.get_custom_aliases().items():
153+
cli.config.remove_custom_alias(alias, command)
154+
print(f"Custom alias '{alias}' removed for command '{command}'")
155+
else:
156+
print(f"Custom alias '{alias}' does not exist for command '{command}'")
157+
158+
sys.exit(ExitCodes.SUCCESS)
159+
113160
if parsed.command == "register-plugin":
114161
if parsed.action is None:
115162
print("register-plugin requires a module name!", file=sys.stderr)
@@ -219,6 +266,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
219266
parsed.command not in cli.ops
220267
and parsed.command not in plugins.available(cli.config)
221268
and parsed.command not in HELP_TOPICS
269+
and parsed.command not in cli.config.get_custom_aliases().keys()
222270
):
223271
print(f"Unrecognized command {parsed.command}", file=sys.stderr)
224272
sys.exit(ExitCodes.UNRECOGNIZED_COMMAND)

linodecli/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,13 @@ def find_operation(self, command, action):
238238
Finds the corresponding operation for the given command and action.
239239
"""
240240
if command not in self.ops:
241-
raise ValueError(f"Command not found: {command}")
241+
# Check that the passed command is not an alias before raising an error
242+
if command in self.config.get_custom_aliases().keys():
243+
print(command)
244+
command = self.config.get_custom_aliases()[command]
245+
print(command)
246+
else:
247+
raise ValueError(f"Command not found: {command}")
242248

243249
command_dict = self.ops[command]
244250

linodecli/configuration/config.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,49 @@ def _handle_no_default_user(self): # pylint: disable=too-many-branches
597597
self.write_config()
598598
return
599599
print(f"No user {username}")
600+
601+
def set_custom_alias(self, alias: str, command: str):
602+
"""
603+
Sets a custom alias for a Linode CLI command.
604+
605+
:param alias: The custom alias name.
606+
:param command: The command the custom alias maps to.
607+
"""
608+
if not self.config.has_section("custom_aliases"):
609+
self.config.add_section("custom_aliases")
610+
611+
self.config.set("custom_aliases", alias, command)
612+
self.write_config()
613+
614+
def remove_custom_alias(self, alias: str, command: str):
615+
"""
616+
Removes a custom alias from the Linode CLI configuration.
617+
618+
:param alias: The alias name to remove.
619+
:param command: The command the alias is mapped to.
620+
"""
621+
if not self.config.has_section("custom_aliases"):
622+
print(f"Error: No custom aliases have been set.", file=sys.stderr)
623+
return
624+
625+
if not self.config.has_option("custom_aliases", alias):
626+
print(f"Error: Custom alias '{alias}' does not exist.", file=sys.stderr)
627+
return
628+
629+
# Check if the alias maps to the given command
630+
existing_command = self.config.get("custom_aliases", alias)
631+
if existing_command != command:
632+
print(f"Error: Custom alias '{alias}' is mapped to '{existing_command}', not '{command}'.", file=sys.stderr)
633+
return
634+
635+
# Remove the alias and update the config
636+
self.config.remove_option("custom_aliases", alias)
637+
self.write_config()
638+
639+
def get_custom_aliases(self) -> Dict[str, str]:
640+
"""
641+
Retrieves all stored custom command aliases from the config.
642+
643+
:return: A dictionary mapping custom alias names to their respective commands.
644+
"""
645+
return dict(self.config.items("custom_aliases")) if self.config.has_section("custom_aliases") else {}

0 commit comments

Comments
 (0)