2019-10-07 06:52:00 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-10-08 09:44:35 +00:00
|
|
|
"""Entrypoint and CLI handler."""
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
import argparse
|
2019-10-07 12:44:45 +00:00
|
|
|
import logging
|
2019-10-07 06:52:00 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2019-10-08 09:39:27 +00:00
|
|
|
import ansibledoctor.Exception
|
2019-10-07 06:52:00 +00:00
|
|
|
from ansibledoctor import __version__
|
|
|
|
from ansibledoctor.Config import SingleConfig
|
|
|
|
from ansibledoctor.DocumentationGenerator import Generator
|
|
|
|
from ansibledoctor.DocumentationParser import Parser
|
|
|
|
from ansibledoctor.Utils import SingleLog
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleDoctor:
|
|
|
|
|
|
|
|
def __init__(self):
|
2019-10-07 12:44:45 +00:00
|
|
|
self.log = SingleLog()
|
|
|
|
self.logger = self.log.logger
|
|
|
|
self.args = self._cli_args()
|
|
|
|
self.config = self._get_config()
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
doc_parser = Parser()
|
|
|
|
doc_generator = Generator(doc_parser)
|
|
|
|
doc_generator.render()
|
|
|
|
|
|
|
|
def _cli_args(self):
|
|
|
|
"""
|
|
|
|
Use argparse for parsing CLI arguments.
|
|
|
|
|
|
|
|
:return: args objec
|
|
|
|
"""
|
2019-10-08 22:56:39 +00:00
|
|
|
# TODO: add function to print to stdout instead of file
|
2019-10-07 06:52:00 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
2019-10-07 12:44:45 +00:00
|
|
|
description="Generate documentation from annotated Ansible roles using templates")
|
|
|
|
parser.add_argument("base_dir", nargs="?", help="role directory, (default: current working dir)")
|
2019-10-08 09:30:31 +00:00
|
|
|
parser.add_argument("-c", "--config", nargs="?", dest="config_file", help="location of configuration file")
|
2019-10-07 12:44:45 +00:00
|
|
|
parser.add_argument("-o", "--output", action="store", dest="output_dir", type=str,
|
2019-10-07 06:52:00 +00:00
|
|
|
help="output base dir")
|
2019-10-08 22:56:39 +00:00
|
|
|
parser.add_argument("-f", "--force", action="store_true", default=None, dest="force_overwrite",
|
2019-10-07 12:44:45 +00:00
|
|
|
help="force overwrite output file")
|
2019-10-08 22:56:39 +00:00
|
|
|
parser.add_argument("-d", "--dry-run", action="store_true", default=None, dest="dry_run",
|
|
|
|
help="dry run without writing")
|
2019-10-07 12:44:45 +00:00
|
|
|
parser.add_argument("-v", dest="logging.level", action="append_const", const=-1,
|
|
|
|
help="increase log level")
|
|
|
|
parser.add_argument("-q", dest="logging.level", action="append_const",
|
|
|
|
const=1, help="decrease log level")
|
2019-10-07 06:52:00 +00:00
|
|
|
parser.add_argument("--version", action="version", version="%(prog)s {}".format(__version__))
|
|
|
|
|
2019-10-07 12:44:45 +00:00
|
|
|
return parser.parse_args().__dict__
|
2019-10-07 06:52:00 +00:00
|
|
|
|
2019-10-07 12:44:45 +00:00
|
|
|
def _get_config(self):
|
2019-10-08 09:30:31 +00:00
|
|
|
try:
|
|
|
|
config = SingleConfig(args=self.args)
|
|
|
|
except ansibledoctor.Exception.ConfigError as e:
|
|
|
|
self.log.sysexit_with_message(e)
|
|
|
|
|
2019-10-08 09:43:55 +00:00
|
|
|
self.log.set_level(config.config["logging"]["level"])
|
|
|
|
|
2019-10-07 12:44:45 +00:00
|
|
|
if config.is_role:
|
|
|
|
self.logger.info("Ansible role detected")
|
2019-10-07 06:52:00 +00:00
|
|
|
else:
|
2019-10-08 09:30:31 +00:00
|
|
|
self.log.sysexit_with_message("No Ansible role detected")
|
|
|
|
|
|
|
|
self.logger.info("Using config file {}".format(config.config_file))
|
2019-10-07 12:44:45 +00:00
|
|
|
|
|
|
|
return config
|