Skip to content

Commit

Permalink
feat: put pass/fail out front
Browse files Browse the repository at this point in the history
  • Loading branch information
donbowman committed Apr 11, 2020
1 parent 4dc3959 commit 7b1ef16
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ k8s-validate [--kubernetes-version X.X.X] [--exclude glob-pattern] files

Add a section similar to this to your .pre-commit-hooks.yaml:
- repo: https://github.com/Agilicus/pre-commit-hook-k8svalidate.git
rev: v0.0.6
rev: v0.0.7
hooks:
- id: k8svalidate
args: [--exclude, "**/*.patch.yaml"]
Expand Down
50 changes: 29 additions & 21 deletions pre-commit-hooks/k8svalidate
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def is_valid_type(doc):
bool: true if we should process
"""
valid = True
if "apiVersion" not in doc or "kind" not in doc:
if not doc or "apiVersion" not in doc or "kind" not in doc:
valid = False
return valid

Expand Down Expand Up @@ -82,26 +82,34 @@ def main():
overall_result = True
version = args.kubernetes_version or utils.latest_version()
for input in unskipped(args.files, args.exclude):
with open(input, "r") as i_file:
data = i_file.read()
docs = list(YAML().load_all(data))
for doc in docs:
result = "Valid"
valid = True
if is_valid_type(doc):
try:
utils.validate(doc, version, args.strict)
except utils.SchemaNotFoundError:
result = "Skipped [Unknown Schema]"
except jsonschema.exceptions.ValidationError as e:
result = f"FAIL [{e.args[0]}]"
valid = False
else:
result = "Skipped [Not Kubernetes YAML]"
if not valid:
overall_result = False
s_valid = "PASS" if valid else "FAIL"
print(f"{s_valid}: file {input} -> {result}")
try:
with open(input, "r") as i_file:
data = i_file.read()
docs = list(YAML().load_all(data))
for doc in docs:
result = "Valid"
valid = True
operation = ""
if is_valid_type(doc):
try:
utils.validate(doc, version, args.strict)
operation = "PASS"
except utils.SchemaNotFoundError:
result = "Skipped [Unknown Schema]"
operation = "SKIP"
except jsonschema.exceptions.ValidationError as e:
result = f"FAIL [{e.args[0]}]"
valid = False
operation = "FAIL"
else:
result = "Skipped [Not Kubernetes YAML]"
operation = "SKIP"
if not valid:
overall_result = False
print(f"{operation}: file {input} -> {result}")
except FileNotFoundError:
print(f"SKIP: file {input} -> Does not exist")

return overall_result


Expand Down

0 comments on commit 7b1ef16

Please sign in to comment.