Skip to content

Commit

Permalink
Add a simple CLI tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Timkovich committed Aug 8, 2018
1 parent 7e27cad commit 1e552ea
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
],
entry_points={
"console_scripts": [
"swagger-spec-validator = swagger_spec_validator.cli:main",
],
},
)
58 changes: 58 additions & 0 deletions swagger_spec_validator/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
"""
Swagger spec validator.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals


import argparse
import sys

from swagger_spec_validator.common import SwaggerValidationError
from swagger_spec_validator.util import validate_spec_url


def _extract_error_message(exc):
"""
The validators return nested errors coming from common.wrap_exception, but
other errors like invalid URLs aren't nested. Try to
"""
try:
return exc.args[1].args[0]
except IndexError:
pass

return exc.args[0]


def main():
parser = argparse.ArgumentParser(description=__doc__)

parser.add_argument("url", type=str, help="URL of Swagger spec to validate")
parser.add_argument(
"-q",
"--quiet",
action="store_true",
help="No output if validation is successful",
)

args = parser.parse_args()

try:
validate_spec_url(args.url)
except SwaggerValidationError as exc:
error_message = _extract_error_message(exc)
print(error_message, file=sys.stderr)
return 1

if not args.quiet:
print("Validation successful!", file=sys.stderr)

return 0


if __name__ == "__main__":
sys.exit(main())

0 comments on commit 1e552ea

Please sign in to comment.