-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathcli.py
More file actions
108 lines (98 loc) · 2.51 KB
/
cli.py
File metadata and controls
108 lines (98 loc) · 2.51 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
import logging
import click
import yaml
from importlib.metadata import version
from rich.logging import RichHandler
from scanapi.exit_code import ExitCode
from scanapi.scan import scan
from scanapi.settings import settings
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
@click.group()
@click.version_option(version=version("scanapi"))
def main():
"""Automated Testing and Documentation for your REST API."""
@main.command(context_settings=CONTEXT_SETTINGS)
@click.argument("spec_path", type=click.Path(exists=True), required=False)
@click.option(
"-o",
"--output-path",
"output_path",
type=click.Path(),
help="Report output path.",
)
@click.option(
"-nr",
"--no-report",
"no_report",
is_flag=True,
help="Run ScanAPI without generating report.",
)
@click.option(
"-b",
"--browser",
"open_browser",
is_flag=True,
help="Open the results file using a browser",
)
@click.option(
"-c",
"--config-path",
"config_path",
type=click.Path(exists=True),
help="Configuration file path.",
)
@click.option(
"-t",
"--template",
"template",
type=click.Path(exists=True),
help="Custom report template path. The template must be a .jinja file.",
)
@click.option(
"-ll",
"--log-level",
"log_level",
type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]),
default="INFO",
help="Set the debug logging level for the program.",
)
def run(
spec_path,
output_path,
no_report,
config_path,
template,
log_level,
open_browser,
):
"""
Automated Testing and Documentation for your REST API.
SPEC_PATH argument is the API specification file path.
"""
logging.basicConfig(
level=log_level,
format="%(message)s",
datefmt="[%X]",
handlers=[
RichHandler(
show_time=False, markup=True, show_path=(log_level == "DEBUG")
)
],
)
logger = logging.getLogger(__name__)
click_preferences = {
"spec_path": spec_path,
"output_path": output_path,
"no_report": no_report,
"config_path": config_path,
"template": template,
"open_browser": open_browser,
}
try:
settings.save_preferences(**click_preferences)
except yaml.YAMLError as e:
error_message = "Error loading configuration file."
error_message = f"{error_message}\nPyYAML: {e}"
logger.error(error_message)
raise SystemExit(ExitCode.USAGE_ERROR)
scan()