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
|
2023-02-12 11:57:57 +00:00
|
|
|
import os
|
2019-10-07 06:52:00 +00:00
|
|
|
|
2021-01-01 12:50:41 +00:00
|
|
|
import ansibledoctor.exception
|
2019-10-07 06:52:00 +00:00
|
|
|
from ansibledoctor import __version__
|
2021-01-01 12:50:41 +00:00
|
|
|
from ansibledoctor.config import SingleConfig
|
|
|
|
from ansibledoctor.doc_generator import Generator
|
|
|
|
from ansibledoctor.doc_parser import Parser
|
|
|
|
from ansibledoctor.utils import SingleLog
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AnsibleDoctor:
|
2023-01-21 21:17:54 +00:00
|
|
|
"""Create main object."""
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
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()
|
2023-02-12 11:57:57 +00:00
|
|
|
self._execute()
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
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(
|
2020-04-05 21:16:53 +00:00
|
|
|
description="Generate documentation from annotated Ansible roles using templates"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-02-12 11:57:57 +00:00
|
|
|
"base_dir", nargs="?", help="base directory (default: current working directory)"
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-02-12 11:57:57 +00:00
|
|
|
"-c", "--config", dest="config_file", help="path to configuration file"
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-02-12 11:57:57 +00:00
|
|
|
"-o", "--output", dest="output_dir", action="store", help="output directory"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-r",
|
|
|
|
"--recursive",
|
|
|
|
dest="recursive",
|
|
|
|
action="store_true",
|
|
|
|
default=None,
|
|
|
|
help="run recursively over the base directory subfolders"
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--force",
|
|
|
|
dest="force_overwrite",
|
|
|
|
action="store_true",
|
|
|
|
default=None,
|
|
|
|
help="force overwrite output file"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--dry-run",
|
|
|
|
dest="dry_run",
|
|
|
|
action="store_true",
|
|
|
|
default=None,
|
|
|
|
help="dry run without writing"
|
|
|
|
)
|
2021-07-27 20:01:56 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--no-role-detection",
|
|
|
|
dest="role_detection",
|
|
|
|
action="store_false",
|
|
|
|
default=None,
|
|
|
|
help="disable automatic role detection"
|
|
|
|
)
|
2020-04-05 21:16:53 +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"
|
|
|
|
)
|
2023-01-20 10:56:12 +00:00
|
|
|
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
2019-10-07 06:52:00 +00:00
|
|
|
|
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)
|
2021-01-01 12:50:41 +00:00
|
|
|
except ansibledoctor.exception.ConfigError as e:
|
2019-10-08 09:30:31 +00:00
|
|
|
self.log.sysexit_with_message(e)
|
|
|
|
|
2023-02-12 11:57:57 +00:00
|
|
|
return config
|
2019-10-08 09:43:55 +00:00
|
|
|
|
2023-02-12 11:57:57 +00:00
|
|
|
def _execute(self):
|
|
|
|
cwd = self.config.base_dir
|
|
|
|
walkdirs = [cwd]
|
2019-10-08 09:30:31 +00:00
|
|
|
|
2023-02-12 11:57:57 +00:00
|
|
|
if self.config.recursive:
|
|
|
|
walkdirs = [f.path for f in os.scandir(cwd) if f.is_dir()]
|
2019-10-07 12:44:45 +00:00
|
|
|
|
2023-02-12 11:57:57 +00:00
|
|
|
for item in walkdirs:
|
|
|
|
os.chdir(item)
|
|
|
|
|
|
|
|
self.config.set_config(base_dir=os.getcwd())
|
|
|
|
try:
|
|
|
|
self.log.set_level(self.config.config["logging"]["level"])
|
|
|
|
except ValueError as e:
|
|
|
|
self.log.sysexit_with_message(f"Can not set log level.\n{str(e)}")
|
|
|
|
self.logger.info(f"Using config file: {self.config.config_file}")
|
|
|
|
|
|
|
|
self.logger.debug(f"Using working dir: {item}")
|
|
|
|
|
|
|
|
if self.config.config["role_detection"]:
|
|
|
|
if self.config.is_role:
|
|
|
|
self.logger.info(f"Ansible role detected: {self.config.config['role_name']}")
|
|
|
|
else:
|
|
|
|
self.log.sysexit_with_message("No Ansible role detected")
|
|
|
|
else:
|
|
|
|
self.logger.info("Ansible role detection disabled")
|
|
|
|
|
|
|
|
doc_parser = Parser()
|
|
|
|
doc_generator = Generator(doc_parser)
|
|
|
|
doc_generator.render()
|
2020-12-31 13:16:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
AnsibleDoctor()
|