|
| 1 | +from structkit.commands import Command |
| 2 | +from structkit.sources import ( |
| 3 | + SourceError, |
| 4 | + add_source, |
| 5 | + get_sources_config_path, |
| 6 | + read_sources, |
| 7 | + remove_source, |
| 8 | + validate_source_path, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class SourcesCommand(Command): |
| 13 | + """Manage named custom structure sources.""" |
| 14 | + |
| 15 | + def __init__(self, parser): |
| 16 | + super().__init__(parser) |
| 17 | + parser.description = "Manage named custom structure sources" |
| 18 | + parser.add_argument('--config-path', type=str, help='Override sources config path (env: STRUCTKIT_SOURCES_CONFIG)') |
| 19 | + subparsers = parser.add_subparsers(dest='sources_command') |
| 20 | + |
| 21 | + subparsers.add_parser('list', help='List configured sources').set_defaults(sources_func=self.list_sources) |
| 22 | + |
| 23 | + add_parser = subparsers.add_parser('add', help='Add or update a local source') |
| 24 | + add_parser.add_argument('name') |
| 25 | + add_parser.add_argument('path_or_url') |
| 26 | + add_parser.set_defaults(sources_func=self.add_source) |
| 27 | + |
| 28 | + remove_parser = subparsers.add_parser('remove', help='Remove a configured source') |
| 29 | + remove_parser.add_argument('name') |
| 30 | + remove_parser.set_defaults(sources_func=self.remove_source) |
| 31 | + |
| 32 | + show_parser = subparsers.add_parser('show', help='Show a configured source') |
| 33 | + show_parser.add_argument('name') |
| 34 | + show_parser.set_defaults(sources_func=self.show_source) |
| 35 | + |
| 36 | + validate_parser = subparsers.add_parser('validate', help='Validate a configured source') |
| 37 | + validate_parser.add_argument('name') |
| 38 | + validate_parser.set_defaults(sources_func=self.validate_source) |
| 39 | + |
| 40 | + parser.set_defaults(func=self.execute) |
| 41 | + |
| 42 | + def execute(self, args): |
| 43 | + if not hasattr(args, 'sources_func'): |
| 44 | + self.parser.print_help() |
| 45 | + return |
| 46 | + try: |
| 47 | + args.sources_func(args) |
| 48 | + except SourceError as exc: |
| 49 | + self.logger.error(f"❗ {exc}") |
| 50 | + raise SystemExit(1) from exc |
| 51 | + |
| 52 | + def list_sources(self, args): |
| 53 | + sources = read_sources(args.config_path) |
| 54 | + print(f"Sources config: {args.config_path or get_sources_config_path()}") |
| 55 | + if not sources: |
| 56 | + print("No sources configured.") |
| 57 | + return |
| 58 | + for name, path in sorted(sources.items()): |
| 59 | + print(f"{name}\t{path}") |
| 60 | + |
| 61 | + def add_source(self, args): |
| 62 | + path = add_source(args.name, args.path_or_url, args.config_path) |
| 63 | + print(f"Added source '{args.name}' -> {read_sources(args.config_path)[args.name]}") |
| 64 | + print(f"Sources config: {path}") |
| 65 | + |
| 66 | + def remove_source(self, args): |
| 67 | + path = remove_source(args.name, args.config_path) |
| 68 | + print(f"Removed source '{args.name}'") |
| 69 | + print(f"Sources config: {path}") |
| 70 | + |
| 71 | + def show_source(self, args): |
| 72 | + sources = read_sources(args.config_path) |
| 73 | + if args.name not in sources: |
| 74 | + raise SourceError(f"source not found: {args.name}") |
| 75 | + print(f"{args.name}\t{sources[args.name]}") |
| 76 | + |
| 77 | + def validate_source(self, args): |
| 78 | + sources = read_sources(args.config_path) |
| 79 | + if args.name not in sources: |
| 80 | + raise SourceError(f"source not found: {args.name}") |
| 81 | + ok, message = validate_source_path(sources[args.name]) |
| 82 | + if not ok: |
| 83 | + raise SourceError(message) |
| 84 | + print(f"Source '{args.name}' is valid: {message}") |
0 commit comments