-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
141 lines (110 loc) · 4.16 KB
/
cli.py
File metadata and controls
141 lines (110 loc) · 4.16 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
"""
CLI tool for converting Markdown to Postman Collections.
"""
import argparse
import os
import sys
from pathlib import Path
from md_to_postman.markdown_parser import MarkdownParser
from md_to_postman.postman_builder import PostmanCollectionBuilder
def main():
parser = argparse.ArgumentParser(
description="Convert structured Markdown files to Postman Collections v2.1"
)
parser.add_argument("input_file", help="Path to the markdown file to convert")
parser.add_argument(
"-o",
"--output",
help="Output file path (default: <input_file>.postman_collection.json)",
)
parser.add_argument(
"-n", "--name", help="Collection name (default: input filename)"
)
parser.add_argument(
"-d",
"--description",
default="Collection generated from Markdown",
help="Collection description",
)
parser.add_argument(
"--validate",
action="store_true",
help="Only validate the markdown structure without conversion",
)
parser.add_argument("--verbose", action="store_true", help="Enable verbose output")
args = parser.parse_args()
# Check if input file exists
if not os.path.exists(args.input_file):
print(f"Error: File '{args.input_file}' not found", file=sys.stderr)
return 1
try:
# Read input file
with open(args.input_file, encoding="utf-8") as file:
markdown_content = file.read()
# Parse markdown
parser_obj = MarkdownParser()
requests = parser_obj.parse(markdown_content)
if args.verbose:
print(f"Parsed {len(requests)} requests from {args.input_file}")
for req in requests:
print(f" - {req.name} (folder: {req.folder or 'root'})")
if not requests:
print(
"Warning: No valid requests found in the markdown file", file=sys.stderr
)
if not args.validate:
return 1
# Validation mode
if args.validate:
print("✅ Markdown structure validation:")
print(f" Requests found: {len(requests)}")
folders = set(req.folder for req in requests if req.folder)
print(f" Folders: {len(folders)}")
if folders:
for folder in sorted(folders):
print(f" - {folder}")
# Check for issues
issues = []
for req in requests:
if not req.curl_command.strip():
issues.append(f"Request '{req.name}': Empty cURL command")
if not req.metadata.description:
issues.append(f"Request '{req.name}': Missing description")
if issues:
print(" Issues found:")
for issue in issues:
print(f" ⚠️ {issue}")
else:
print(" ✅ No issues found")
return 0
# Generate collection name
collection_name = args.name or Path(args.input_file).stem
# Build collection
builder = PostmanCollectionBuilder()
collection = builder.build_collection(
requests, collection_name, args.description
)
# Generate output filename
output_file = args.output
if not output_file:
input_path = Path(args.input_file)
output_file = str(
input_path.parent / f"{input_path.stem}.postman_collection.json"
)
# Save collection
builder.save_collection(collection, output_file)
print("✅ Postman collection generated successfully!")
print(f" Input: {args.input_file}")
print(f" Output: {output_file}")
print(f" Requests: {len(requests)}")
print(f" Folders: {len(set(req.folder for req in requests if req.folder))}")
print(f" Variables: {len(collection.get('variable', []))}")
return 0
except Exception as e:
print(f"Error: {str(e)}", file=sys.stderr)
if args.verbose:
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(main())