|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: GPL-2.0 |
| 3 | + |
| 4 | +"""CLI tool for administrative operations on AIR reviews""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +import configparser |
| 8 | +import json |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | +import requests |
| 12 | + |
| 13 | + |
| 14 | +# ANSI color codes |
| 15 | +class Colors: |
| 16 | + RESET = '\033[0m' |
| 17 | + RED = '\033[91m' |
| 18 | + GREEN = '\033[32m' |
| 19 | + YELLOW = '\033[93m' |
| 20 | + BLUE = '\033[94m' |
| 21 | + CYAN = '\033[96m' |
| 22 | + BOLD = '\033[1m' |
| 23 | + |
| 24 | + |
| 25 | +def colorize(text: str, color: str) -> str: |
| 26 | + """Add color to text if output is a TTY |
| 27 | +
|
| 28 | + Args: |
| 29 | + text: Text to colorize |
| 30 | + color: Color code |
| 31 | +
|
| 32 | + Returns: |
| 33 | + Colored text if stdout is a TTY, otherwise plain text |
| 34 | + """ |
| 35 | + if sys.stdout.isatty(): |
| 36 | + return f"{color}{text}{Colors.RESET}" |
| 37 | + return text |
| 38 | + |
| 39 | + |
| 40 | +def load_config() -> configparser.ConfigParser: |
| 41 | + """Load configuration from ~/.air.conf |
| 42 | +
|
| 43 | + Returns: |
| 44 | + ConfigParser instance (may be empty if file doesn't exist) |
| 45 | + """ |
| 46 | + config = configparser.ConfigParser() |
| 47 | + config_path = Path.home() / '.air.conf' |
| 48 | + |
| 49 | + if config_path.exists(): |
| 50 | + try: |
| 51 | + config.read(config_path) |
| 52 | + except Exception as e: |
| 53 | + print(f"Warning: Failed to read config file {config_path}: {e}", file=sys.stderr) |
| 54 | + |
| 55 | + return config |
| 56 | + |
| 57 | + |
| 58 | +def delete_review(url: str, token: str, review_id: str) -> bool: |
| 59 | + """Delete a review (superuser only) |
| 60 | +
|
| 61 | + Args: |
| 62 | + url: AIR service URL |
| 63 | + token: API token (must be superuser) |
| 64 | + review_id: Review ID to delete |
| 65 | +
|
| 66 | + Returns: |
| 67 | + True if successful |
| 68 | + """ |
| 69 | + api_url = f"{url}/api/review" |
| 70 | + params = { |
| 71 | + 'id': review_id, |
| 72 | + 'token': token, |
| 73 | + } |
| 74 | + |
| 75 | + try: |
| 76 | + response = requests.delete(api_url, params=params, timeout=30) |
| 77 | + response.raise_for_status() |
| 78 | + return response.json().get('success', False) |
| 79 | + except requests.exceptions.RequestException as e: |
| 80 | + print(f"Error deleting review: {e}", file=sys.stderr) |
| 81 | + sys.exit(1) |
| 82 | + except json.JSONDecodeError as e: |
| 83 | + print(f"Error parsing response: {e}", file=sys.stderr) |
| 84 | + sys.exit(1) |
| 85 | + |
| 86 | + |
| 87 | +def create_token(url: str, token: str, name: str) -> str: |
| 88 | + """Create a new token (superuser only) |
| 89 | +
|
| 90 | + Args: |
| 91 | + url: AIR service URL |
| 92 | + token: API token (must be superuser) |
| 93 | + name: Human-readable name for the new token |
| 94 | +
|
| 95 | + Returns: |
| 96 | + The newly created token string |
| 97 | + """ |
| 98 | + api_url = f"{url}/api/token" |
| 99 | + payload = { |
| 100 | + 'token': token, |
| 101 | + 'name': name, |
| 102 | + } |
| 103 | + |
| 104 | + try: |
| 105 | + response = requests.post(api_url, json=payload, timeout=30) |
| 106 | + response.raise_for_status() |
| 107 | + data = response.json() |
| 108 | + return data.get('token') |
| 109 | + except requests.exceptions.RequestException as e: |
| 110 | + print(f"Error creating token: {e}", file=sys.stderr) |
| 111 | + sys.exit(1) |
| 112 | + except json.JSONDecodeError as e: |
| 113 | + print(f"Error parsing response: {e}", file=sys.stderr) |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + # Load config file first to get defaults |
| 119 | + config = load_config() |
| 120 | + |
| 121 | + parser = argparse.ArgumentParser( |
| 122 | + description='Administrative operations for AIR reviews', |
| 123 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 124 | + epilog=""" |
| 125 | +Examples: |
| 126 | + # Delete a review |
| 127 | + %(prog)s --url https://example.com/air --token mytoken --delete abc-123-def |
| 128 | +
|
| 129 | + # Create a new token |
| 130 | + %(prog)s --url https://example.com/air --token mytoken --create-token "User Name" |
| 131 | +
|
| 132 | +Configuration file: |
| 133 | + You can create ~/.air.conf to avoid repeating common parameters: |
| 134 | +
|
| 135 | + [air] |
| 136 | + url = https://example.com/air |
| 137 | + token = mytoken |
| 138 | +
|
| 139 | + Command-line arguments always override config file values. |
| 140 | + """ |
| 141 | + ) |
| 142 | + |
| 143 | + parser.add_argument('--url', |
| 144 | + help='AIR service URL (e.g., https://example.com/air)') |
| 145 | + parser.add_argument('--token', |
| 146 | + help='API authentication token (required for admin operations)') |
| 147 | + parser.add_argument('--delete', metavar='REVIEW_ID', |
| 148 | + help='Delete the specified review (requires superuser token)') |
| 149 | + parser.add_argument('--create-token', metavar='NAME', |
| 150 | + help='Create a new token with the given name (requires superuser token)') |
| 151 | + |
| 152 | + args = parser.parse_args() |
| 153 | + |
| 154 | + # Fill in missing arguments from config file |
| 155 | + if config.has_section('air'): |
| 156 | + if args.url is None and config.has_option('air', 'url'): |
| 157 | + args.url = config.get('air', 'url') |
| 158 | + if args.token is None and config.has_option('air', 'token'): |
| 159 | + args.token = config.get('air', 'token') |
| 160 | + |
| 161 | + # Convert empty strings to None (allows unsetting config values) |
| 162 | + if args.token == '': |
| 163 | + args.token = None |
| 164 | + |
| 165 | + # Validate that we have URL |
| 166 | + if not args.url: |
| 167 | + parser.error('--url is required (either via command-line or ~/.air.conf)') |
| 168 | + |
| 169 | + args.url = args.url.rstrip('/') |
| 170 | + |
| 171 | + # Check that at least one operation is specified |
| 172 | + if not args.delete and not getattr(args, 'create_token', None): |
| 173 | + parser.error('No operation specified. Use --delete REVIEW_ID or --create-token NAME') |
| 174 | + |
| 175 | + # Handle --delete operation |
| 176 | + if args.delete: |
| 177 | + if not args.token: |
| 178 | + parser.error('--delete requires --token (must be superuser)') |
| 179 | + |
| 180 | + review_id = args.delete |
| 181 | + print(f"Deleting review {review_id}...") |
| 182 | + success = delete_review(args.url, args.token, review_id) |
| 183 | + if success: |
| 184 | + print(colorize(f"Review {review_id} deleted successfully", Colors.GREEN)) |
| 185 | + else: |
| 186 | + print(colorize("Failed to delete review", Colors.RED), file=sys.stderr) |
| 187 | + sys.exit(1) |
| 188 | + return |
| 189 | + |
| 190 | + # Handle --create-token operation |
| 191 | + if getattr(args, 'create_token', None): |
| 192 | + if not args.token: |
| 193 | + parser.error('--create-token requires --token (must be superuser)') |
| 194 | + |
| 195 | + name = args.create_token |
| 196 | + print(f"Creating token for '{name}'...") |
| 197 | + new_token = create_token(args.url, args.token, name) |
| 198 | + if new_token: |
| 199 | + print(colorize("Token created successfully", Colors.GREEN)) |
| 200 | + print(f"Name: {name}") |
| 201 | + print(f"Token: {colorize(new_token, Colors.CYAN)}") |
| 202 | + else: |
| 203 | + print(colorize("Failed to create token", Colors.RED), file=sys.stderr) |
| 204 | + sys.exit(1) |
| 205 | + return |
| 206 | + |
| 207 | + |
| 208 | +if __name__ == '__main__': |
| 209 | + main() |
0 commit comments