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.
pull/111/head
Roo Sczesnak 2020-05-27 23:03:56 -07:00
parent 641060d635
commit 3a5073831f
1 changed files with 21 additions and 12 deletions

View File

@ -97,12 +97,26 @@ def main():
args = parser.parse_args() args = parser.parse_args()
_init_logger(args.debug) _init_logger(args.debug)
if len(args.nginx_files) == 1 and args.nginx_files[0] != '-': # generate a list of user-expanded absolute paths from the nginx_files input arguments
path = os.path.expanduser(args.nginx_files[0]) nginx_files = []
if not os.path.exists(path):
sys.stderr.write('File {path!r} was not found.\nPlease specify correct path to configuration.\n'.format( for input_path in args.nginx_files:
path=path)) if input_path == '-':
sys.exit(1) 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: try:
severity = gixy.severity.ALL[args.level] severity = gixy.severity.ALL[args.level]
@ -152,12 +166,7 @@ def main():
formatter = formatters()[config.output_format]() formatter = formatters()[config.output_format]()
failed = False failed = False
for input_path in args.nginx_files: for path in 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
with Gixy(config=config) as yoda: with Gixy(config=config) as yoda:
try: try:
if path == '-': if path == '-':