From 3a5073831f6db85715f63ed52bc272fa81dee1a2 Mon Sep 17 00:00:00 2001 From: Roo Sczesnak Date: Wed, 27 May 2020 23:03:56 -0700 Subject: [PATCH] Refactor input validation to accept stdin on all platforms. Closes #110. Also drops redundant check that files exist before processing, and checks that file paths are not specified at the same time as stdin. --- gixy/cli/main.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/gixy/cli/main.py b/gixy/cli/main.py index a1120d2..fd54dde 100644 --- a/gixy/cli/main.py +++ b/gixy/cli/main.py @@ -97,12 +97,26 @@ def main(): args = parser.parse_args() _init_logger(args.debug) - if len(args.nginx_files) == 1 and args.nginx_files[0] != '-': - path = os.path.expanduser(args.nginx_files[0]) - if not os.path.exists(path): - sys.stderr.write('File {path!r} was not found.\nPlease specify correct path to configuration.\n'.format( - path=path)) - sys.exit(1) + # generate a list of user-expanded absolute paths from the nginx_files input arguments + nginx_files = [] + + for input_path in args.nginx_files: + if input_path == '-': + if len(args.nginx_files) > 1: + sys.stderr.write('Expected either file paths or stdin, got both.\n') + sys.exit(1) + + nginx_files.append('-') + else: + path = os.path.expanduser(os.path.abspath(input_path)) + + if not os.path.exists(path): + sys.stderr.write( + 'File {path!r} was not found.\nPlease specify correct path to configuration.\n'.format(path=path) + ) + sys.exit(1) + + nginx_files.append(path) try: severity = gixy.severity.ALL[args.level] @@ -152,12 +166,7 @@ def main(): formatter = formatters()[config.output_format]() failed = False - for input_path in args.nginx_files: - path = os.path.abspath(os.path.expanduser(input_path)) - if not os.path.exists(path): - LOG.error('File %s was not found', path) - continue - + for path in nginx_files: with Gixy(config=config) as yoda: try: if path == '-':