feat(alias): add interactive alias setup prompt and extensible alias management
This commit is contained in:
@@ -43,11 +43,19 @@
|
|||||||
"field_path": "Path"
|
"field_path": "Path"
|
||||||
},
|
},
|
||||||
"alias": {
|
"alias": {
|
||||||
"description": "Configure or regenerate custom command shims for an applet",
|
"description": "Configure, update, or list custom command aliases for Gravitiny or installed applets",
|
||||||
"usage": "Usage: gravitiny alias <applet_id> <new_alias>",
|
"usage": "Usage: gravitiny alias [applet_id | gravitiny] <alias_name> | gravitiny alias --list | gravitiny alias --remove <alias_name>",
|
||||||
"missing_args": "Error: Applet ID and new alias name are required.",
|
"missing_args": "Error: Alias name is required. Usage: gravitiny alias [applet_id] <alias_name>",
|
||||||
"applet_not_found": "Error: Applet '{applet_id}' is not found.",
|
"applet_not_found": "Error: Applet '{applet_id}' is not found.",
|
||||||
"success": "Generated shim '{alias}' for applet '{applet_id}' in {bin_dir}."
|
"success": "Generated alias '{alias}' for '{target}' in {bin_dir}.",
|
||||||
|
"success_gravitiny": "Generated master alias '{alias}' for Gravitiny in {bin_dir}.",
|
||||||
|
"removed": "Removed alias '{alias}' from {bin_dir}.",
|
||||||
|
"remove_not_found": "Alias '{alias}' does not exist in {bin_dir}.",
|
||||||
|
"list_header": "Configured Command Aliases & Shims ({count}):",
|
||||||
|
"no_shims_found": "No active command shims found in {bin_dir}.",
|
||||||
|
"prompt_ask_alias": "\nWould you like to configure a custom shorthand alias for Gravitiny (e.g. 'gt', 'g')? (y/N): ",
|
||||||
|
"prompt_enter_alias": "Enter shorthand alias name: ",
|
||||||
|
"alias_configured": "Configured alias '{alias}' successfully."
|
||||||
},
|
},
|
||||||
"setup": {
|
"setup": {
|
||||||
"description": "Initialize Gravitiny home directory, venv, and bin PATH configuration",
|
"description": "Initialize Gravitiny home directory, venv, and bin PATH configuration",
|
||||||
|
|||||||
+60
-10
@@ -237,24 +237,63 @@ def cmd_info(args: List[str]) -> int:
|
|||||||
|
|
||||||
|
|
||||||
def cmd_alias(args: List[str]) -> int:
|
def cmd_alias(args: List[str]) -> int:
|
||||||
if len(args) < 2:
|
shim_gen = ShimGenerator()
|
||||||
print(catalog.get("commands.alias.missing_args"))
|
|
||||||
print(catalog.get("commands.alias.usage"))
|
# Subcommand: list aliases
|
||||||
return 1
|
if not args or "-l" in args or "--list" in args or args[0] == "list":
|
||||||
|
shims = shim_gen.list_shims()
|
||||||
|
if not shims:
|
||||||
|
print(catalog.format("commands.alias.no_shims_found", bin_dir=shim_gen.bin_dir))
|
||||||
|
return 0
|
||||||
|
|
||||||
|
print(catalog.format("commands.alias.list_header", count=len(shims)))
|
||||||
|
print("-" * 55)
|
||||||
|
for s in shims:
|
||||||
|
clean_name = s.stem if os.name == "nt" else s.name
|
||||||
|
print(f" - {clean_name:<18} -> {s}")
|
||||||
|
print()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# Subcommand: remove alias
|
||||||
|
if "-r" in args or "--remove" in args or args[0] in ["rm", "remove", "delete"]:
|
||||||
|
target_alias = args[1] if len(args) > 1 else (args[0] if args[0] not in ["-r", "--remove", "rm", "remove", "delete"] else "")
|
||||||
|
if not target_alias:
|
||||||
|
print(catalog.get("commands.alias.missing_args"))
|
||||||
|
return 1
|
||||||
|
|
||||||
|
removed = shim_gen.remove_shim(target_alias)
|
||||||
|
if removed:
|
||||||
|
print(catalog.format("commands.alias.removed", alias=target_alias, bin_dir=shim_gen.bin_dir))
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
print(catalog.format("commands.alias.remove_not_found", alias=target_alias, bin_dir=shim_gen.bin_dir))
|
||||||
|
return 1
|
||||||
|
|
||||||
|
# Single argument: shorthand for Gravitiny Core itself (e.g. 'gravitiny alias gt')
|
||||||
|
if len(args) == 1:
|
||||||
|
alias_name = args[0].strip().lower()
|
||||||
|
shim_path = shim_gen.generate_master_shim(alias=alias_name)
|
||||||
|
print(catalog.format("commands.alias.success_gravitiny", alias=alias_name, bin_dir=shim_gen.bin_dir))
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# Two arguments: 'gravitiny alias <target> <alias>'
|
||||||
|
target, alias_name = args[0].strip().lower(), args[1].strip().lower()
|
||||||
|
|
||||||
|
if target in ["gravitiny", "core", "self", "main"]:
|
||||||
|
shim_gen.generate_master_shim(alias=alias_name)
|
||||||
|
print(catalog.format("commands.alias.success_gravitiny", alias=alias_name, bin_dir=shim_gen.bin_dir))
|
||||||
|
return 0
|
||||||
|
|
||||||
applet_id, alias_name = args[0], args[1]
|
|
||||||
discovery = AppletDiscovery()
|
discovery = AppletDiscovery()
|
||||||
applet = discovery.get_applet(applet_id)
|
applet = discovery.get_applet(target)
|
||||||
|
|
||||||
if not applet:
|
if not applet:
|
||||||
print(catalog.format("commands.alias.applet_not_found", applet_id=applet_id))
|
print(catalog.format("commands.alias.applet_not_found", applet_id=target))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
shim_gen = ShimGenerator()
|
|
||||||
created = shim_gen.generate_shim(applet, alias=alias_name)
|
created = shim_gen.generate_shim(applet, alias=alias_name)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print(catalog.format("commands.alias.success", alias=alias_name, applet_id=applet.id, bin_dir=shim_gen.bin_dir))
|
print(catalog.format("commands.alias.success", alias=alias_name, target=applet.name, bin_dir=shim_gen.bin_dir))
|
||||||
return 0
|
return 0
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@@ -375,6 +414,17 @@ def cmd_setup() -> int:
|
|||||||
manual_hint = f"PowerShell: [Environment]::SetEnvironmentVariable('Path', $env:Path + ';{bin_dir}', 'User')" if os.name == "nt" else f'export PATH="{bin_dir}:$PATH"'
|
manual_hint = f"PowerShell: [Environment]::SetEnvironmentVariable('Path', $env:Path + ';{bin_dir}', 'User')" if os.name == "nt" else f'export PATH="{bin_dir}:$PATH"'
|
||||||
print(catalog.format("commands.setup.path_failed", manual=manual_hint))
|
print(catalog.format("commands.setup.path_failed", manual=manual_hint))
|
||||||
|
|
||||||
|
# Optional interactive alias prompt
|
||||||
|
try:
|
||||||
|
alias_prompt = input(catalog.get("commands.alias.prompt_ask_alias")).strip().lower()
|
||||||
|
if alias_prompt in ["y", "yes"]:
|
||||||
|
alias_name = input(catalog.get("commands.alias.prompt_enter_alias")).strip().lower()
|
||||||
|
if alias_name:
|
||||||
|
shim_gen.generate_master_shim(alias=alias_name)
|
||||||
|
print(catalog.format("commands.alias.success_gravitiny", alias=alias_name, bin_dir=bin_dir))
|
||||||
|
except (KeyboardInterrupt, EOFError):
|
||||||
|
pass
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -74,6 +74,68 @@ class ShimGenerator:
|
|||||||
|
|
||||||
return created_paths
|
return created_paths
|
||||||
|
|
||||||
|
def generate_master_shim(self, alias: str = "gravitiny") -> Path:
|
||||||
|
"""Generates a master CLI command shim for Gravitiny Core itself."""
|
||||||
|
clean_name = alias.strip().lower()
|
||||||
|
is_windows = os.name == "nt"
|
||||||
|
python_exe = self._get_python_interpreter()
|
||||||
|
|
||||||
|
# Locate core entrypoint
|
||||||
|
user_home = Path.home()
|
||||||
|
core_main = user_home / ".gravitiny" / "core" / "main.py"
|
||||||
|
if not core_main.exists():
|
||||||
|
core_main = Path(__file__).resolve().parent.parent / "main.py"
|
||||||
|
|
||||||
|
if is_windows:
|
||||||
|
shim_file = self.bin_dir / f"{clean_name}.cmd"
|
||||||
|
if core_main.exists():
|
||||||
|
content = (
|
||||||
|
"@echo off\n"
|
||||||
|
"setlocal\n"
|
||||||
|
f'"{python_exe}" "{core_main}" %*\n'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
content = (
|
||||||
|
"@echo off\n"
|
||||||
|
"setlocal\n"
|
||||||
|
f'"{python_exe}" -m gravitiny %*\n'
|
||||||
|
)
|
||||||
|
with open(shim_file, "w", encoding="utf-8") as f:
|
||||||
|
f.write(content)
|
||||||
|
return shim_file
|
||||||
|
else:
|
||||||
|
shim_file = self.bin_dir / clean_name
|
||||||
|
if core_main.exists():
|
||||||
|
content = (
|
||||||
|
"#!/usr/bin/env sh\n"
|
||||||
|
f'exec "{python_exe}" "{core_main}" "$@"\n'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
content = (
|
||||||
|
"#!/usr/bin/env sh\n"
|
||||||
|
f'exec "{python_exe}" -m gravitiny "$@"\n'
|
||||||
|
)
|
||||||
|
with open(shim_file, "w", encoding="utf-8") as f:
|
||||||
|
f.write(content)
|
||||||
|
try:
|
||||||
|
os.chmod(shim_file, 0o755)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return shim_file
|
||||||
|
|
||||||
|
def list_shims(self) -> List[Path]:
|
||||||
|
"""Lists all active command shims in the bin directory."""
|
||||||
|
if not self.bin_dir.exists():
|
||||||
|
return []
|
||||||
|
is_windows = os.name == "nt"
|
||||||
|
shims = []
|
||||||
|
for item in self.bin_dir.iterdir():
|
||||||
|
if is_windows and item.name.endswith(".cmd"):
|
||||||
|
shims.append(item)
|
||||||
|
elif not is_windows and not item.is_dir():
|
||||||
|
shims.append(item)
|
||||||
|
return sorted(shims, key=lambda p: p.name)
|
||||||
|
|
||||||
def remove_shim(self, shim_name: str) -> bool:
|
def remove_shim(self, shim_name: str) -> bool:
|
||||||
"""Removes a shim by name."""
|
"""Removes a shim by name."""
|
||||||
is_windows = os.name == "nt"
|
is_windows = os.name == "nt"
|
||||||
|
|||||||
+16
-2
@@ -105,6 +105,20 @@ def install_gravitiny_core() -> int:
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
from gravitiny.src.shims import ShimGenerator
|
||||||
|
shim_gen = ShimGenerator(bin_dir=bin_dir)
|
||||||
|
|
||||||
|
# 4. Optional Shorthand Alias Prompt
|
||||||
|
try:
|
||||||
|
alias_prompt = input("\nWould you like to configure a custom shorthand alias for Gravitiny (e.g. 'gt', 'g')? (y/N): ").strip().lower()
|
||||||
|
if alias_prompt in ["y", "yes"]:
|
||||||
|
alias_name = input("Enter shorthand alias name: ").strip().lower()
|
||||||
|
if alias_name:
|
||||||
|
shim_gen.generate_master_shim(alias=alias_name)
|
||||||
|
print(f"Created shorthand alias '{alias_name}' for Gravitiny in {bin_dir}.")
|
||||||
|
except (KeyboardInterrupt, EOFError):
|
||||||
|
pass
|
||||||
|
|
||||||
manifest = load_manager_manifest()
|
manifest = load_manager_manifest()
|
||||||
print("=" * 65)
|
print("=" * 65)
|
||||||
print(f" {manifest.get('name', 'Gravitiny')} Core v{manifest.get('version', '0.1.0')} installed successfully!")
|
print(f" {manifest.get('name', 'Gravitiny')} Core v{manifest.get('version', '0.1.0')} installed successfully!")
|
||||||
@@ -118,8 +132,8 @@ def install_gravitiny_core() -> int:
|
|||||||
print()
|
print()
|
||||||
print(" Next steps:")
|
print(" Next steps:")
|
||||||
print(" 1. Restart your terminal window.")
|
print(" 1. Restart your terminal window.")
|
||||||
print(" 2. Run: gravitiny search dictionary")
|
print(" 2. Run: gravitiny search")
|
||||||
print(" 3. Run: gravitiny install sec")
|
print(" 3. Run: gravitiny install myip")
|
||||||
print("=" * 65)
|
print("=" * 65)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user