-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
143 lines (110 loc) · 4.49 KB
/
cli.py
File metadata and controls
143 lines (110 loc) · 4.49 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
"""
YAML to SQL CLI Tool
Convert YAML schema definitions to SQL CREATE TABLE statements.
"""
import click
from pathlib import Path
import sys
from .main import convert_yaml_to_sql
@click.group()
@click.version_option(version="1.0.0", prog_name="yaml2sql")
def cli():
"""Convert YAML schema definitions to SQL CREATE TABLE statements."""
pass
@cli.command()
@click.argument('input_dir', type=click.Path(exists=True, file_okay=False, dir_okay=True))
@click.argument('output_dir', type=click.Path(file_okay=False, dir_okay=True))
@click.option('--output-file', '-o', default='schema.sql', help='Output SQL filename (default: schema.sql)')
@click.option('--dry-run', is_flag=True, help='Show what would be generated without writing files')
@click.option('--verbose', '-v', is_flag=True, help='Enable verbose output')
def convert(input_dir, output_dir, output_file, dry_run, verbose):
"""Convert YAML files in INPUT_DIR to SQL in OUTPUT_DIR.
Examples:
yaml2sql convert ./schema/yaml ./output
yaml2sql convert ./schema/yaml ./output --output-file tables.sql
yaml2sql convert ./schema/yaml ./output --dry-run --verbose
"""
input_path = Path(input_dir)
output_path = Path(output_dir)
if verbose:
click.echo(f"📁 Input directory: {input_path.absolute()}")
click.echo(f"📁 Output directory: {output_path.absolute()}")
click.echo(f"📄 Output file: {output_file}")
# Check for YAML files
yaml_files = list(input_path.glob("*.yaml")) + list(input_path.glob("*.yml"))
if not yaml_files:
click.echo("❌ No YAML files found in input directory", err=True)
sys.exit(1)
if verbose:
click.echo(f"📋 Found {len(yaml_files)} YAML files:")
for f in yaml_files:
click.echo(f" • {f.name}")
if dry_run:
click.echo("🔍 DRY RUN: Would generate SQL without writing files")
# TODO: Add dry run functionality to show what would be generated
return
try:
# Ensure output directory exists
output_path.mkdir(parents=True, exist_ok=True)
# Convert YAML to SQL
convert_yaml_to_sql(str(input_path), str(output_path))
generated_file = output_path / "schema.sql"
if output_file != "schema.sql":
new_file = output_path / output_file
generated_file.rename(new_file)
generated_file = new_file
click.echo(f"✅ Successfully generated: {generated_file}")
if verbose:
with open(generated_file, 'r') as f:
lines = f.readlines()
click.echo(f"📊 Generated {len(lines)} lines of SQL")
except Exception as e:
click.echo(f"❌ Error: {str(e)}", err=True)
sys.exit(1)
@cli.command()
@click.argument('input_dir', type=click.Path(exists=True, file_okay=False, dir_okay=True))
def validate(input_dir):
"""Validate YAML files can be parsed correctly.
Examples:
yaml2sql validate ./schema/yaml
"""
input_path = Path(input_dir)
# Check for YAML files
yaml_files = list(input_path.glob("*.yaml")) + list(input_path.glob("*.yml"))
if not yaml_files:
click.echo("❌ No YAML files found in input directory", err=True)
sys.exit(1)
click.echo(f"🔍 Validating {len(yaml_files)} YAML files...")
errors = []
for yaml_file in yaml_files:
try:
import yaml
with open(yaml_file, 'r') as f:
yaml.safe_load(f)
click.echo(f"✅ {yaml_file.name}")
except Exception as e:
errors.append((yaml_file.name, str(e)))
click.echo(f"❌ {yaml_file.name}: {str(e)}")
if errors:
click.echo(f"\n❌ Found {len(errors)} validation errors")
sys.exit(1)
else:
click.echo(f"\n✅ All {len(yaml_files)} files are valid!")
@cli.command()
@click.argument('input_dir', type=click.Path(exists=True, file_okay=False, dir_okay=True))
def list_files(input_dir):
"""List YAML files that would be processed.
Examples:
yaml2sql list-files ./schema/yaml
"""
input_path = Path(input_dir)
yaml_files = sorted(list(input_path.glob("*.yaml")) + list(input_path.glob("*.yml")))
if not yaml_files:
click.echo("❌ No YAML files found in input directory")
sys.exit(1)
click.echo(f"📋 Found {len(yaml_files)} YAML files in {input_path}:")
for yaml_file in yaml_files:
click.echo(f" • {yaml_file.name}")
if __name__ == '__main__':
cli()