Files
env-pro/app/env_pro/commands/profile_cmds.py

111 lines
3.2 KiB
Python

"""Profile management commands for env-pro."""
import click
from typing import Optional
@click.command()
@click.argument("name")
def create(name: str):
"""Create a new profile."""
from env_pro.core.profile import create_profile, set_active_profile
create_profile(name)
set_active_profile(name)
click.echo(f"Created and activated profile '{name}'")
@click.command()
def list():
"""List all profiles."""
from env_pro.core.profile import list_profiles, get_active_profile
profiles = list_profiles()
active = get_active_profile()
if not profiles:
click.echo("No profiles found. Run 'env-pro init' first.")
return
click.echo("Profiles:")
for profile in profiles:
marker = " *" if profile == active else ""
click.echo(f" {profile}{marker}")
@click.command()
@click.argument("name")
def switch(name: str):
"""Switch to a profile (persistent)."""
from env_pro.core.profile import set_active_profile, list_profiles
profiles = list_profiles()
if name not in profiles:
raise click.ClickException(f"Profile '{name}' not found. Available: {', '.join(profiles)}")
set_active_profile(name)
click.echo(f"Switched to profile '{name}'")
@click.command()
@click.argument("name")
def use(name: str):
"""Use a profile for current session."""
from env_pro.core.profile import list_profiles
profiles = list_profiles()
if name not in profiles:
raise click.ClickException(f"Profile '{name}' not found. Available: {', '.join(profiles)}")
click.echo(f"Profile '{name}' will be used. Set ENV_PRO_PROFILE env var to persist.")
@click.command()
@click.argument("name")
def delete(name: str):
"""Delete a profile."""
from env_pro.core.profile import delete_profile, list_profiles
profiles = list_profiles()
if name not in profiles:
raise click.ClickException(f"Profile '{name}' not found")
if name == "default":
raise click.ClickException("Cannot delete 'default' profile")
delete_profile(name)
click.echo(f"Deleted profile '{name}'")
@click.command()
@click.argument("other", required=False)
def diff(other: Optional[str]):
"""Compare current profile with another."""
from env_pro.core.profile import (
get_active_profile, get_profile_vars, list_profiles
)
current = get_active_profile() or "default"
other = other or click.prompt("Compare with profile")
profiles = list_profiles()
if other not in profiles:
raise click.ClickException(f"Profile '{other}' not found")
current_vars = get_profile_vars(current)
other_vars = get_profile_vars(other)
all_keys = set(current_vars.keys()) | set(other_vars.keys())
click.echo(f"Diff: {current} vs {other}")
click.echo("-" * 40)
for key in sorted(all_keys):
current_val = current_vars.get(key, "<not set>")
other_val = other_vars.get(key, "<not set>")
if current_val != other_val:
click.echo(f" {key}:")
click.echo(f" {current}: {current_val[:50]}{'...' if len(current_val) > 50 else ''}")
click.echo(f" {other}: {other_val[:50]}{'...' if len(other_val) > 50 else ''}")