-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (68 loc) · 3.15 KB
/
Copy pathmain.py
File metadata and controls
85 lines (68 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import argparse
import logging
import os
from dotenv import load_dotenv
from struct_module.utils import read_config_file, merge_configs
from struct_module.commands.generate import GenerateCommand
from struct_module.commands.info import InfoCommand
from struct_module.commands.validate import ValidateCommand
from struct_module.commands.list import ListCommand
from struct_module.commands.generate_schema import GenerateSchemaCommand
from struct_module.commands.mcp import MCPCommand
from struct_module.logging_config import configure_logging
# Optional dependency: shtab for static shell completion generation
try:
import shtab # type: ignore
except Exception: # pragma: no cover - optional at runtime
shtab = None
load_dotenv()
def get_parser():
parser = argparse.ArgumentParser(
description="Generate project structure from YAML configuration.",
prog="struct",
epilog="Thanks for using %(prog)s! :)",
)
# Create subparsers
subparsers = parser.add_subparsers()
InfoCommand(subparsers.add_parser('info', help='Show information about the package'))
ValidateCommand(subparsers.add_parser('validate', help='Validate the YAML configuration file'))
GenerateCommand(subparsers.add_parser('generate', help='Generate the project structure'))
ListCommand(subparsers.add_parser('list', help='List available structures'))
GenerateSchemaCommand(subparsers.add_parser('generate-schema', help='Generate JSON schema for available structures'))
MCPCommand(subparsers.add_parser('mcp', help='MCP (Model Context Protocol) support'))
# init to create a basic .struct.yaml
from struct_module.commands.init import InitCommand
InitCommand(subparsers.add_parser('init', help='Initialize a basic .struct.yaml in the target directory'))
# completion manager
from struct_module.commands.completion import CompletionCommand
CompletionCommand(subparsers.add_parser('completion', help='Manage shell completions'))
# Add shtab completion printing flags if available
if shtab is not None:
# Adds --print-completion and --shell flags
shtab.add_argument_to(parser)
return parser
def main():
parser = get_parser()
args = parser.parse_args()
# Check if a subcommand was provided
if not hasattr(args, 'func'):
parser.print_help()
parser.exit()
# Read config file if provided
if getattr(args, 'config_file', None):
file_config = read_config_file(args.config_file)
args = argparse.Namespace(**merge_configs(file_config, args))
# Resolve logging level precedence: STRUCT_LOG_LEVEL env > --debug (if present) > --log
env_level = os.getenv('STRUCT_LOG_LEVEL')
if env_level:
logging_level = getattr(logging, env_level.upper(), logging.INFO)
else:
# Some commands (like mcp) may add a --debug flag; respect it
if getattr(args, 'debug', False):
logging_level = logging.DEBUG
else:
logging_level = getattr(logging, getattr(args, 'log', 'INFO').upper(), logging.INFO)
configure_logging(level=logging_level, log_file=getattr(args, 'log_file', None))
args.func(args)
if __name__ == "__main__":
main()