|
1 | 1 | import os |
2 | 2 | import logging |
3 | | -import yaml |
4 | 3 | from dotenv import load_dotenv |
5 | | -from .utils import read_config_file, merge_configs, validate_configuration, create_structure |
| 4 | +from struct_module.utils import read_config_file, merge_configs |
| 5 | +from struct_module.commands.generate import GenerateCommand |
| 6 | +from struct_module.commands.info import InfoCommand |
| 7 | +from struct_module.commands.validate import ValidateCommand |
| 8 | + |
| 9 | +import argparse |
6 | 10 |
|
7 | 11 | load_dotenv() |
8 | 12 |
|
|
14 | 18 |
|
15 | 19 |
|
16 | 20 | def main(): |
17 | | - import argparse |
18 | | - |
19 | 21 | parser = argparse.ArgumentParser( |
20 | 22 | description="Generate project structure from YAML configuration.", |
21 | 23 | prog="struct", |
22 | 24 | epilog="Thanks for using %(prog)s! :)", |
23 | | - |
24 | 25 | ) |
25 | | - parser.add_argument('yaml_file', type=str, help='Path to the YAML configuration file') |
26 | | - parser.add_argument('base_path', type=str, help='Base path where the structure will be created') |
27 | | - parser.add_argument('-c', '--config-file', type=str, help='Path to a configuration file') |
28 | | - parser.add_argument('-l', '--log', type=str, default='INFO', help='Set the logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)') |
29 | | - parser.add_argument('-d', '--dry-run', action='store_true', help='Perform a dry run without creating any files or directories') |
30 | | - parser.add_argument('-v', '--vars', type=str, help='Template variables in the format KEY1=value1,KEY2=value2') |
31 | | - parser.add_argument('-b', '--backup', type=str, help='Path to the backup folder') |
32 | | - parser.add_argument('-f', '--file-strategy', type=str, choices=['overwrite', 'skip', 'append', 'rename', 'backup'], default='overwrite', help='Strategy for handling existing files') |
33 | | - parser.add_argument('-i', '--log-file', type=str, help='Path to a log file') |
34 | | - parser.add_argument('-p', '--global-system-prompt', type=str, help='Global system prompt for OpenAI') |
35 | 26 |
|
| 27 | + # Create subparsers |
| 28 | + subparsers = parser.add_subparsers() |
| 29 | + |
| 30 | + |
| 31 | + InfoCommand(subparsers.add_parser('info', help='Show information about the package')) |
| 32 | + ValidateCommand(subparsers.add_parser('validate', help='Validate the YAML configuration file')) |
| 33 | + GenerateCommand(subparsers.add_parser('generate', help='Generate the project structure')) |
36 | 34 | args = parser.parse_args() |
37 | 35 |
|
| 36 | + # Check if a subcommand was provided |
| 37 | + if not hasattr(args, 'func'): |
| 38 | + parser.print_help() |
| 39 | + parser.exit() |
| 40 | + |
38 | 41 | # Read config file if provided |
39 | 42 | if args.config_file: |
40 | 43 | file_config = read_config_file(args.config_file) |
41 | 44 | args = argparse.Namespace(**merge_configs(file_config, args)) |
42 | 45 |
|
43 | 46 | logging_level = getattr(logging, args.log.upper(), logging.INFO) |
44 | | - template_vars = dict(item.split('=') for item in args.vars.split(',')) if args.vars else None |
45 | | - backup_path = args.backup |
46 | | - |
47 | | - if backup_path and not os.path.exists(backup_path): |
48 | | - os.makedirs(backup_path) |
49 | | - |
50 | | - if args.base_path and not os.path.exists(args.base_path): |
51 | | - logging.info(f"Creating base path: {args.base_path}") |
52 | | - os.makedirs(args.base_path) |
53 | 47 |
|
54 | 48 | logging.basicConfig( |
55 | 49 | level=logging_level, |
56 | 50 | filename=args.log_file, |
57 | 51 | format='[%(asctime)s][%(levelname)s][struct] >>> %(message)s', |
58 | 52 | ) |
59 | | - logging.info(f"Starting to create project structure from {args.yaml_file} in {args.base_path}") |
60 | | - logging.debug(f"YAML file path: {args.yaml_file}, Base path: {args.base_path}, Dry run: {args.dry_run}, Template vars: {template_vars}, Backup path: {backup_path}") |
61 | | - |
62 | | - with open(args.yaml_file, 'r') as f: |
63 | | - config = yaml.safe_load(f) |
64 | | - |
65 | | - validate_configuration(config.get('structure', [])) |
66 | | - create_structure(args.base_path, config.get('structure', []), args.dry_run, template_vars, backup_path, args.file_strategy, args.global_system_prompt) |
67 | | - |
68 | | - logging.info("Finished creating project structure") |
69 | 53 |
|
| 54 | + args.func(args) |
70 | 55 |
|
71 | 56 | if __name__ == "__main__": |
72 | 57 | main() |
0 commit comments