Skip to content

Commit a85f3b2

Browse files
committed
Updated log statements for CLI; optimised error handling logic for incorrect args
1 parent 5d71c4b commit a85f3b2

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/murfey/cli/generate_openapi_schema.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ def run():
6161

6262
# Load the relevant FastAPI app
6363
target = str(args.target).lower()
64-
if args.debug:
65-
print(f"Constructing OpenAPI schema for {target}")
6664

6765
# Silence output during import; only return genuine errors
6866
buffer = io.StringIO()
@@ -76,8 +74,9 @@ def run():
7674
"Unexpected value for target server. It must be one of "
7775
"'instrument-server' or 'server'"
7876
)
77+
if args.debug:
78+
print(f"Imported FastAPI app for {target}")
7979

80-
output = str(args.output).lower()
8180
if not app.openapi_schema:
8281
schema = get_openapi(
8382
title=app.title,
@@ -86,19 +85,28 @@ def run():
8685
description=app.description,
8786
routes=app.routes,
8887
)
88+
if args.debug:
89+
print(f"Constructed OpenAPI schema for {target}")
8990
else:
9091
schema = app.openapi_schema
92+
if args.debug:
93+
print(f"Loaded OpenAPI schema for {target}")
9194

95+
output = str(args.output).lower()
96+
if output not in ("json", "yaml"):
97+
raise ValueError(
98+
"Invalid file format selected. Output must be either 'json' or 'yaml'"
99+
)
92100
murfey_dir = Path(murfey.__path__[0])
93101
save_path = (
94-
murfey_dir / "util" / f"openapi.{output}"
102+
murfey_dir / "util" / f"openapi-{target}.{output}"
95103
if not args.to_file
96104
else Path(args.to_file)
97105
)
98106
with open(save_path, "w") as f:
99107
if output == "json":
100108
json.dump(schema, f, indent=2)
101-
elif output == "yaml":
109+
else:
102110
yaml.dump(
103111
schema,
104112
f,
@@ -107,11 +115,8 @@ def run():
107115
sort_keys=False,
108116
indent=2,
109117
)
110-
else:
111-
raise ValueError(
112-
"Invalid file format select. Output must be either 'json' or 'yaml'"
113-
)
114-
print(f"OpenAPI schema save to {save_path}")
118+
print(f"OpenAPI schema saved to {save_path}")
119+
exit()
115120

116121

117122
if __name__ == "__main__":

0 commit comments

Comments
 (0)