ansible-later/ansiblelater/__main__.py

62 lines
2.3 KiB
Python
Raw Normal View History

2018-12-19 10:19:07 +00:00
#!/usr/bin/env python
2019-04-04 14:06:18 +00:00
"""Main program."""
2018-12-19 10:19:07 +00:00
import argparse
2018-12-19 10:19:07 +00:00
import logging
2019-03-28 00:20:43 +00:00
from ansiblelater import LOG, __version__, logger
from ansiblelater.command import base, candidates
2018-12-19 10:19:07 +00:00
def main():
2019-04-04 14:06:18 +00:00
"""Run main program."""
parser = argparse.ArgumentParser(
description="Validate ansible files against best pratice guideline")
2019-04-02 14:34:03 +00:00
parser.add_argument("-c", "--config", dest="config_file",
help="Location of configuration file")
2019-04-02 14:34:03 +00:00
parser.add_argument("-r", "--rules", dest="rules.standards",
help="Location of standards rules")
2019-04-02 14:34:03 +00:00
parser.add_argument("-q", "--quiet", dest="logging.level", action="store_const",
const=logging.ERROR, help="Only output errors")
2019-04-02 14:34:03 +00:00
parser.add_argument("-s", "--standards", dest="rules.filter", action="append",
help="limit standards to specific names")
2019-04-02 14:34:03 +00:00
parser.add_argument("-v", dest="logging.level", action="count",
2019-03-28 00:20:43 +00:00
help="Show more verbose output")
2019-04-02 14:34:03 +00:00
parser.add_argument("rules.files", nargs="*")
parser.add_argument("--version", action="version", version="%(prog)s {}".format(__version__))
args = parser.parse_args().__dict__
2018-12-19 10:19:07 +00:00
settings = base.get_settings(args)
2019-04-03 15:42:46 +00:00
config = settings.config
2019-04-02 14:34:03 +00:00
# print(json.dumps(settings.config, indent=4, sort_keys=True))
2019-04-03 15:42:46 +00:00
logger.update_logger(LOG, config["logging"]["level"], config["logging"]["json"])
files = config["rules"]["files"]
standards = base.get_standards(config["rules"]["standards"])
errors = 0
2019-04-02 14:34:03 +00:00
for filename in files:
lines = None
candidate = candidates.classify(filename, settings, standards)
if candidate:
if candidate.binary:
LOG.info("Not reviewing binary file %s" % filename)
continue
if candidate.vault:
LOG.info("Not reviewing vault file %s" % filename)
continue
if lines:
LOG.info("Reviewing %s lines %s" % (candidate, lines))
else:
LOG.info("Reviewing all of %s" % candidate)
errors = errors + candidate.review(settings, lines)
else:
LOG.info("Couldn't classify file %s" % filename)
# return errors
2018-12-19 10:19:07 +00:00
2019-01-08 15:22:19 +00:00
if __name__ == "__main__":
main()