Skip to content

Instantly share code, notes, and snippets.

@msawangwan
Last active April 17, 2020 23:12
Show Gist options
  • Save msawangwan/165859d94ca0901ddd21f19831033413 to your computer and use it in GitHub Desktop.
Save msawangwan/165859d94ca0901ddd21f19831033413 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
'''
simple yaml file validator, reports any syntax errors.
'''
import sys
import argparse
# pylint: disable=import-error
import yaml
def validate(*filepaths) -> int:
exit_code = 0
for filepath in filepaths:
with open(filepath, 'r', encoding='utf-8') as f:
sys.stdout.write('{}'.format(filepath))
try:
for _ in yaml.safe_load_all(f.read()):
pass
except Exception as e:
exit_code = 1
sys.stdout.write(' - error \n\n({})\n\n'.format(e))
else:
sys.stdout.write(' - ok\n')
return exit_code
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'filepaths',
nargs='+',
help='''validate one or more yaml files.''',
)
args = parser.parse_args()
sys.exit(validate(*args.filepaths))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment