-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__main__.py
41 lines (33 loc) · 1 KB
/
__main__.py
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
import json
try:
import click
except ImportError:
raise ImportError("Click is required to use this command, try `pip install click`.")
from .main import api
@click.group()
def cli():
pass
@cli.command()
@click.option(
"-o",
"--output",
type=click.Path(dir_okay=False, writable=True, allow_dash=True),
default="-",
help="Output file.",
)
def show(output):
"""Print the API as OpenAPI JSON document to stdout."""
if output == "-":
click.echo(json.dumps(api.openapi(), indent=2))
else:
# We only write the file if it either does not exist or in case it
# actually differs to avoid modifying the file metadata on every commit.
try:
current_api = click.open_file(output, "r").read()
except FileNotFoundError:
current_api = None
new_api = json.dumps(api.openapi(), indent=2) + "\n"
if current_api != new_api:
click.open_file(output, "w").write(new_api)
if __name__ == "__main__":
cli()