Files
cli/secnex/app/config.py
2026-01-27 16:35:46 +01:00

114 lines
4.5 KiB
Python

import typer
from typing import Optional
from rich.console import Console
from rich.table import Table
from rich.prompt import Prompt
import os
import json
console = Console()
config_app = typer.Typer()
@config_app.command()
def create(
name: Optional[str] = typer.Option(None, "--name", "-n", help="The name of the config"),
client_id: Optional[str] = typer.Option(None, "--client-id", "-c", help="The ID of the client"),
client_secret: Optional[str] = typer.Option(None, "--client-secret", "-s", help="The secret of the client"),
tenant_id: Optional[str] = typer.Option(None, "--tenant-id", "-t", help="The ID of the tenant"),
ui: Optional[str] = typer.Option(None, "--ui", "-u", help="The UI to use"),
host: Optional[str] = typer.Option(None, "--host", "-h", help="The host of the SecNex API"),
):
"""Create a new config"""
if name is None:
name = "default"
if host is None:
host = Prompt.ask("Enter the host of the SecNex API", default="http://localhost:3003")
if ui is None:
ui = Prompt.ask("Enter the UI to use", default="http://localhost:3000")
if client_id is None:
client_id = Prompt.ask("Enter the ID of the client")
if client_secret is None:
client_secret = Prompt.ask("Enter the secret of the client")
if tenant_id is None:
tenant_id = Prompt.ask("Enter the ID of the tenant")
if not os.path.exists(os.path.join(os.path.expanduser("~/.secnex"))):
os.makedirs(os.path.join(os.path.expanduser("~/.secnex")))
if not os.path.exists(os.path.join(os.path.expanduser("~/.secnex"), f"{name}.json")):
with open(os.path.join(os.path.expanduser("~/.secnex"), f"{name}.json"), "w") as f:
json.dump({}, f)
else:
typer.echo(f"Config {name} already exists!", err=True, color=True)
return
with open(os.path.join(os.path.expanduser("~/.secnex"), f"{name}.json"), "w") as f:
json.dump({
"host": host,
"ui": ui,
"client": {
"id": client_id,
"secret": client_secret,
"tenant": tenant_id,
}
}, f)
typer.echo(f"Config {name} created successfully!", color=True)
@config_app.command(name="show", help="Show a config")
def show(name: Optional[str] = typer.Option(None, "--name", "-n", help="The name of the config")):
"""Show a config"""
if name is None:
name = "default"
if not os.path.exists(os.path.join(os.path.expanduser("~/.secnex"), f"{name}.json")):
typer.echo(f"Config {name} does not exist!", err=True, color=True)
return
with open(os.path.join(os.path.expanduser("~/.secnex"), f"{name}.json"), "r") as f:
config = json.load(f)
console.print(f"Config {name}: {config}", style="dim")
@config_app.command(name="ls", help="List all configs")
def ls():
"""List all configs"""
if not os.path.exists(os.path.join(os.path.expanduser("~/.secnex"))):
typer.echo("No configs found!", err=True, color=True)
return
table = Table(title="Configs")
table.add_column("Name", style="bold green")
table.add_column("Path", style="bold yellow")
for file in os.listdir(os.path.join(os.path.expanduser("~/.secnex"))):
if file.endswith(".json"):
table.add_row(file.replace(".json", ""), os.path.join(os.path.expanduser("~/.secnex"), file))
console.print(table)
@config_app.command(name="rm", help="Remove a config")
def rm(name: Optional[str] = typer.Option(None, "--name", "-n", help="The name of the config")):
"""Remove a config"""
if name is None:
name = "default"
if not os.path.exists(os.path.join(os.path.expanduser("~/.secnex"), f"{name}.json")):
typer.echo(f"Config {name} does not exist!", err=True, color=True)
return
os.remove(os.path.join(os.path.expanduser("~/.secnex"), f"{name}.json"))
typer.echo(f"Config {name} removed successfully!", color=True)
@config_app.command(name="path", help="Get the path of a config")
def path(name: Optional[str] = typer.Option(None, "--name", "-n", help="The name of the config")):
"""Get the path of a config"""
if name is None:
name = "default"
if not os.path.exists(os.path.join(os.path.expanduser("~/.secnex"), f"{name}.json")):
typer.echo(f"Config {name} does not exist!", err=True, color=True)
return
typer.echo(os.path.join(os.path.expanduser("~/.secnex"), f"{name}.json"), color=True)