Files
mytoolkit/mycli/commands/env.py
T

86 lines
2.2 KiB
Python

"""Environment paths and quick navigation."""
import functools
import os
import subprocess
from pathlib import Path
import click
from mycli.config import config
from mycli.utils import handle_errors
@functools.lru_cache(maxsize=1)
def _get_paths() -> dict[str, Path]:
"""Lazy load paths from config."""
return config.get_all_paths()
@click.group(name="env")
def env_cmd():
"""Environment paths and navigation."""
pass
@env_cmd.command("goto")
@click.argument("name")
@handle_errors
def goto(name):
"""Print path for cd (use: cd $(mycli env goto <name>))."""
paths = _get_paths()
if name not in paths:
click.echo(f"Unknown path: {name}", err=True)
click.echo(f"Available: {', '.join(paths.keys())}", err=True)
raise click.Exit(1)
click.echo(paths[name])
@env_cmd.command("list")
@handle_errors
def list_paths():
"""List all configured paths."""
paths = _get_paths()
max_len = max(len(k) for k in paths.keys())
for name, path in sorted(paths.items()):
exists = "✓" if path.exists() else "✗"
click.echo(f"{name:{max_len}} {exists} {path}")
@env_cmd.command("cd")
@click.argument("name")
@handle_errors
def cd_path(name):
"""Change directory (launches new shell)."""
paths = _get_paths()
if name not in paths:
click.echo(f"Unknown path: {name}", err=True)
raise click.Exit(1)
target = paths[name]
if not target.exists():
click.echo(f"Path does not exist: {target}", err=True)
raise click.Exit(1)
shell = os.environ.get("SHELL", "/bin/zsh")
click.echo(f"Starting shell in: {target}")
subprocess.run([shell], cwd=target)
@env_cmd.command("status")
@handle_errors
def status():
"""Show environment status."""
paths = _get_paths()
click.secho("=== Paths ===", fg="cyan")
for name, path in sorted(paths.items()):
exists = "✓" if path.exists() else "✗"
click.echo(f" {name:12} [{exists}] {path}")
click.secho("\n=== OpenFOAM ===", fg="cyan")
openfoam_path = "/Volumes/OpenFOAM/openfoam/build"
if Path("/Volumes/OpenFOAM").exists():
click.secho(f" Mounted: {openfoam_path}", fg="green")
else:
click.secho(" Not mounted", fg="red")