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

64 lines
1.9 KiB
Python

"""Export commands for env-pro."""
import click
from pathlib import Path
@click.command()
@click.argument("profile", required=False)
@click.option("--output", "-o", help="Output file (default: stdout)")
@click.option("format", "--format", type=click.Choice(["env", "json", "yaml"]), default="env")
def export(profile: str, output: str, format: str):
"""Export environment variables to a file."""
from env_pro.core.profile import get_active_profile, get_profile_vars
from env_pro.core.encryption import decrypt_value, get_key_from_keyring
import json
profile = profile or get_active_profile() or "default"
vars = get_profile_vars(profile)
keyring_key = get_key_from_keyring()
decrypted_vars = {}
for key, value in vars.items():
if keyring_key:
try:
decrypted_vars[key] = decrypt_value(value, keyring_key)
except Exception:
decrypted_vars[key] = value
else:
decrypted_vars[key] = value
if format == "json":
content = json.dumps(decrypted_vars, indent=2)
elif format == "yaml":
import yaml
content = yaml.dump(decrypted_vars)
else:
lines = [f"{k}={v}" for k, v in decrypted_vars.items()]
content = "\n".join(lines)
if output:
Path(output).write_text(content)
click.echo(f"Exported to {output}")
else:
click.echo(content)
@click.command()
def example():
"""Generate .env.example file."""
from env_pro.core.profile import get_active_profile, get_profile_vars
profile = get_active_profile() or "default"
vars = get_profile_vars(profile)
lines = ["# Generated by env-pro", f"# Profile: {profile}", ""]
for key, value in vars.items():
if len(value) > 20:
value = "<secret>"
lines.append(f"{key}={value}")
click.echo("\n".join(lines))