From 0f9b192c13746ad0a9cdb4613cb211e441fc0654 Mon Sep 17 00:00:00 2001 From: Yoav Tzelnick Date: Fri, 20 Oct 2017 20:36:14 +0300 Subject: [PATCH] Syntax error in configuration now fails the manager. making the exit code 1 on syntax error --- gixy/cli/main.py | 18 +++++++++++------- gixy/core/exceptions.py | 2 ++ gixy/parser/nginx_parser.py | 5 ++--- 3 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 gixy/core/exceptions.py diff --git a/gixy/cli/main.py b/gixy/cli/main.py index 4155039..881f53c 100644 --- a/gixy/cli/main.py +++ b/gixy/cli/main.py @@ -9,6 +9,8 @@ from gixy.core.plugins_manager import PluginsManager from gixy.core.config import Config from gixy.cli.argparser import create_parser +from gixy.core.exceptions import InvalidConfiguration + LOG = logging.getLogger() @@ -158,13 +160,15 @@ def main(): continue with Gixy(config=config) as yoda: - if path == '-': - with os.fdopen(sys.stdin.fileno(), 'rb') as fdata: - yoda.audit('', fdata, is_stdin=True) - else: - with open(path, mode='rb') as fdata: - yoda.audit(path, fdata, is_stdin=False) - + try: + if path == '-': + with os.fdopen(sys.stdin.fileno(), 'rb') as fdata: + yoda.audit('', fdata, is_stdin=True) + else: + with open(path, mode='rb') as fdata: + yoda.audit(path, fdata, is_stdin=False) + except InvalidConfiguration: + failed = True formatter.feed(path, yoda) failed = failed or sum(yoda.stats.values()) > 0 diff --git a/gixy/core/exceptions.py b/gixy/core/exceptions.py new file mode 100644 index 0000000..c96fe46 --- /dev/null +++ b/gixy/core/exceptions.py @@ -0,0 +1,2 @@ +class InvalidConfiguration(Exception): + pass diff --git a/gixy/parser/nginx_parser.py b/gixy/parser/nginx_parser.py index 83ad06b..38c3f8b 100644 --- a/gixy/parser/nginx_parser.py +++ b/gixy/parser/nginx_parser.py @@ -4,7 +4,7 @@ import fnmatch from pyparsing import ParseException - +from gixy.core.exceptions import InvalidConfiguration from gixy.parser import raw_parser from gixy.directives import block, directive @@ -29,7 +29,6 @@ def parse_file(self, path, root=None): def parse(self, content, root=None, path_info=None): if not root: root = block.Root() - try: parsed = self.parser.parse(content) except ParseException as e: @@ -38,7 +37,7 @@ def parse(self, content, root=None, path_info=None): LOG.error('Failed to parse config "{file}": {error}'.format(file=path_info, error=error_msg)) else: LOG.error('Failed to parse config: {error}'.format(error=error_msg)) - return root + raise InvalidConfiguration(error_msg) if len(parsed) and parsed[0].getName() == 'file_delimiter': # Were parse nginx dump