""" Backend Management CLI Commands Commands for configuring and managing issue tracking backends. """ import click from .utils import ( load_backend_configs, save_backend_configs, format_backend_list, test_backend_connection, validate_backend_type, echo_success, echo_error, echo_warning, confirm_action ) @click.group() def backend_group(): """Backend configuration and management.""" pass @backend_group.command('list') def list_backends(): """List configured backends.""" configs = load_backend_configs() click.echo(format_backend_list(configs)) @backend_group.command('add') @click.argument('name') @click.argument('backend_type', type=click.Choice(['local', 'gitea'])) @click.pass_context def add_backend(ctx, name, backend_type): """Add a new backend configuration.""" configs = load_backend_configs() if name in configs: if not confirm_action(f"Backend '{name}' already exists. Overwrite?"): click.echo("Aborted") return if backend_type == 'local': db_path = click.prompt('Database path', default=f'~/.config/issue-tracker/{name}.db') config = { 'type': 'local', 'db_path': str(db_path) } elif backend_type == 'gitea': base_url = click.prompt('Gitea base URL (e.g., https://git.example.com)') owner = click.prompt('Repository owner/organization') repo = click.prompt('Repository name') token = click.prompt('Access token', hide_input=True) config = { 'type': 'gitea', 'base_url': base_url.rstrip('/'), 'owner': owner, 'repo': repo, 'token': token } # Test connection click.echo("Testing connection...") if test_backend_connection(config): echo_success("Connection successful!") else: echo_warning("Connection test failed, but configuration will be saved anyway.") # Save configuration configs[name] = config save_backend_configs(configs) echo_success(f"Backend '{name}' added successfully") # Set as default if it's the first one if 'default' not in configs: configs['default'] = name save_backend_configs(configs) echo_info(f"Set '{name}' as default backend") @backend_group.command('remove') @click.argument('name') def remove_backend(name): """Remove a backend configuration.""" configs = load_backend_configs() if name not in configs: echo_error(f"Backend '{name}' not found") return if not confirm_action(f"Remove backend '{name}'?"): click.echo("Aborted") return del configs[name] # Update default if necessary if configs.get('default') == name: remaining_backends = [k for k in configs.keys() if k != 'default'] if remaining_backends: configs['default'] = remaining_backends[0] echo_info(f"Set '{configs['default']}' as new default backend") else: del configs['default'] save_backend_configs(configs) echo_success(f"Backend '{name}' removed") @backend_group.command('test') @click.argument('name') def test_backend(name): """Test backend connection.""" configs = load_backend_configs() if name not in configs: echo_error(f"Backend '{name}' not found") return config = configs[name] click.echo(f"Testing connection to '{name}'...") if test_backend_connection(config): echo_success("Connection successful!") else: echo_error("Connection failed!") @backend_group.command('set-default') @click.argument('name') def set_default_backend(name): """Set default backend.""" configs = load_backend_configs() if name not in configs: echo_error(f"Backend '{name}' not found") return configs['default'] = name save_backend_configs(configs) echo_success(f"Set '{name}' as default backend")