doxy/doxy/cli.py

105 lines
2.7 KiB
Python
Raw Normal View History

import sys
2023-01-10 08:57:57 +01:00
from pathlib import Path
2023-01-10 15:45:35 +01:00
import click
2023-01-10 10:14:38 +01:00
from rich import print
from rich.rule import Rule
2023-01-10 10:14:38 +01:00
from rich.tree import Tree
2023-01-16 14:42:00 +01:00
from doxy import output, services
2023-01-10 15:45:35 +01:00
from doxy.config import Config
2023-01-10 08:57:57 +01:00
try:
CONFIG = Config()
CONFIG.load()
except FileNotFoundError as exc:
click.echo(exc, sys.stderr)
sys.exit(1)
2023-01-10 08:57:57 +01:00
2023-01-16 14:42:00 +01:00
def complete_service_name(ctx, param, incomplete):
return [
k
for k in services.find_services(Path(CONFIG.root_directory))
if k.startswith(incomplete)
]
2023-01-10 08:57:57 +01:00
@click.group()
2023-01-16 14:42:00 +01:00
@click.option(
"--format",
"-f",
type=click.Choice(["fancy", "simple"], case_sensitive=False),
default="fancy",
show_default=True,
help="output formatting",
)
2023-01-10 08:57:57 +01:00
@click.pass_context
2023-01-16 14:42:00 +01:00
def main(ctx, format):
2023-01-10 08:57:57 +01:00
ctx.ensure_object(dict)
ctx.obj["CONFIG"] = CONFIG
2023-01-16 14:42:00 +01:00
ctx.obj["FORMAT"] = format.lower()
2023-01-10 08:57:57 +01:00
2023-01-13 15:03:30 +01:00
@click.command(help="list available services")
2023-01-16 14:42:00 +01:00
@click.pass_context
def list(ctx):
output.print_services(ctx, services.find_services(Path(CONFIG.root_directory)))
2023-01-10 10:24:38 +01:00
2023-01-13 15:03:30 +01:00
@click.command(help="edit the compose file")
2023-01-10 15:19:16 +01:00
@click.argument("service", nargs=1, shell_complete=complete_service_name)
@click.pass_context
@services.only_if_service_exists
2023-01-10 15:19:16 +01:00
def edit(ctx, service):
print(Rule(f"Editing {service}"))
compose_file = services.get_compose_file(
Path(ctx.obj["CONFIG"].root_directory) / service
)
click.edit(filename=Path(compose_file))
2023-01-10 15:19:16 +01:00
@click.command(
context_settings=dict(
ignore_unknown_options=True,
2023-01-13 15:03:30 +01:00
),
help="run docker-compose commands",
)
2023-01-10 10:24:38 +01:00
@click.argument("service", nargs=1, shell_complete=complete_service_name)
2023-01-10 08:57:57 +01:00
@click.argument("command", nargs=-1)
@click.pass_context
@services.only_if_service_exists
2023-01-10 08:57:57 +01:00
def control(ctx, service, command):
print(Rule(f"Controlling {service}"))
compose_file = services.get_compose_file(
Path(ctx.obj["CONFIG"].root_directory) / service
)
services.docker_compose_command(command, compose_file)
2023-01-16 14:42:00 +01:00
@click.command(help="pull the latest service images and restart")
@click.argument("service", nargs=1, shell_complete=complete_service_name)
@click.option(
"--remove", "-r", is_flag=True, default=False, help="remove unused volumes"
)
@click.pass_context
@services.only_if_service_exists
def update(ctx, service, remove):
compose_file = services.get_compose_file(
Path(ctx.obj["CONFIG"].root_directory) / service
)
print(Rule(f"Updating {service}"))
2023-01-13 15:43:30 +01:00
command_chain = [
["down" if remove else "stop"],
["pull"],
["up", "-d"],
]
for command in command_chain:
services.docker_compose_command(command, compose_file)
2023-01-10 08:57:57 +01:00
main.add_command(list)
2023-01-10 15:19:16 +01:00
main.add_command(edit)
2023-01-10 08:57:57 +01:00
main.add_command(control)
main.add_command(update)