Skip to content

Commit

Permalink
Check non default schema and rules path for existance
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Jan 20, 2024
1 parent 1b3617d commit b0e0a68
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.2.5 (unreleased)

- Check non-default schema and rules paths for existence

# 0.2.4

- Dependency updates
Expand Down
7 changes: 5 additions & 2 deletions iac_validate/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import click

DEFAULT_SCHEMA = ".schema.yaml"
DEFAULT_RULES = ".rules/"

rules = click.option(
"-r",
"--rules",
type=click.Path(exists=False, dir_okay=True, file_okay=False),
envvar="IAC_VALIDATE_RULES",
default=".rules/",
default=DEFAULT_RULES,
help="Path to semantic rules. (optional, default: '.rules/', env: IAC_VALIDATE_RULES)",
required=False,
)
Expand All @@ -19,7 +22,7 @@
"--schema",
type=click.Path(exists=False, dir_okay=False, file_okay=True),
envvar="IAC_VALIDATE_SCHEMA",
default=".schema.yaml",
default=DEFAULT_SCHEMA,
help="Path to schema file. (optional, default: '.schema.yaml', env: IAC_VALIDATE_SCHEMA)",
required=False,
)
Expand Down
11 changes: 11 additions & 0 deletions iac_validate/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import yamale
from yamale.yamale_error import YamaleError

from .cli.options import DEFAULT_RULES, DEFAULT_SCHEMA
from .yaml import load_yaml_files

logger = logging.getLogger(__name__)
Expand All @@ -25,6 +26,11 @@ def __init__(self, schema_path: str, rules_path: str):
if os.path.exists(schema_path):
logger.info("Loading schema")
self.schema = yamale.make_schema(schema_path, parser="ruamel")
elif schema_path == DEFAULT_SCHEMA:
logger.info("No schema file found")
else:
logger.error("Schema file not found: {}".format(schema_path))
sys.exit(1)
self.errors: List[str] = []
self.rules = {}
if os.path.exists(rules_path):
Expand All @@ -44,6 +50,11 @@ def __init__(self, schema_path: str, rules_path: str):
self.rules[mod.Rule.id] = mod.Rule
except: # noqa: E722
logger.error("Failed loading rule: {}".format(filename))
elif rules_path == DEFAULT_RULES:
logger.info("No rules found")
else:
logger.error("Rules directory not found: {}".format(rules_path))
sys.exit(1)

def _validate_syntax_file(self, file_path: str, strict: bool = True) -> None:
"""Run syntactic validation for a single file"""
Expand Down

0 comments on commit b0e0a68

Please sign in to comment.