Files
mytoolkit/mycli/commands/self_mgmt.py
T

87 lines
2.0 KiB
Python

"""Self-management commands for mycli."""
import os
import subprocess
import sys
from pathlib import Path
import click
def _get_project_root() -> Path:
"""Get mycli project root."""
# This file is at mycli/commands/self.py
return Path(__file__).parent.parent.parent
@click.group(name="self")
def self_cmd():
"""Manage mycli itself."""
pass
@self_cmd.command("update")
def update():
"""Update mycli from the local repository."""
root = _get_project_root()
click.secho("Updating mycli...", fg="cyan")
# Run make install
result = subprocess.run(
["make", "install"],
cwd=str(root),
capture_output=True,
text=True,
)
if result.returncode == 0:
click.secho("mycli updated successfully.", fg="green")
else:
click.secho("Update failed:", fg="red", err=True)
click.echo(result.stderr, err=True)
raise click.Exit(1)
@self_cmd.command("uninstall")
def uninstall():
"""Uninstall mycli."""
root = _get_project_root()
click.secho("Uninstalling mycli...", fg="cyan")
result = subprocess.run(
["make", "uninstall"],
cwd=str(root),
capture_output=True,
text=True,
)
if result.returncode == 0:
click.secho("mycli uninstalled.", fg="green")
else:
click.secho("Uninstall failed:", fg="red", err=True)
click.echo(result.stderr, err=True)
raise click.Exit(1)
@self_cmd.command("info")
def info():
"""Show mycli installation info."""
root = _get_project_root()
click.secho("MyCLI Info:", fg="cyan")
click.echo(f" Project root: {root}")
click.echo(f" Commands dir: {Path(__file__).parent}")
# Check if in PATH
mycli_path = subprocess.run(
["which", "mycli"],
capture_output=True,
text=True,
)
if mycli_path.returncode == 0:
click.echo(f" Executable: {mycli_path.stdout.strip()}")
else:
click.secho(" Executable: not in PATH", fg="yellow")