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
|
|
|
|
2024-06-17 11:51:03 +00:00
|
|
|
import structlog
|
|
|
|
|
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
|
2024-06-17 11:51:03 +00:00
|
|
|
from ansibledoctor.utils import sysexit_with_message
|
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
|
|
|
|
2024-06-17 11:51:03 +00:00
|
|
|
log = structlog.get_logger()
|
2024-06-07 19:51:10 +00:00
|
|
|
|
2024-06-17 11:51:03 +00:00
|
|
|
def __init__(self):
|
2024-06-07 19:51:10 +00:00
|
|
|
try:
|
|
|
|
self.config = SingleConfig()
|
|
|
|
self.config.load(args=self._parse_args())
|
2024-06-12 18:59:55 +00:00
|
|
|
self._execute()
|
|
|
|
except ansibledoctor.exception.DoctorError as e:
|
2024-06-17 11:51:03 +00:00
|
|
|
sysexit_with_message(e)
|
2024-06-12 18:59:55 +00:00
|
|
|
except KeyboardInterrupt:
|
2024-06-17 11:51:03 +00:00
|
|
|
sysexit_with_message("Aborted...")
|
2019-10-07 06:52:00 +00:00
|
|
|
|
2024-06-07 19:51:10 +00:00
|
|
|
def _parse_args(self):
|
2019-10-07 06:52:00 +00:00
|
|
|
"""
|
|
|
|
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(
|
2024-06-07 19:51:10 +00:00
|
|
|
"base_dir",
|
|
|
|
nargs="?",
|
|
|
|
default=self.config.config.base_dir,
|
|
|
|
help="base directory (default: current working directory)",
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2024-06-07 19:51:10 +00:00
|
|
|
"-c",
|
|
|
|
"--config",
|
|
|
|
dest="config_file",
|
|
|
|
help="path to configuration file",
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2024-06-07 19:51:10 +00:00
|
|
|
"-o",
|
|
|
|
"--output",
|
|
|
|
dest="renderer__dest",
|
|
|
|
action="store",
|
|
|
|
default=self.config.config.renderer.dest,
|
|
|
|
help="output directory",
|
|
|
|
metavar="OUTPUT_DIR",
|
2023-02-12 11:57:57 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-r",
|
|
|
|
"--recursive",
|
|
|
|
dest="recursive",
|
|
|
|
action="store_true",
|
2024-06-07 19:51:10 +00:00
|
|
|
default=self.config.config.recursive,
|
2023-11-10 13:50:50 +00:00
|
|
|
help="run recursively over the base directory subfolders",
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--force",
|
2024-06-07 19:51:10 +00:00
|
|
|
dest="renderer.force_overwrite",
|
2020-04-05 21:16:53 +00:00
|
|
|
action="store_true",
|
2024-06-07 19:51:10 +00:00
|
|
|
default=self.config.config.renderer.force_overwrite,
|
2023-11-10 13:50:50 +00:00
|
|
|
help="force overwrite output file",
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--dry-run",
|
|
|
|
dest="dry_run",
|
|
|
|
action="store_true",
|
2024-06-07 19:51:10 +00:00
|
|
|
default=self.config.config.dry_run,
|
2023-11-10 13:50:50 +00:00
|
|
|
help="dry run without writing",
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
2021-07-27 20:01:56 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--no-role-detection",
|
|
|
|
dest="role_detection",
|
|
|
|
action="store_false",
|
2024-06-07 19:51:10 +00:00
|
|
|
default=self.config.config.role.autodetect,
|
2023-11-10 13:50:50 +00:00
|
|
|
help="disable automatic role detection",
|
2021-07-27 20:01:56 +00:00
|
|
|
)
|
2020-04-05 21:16:53 +00:00
|
|
|
parser.add_argument(
|
2024-06-07 19:51:10 +00:00
|
|
|
"-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",
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2024-06-07 19:51:10 +00:00
|
|
|
"--version",
|
|
|
|
action="version",
|
|
|
|
version=f"%(prog)s {__version__}",
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
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
|
|
|
|
2023-02-12 11:57:57 +00:00
|
|
|
def _execute(self):
|
2024-06-07 19:51:10 +00:00
|
|
|
cwd = os.path.abspath(self.config.config.base_dir)
|
2023-02-12 11:57:57 +00:00
|
|
|
walkdirs = [cwd]
|
2019-10-08 09:30:31 +00:00
|
|
|
|
2024-06-07 19:51:10 +00:00
|
|
|
if self.config.config.recursive:
|
2023-02-12 11:57:57 +00:00
|
|
|
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)
|
2024-06-12 18:59:55 +00:00
|
|
|
self.config.load(root_path=os.getcwd())
|
2023-02-12 11:57:57 +00:00
|
|
|
|
2024-06-17 11:51:03 +00:00
|
|
|
self.log.debug("Switch working directory", path=item)
|
|
|
|
self.log.info("Lookup config file", path=self.config.config_files)
|
2023-02-12 11:57:57 +00:00
|
|
|
|
2024-06-07 19:51:10 +00:00
|
|
|
if self.config.config.role.autodetect:
|
|
|
|
if self.config.is_role():
|
2024-06-17 11:51:03 +00:00
|
|
|
structlog.contextvars.bind_contextvars(role=self.config.config.role_name)
|
|
|
|
self.log.info("Ansible role detected")
|
2023-02-12 11:57:57 +00:00
|
|
|
else:
|
2024-06-17 11:51:03 +00:00
|
|
|
sysexit_with_message("No Ansible role detected")
|
2023-02-12 11:57:57 +00:00
|
|
|
else:
|
2024-06-17 11:51:03 +00:00
|
|
|
self.log.info("Ansible role detection disabled")
|
2023-02-12 11:57:57 +00:00
|
|
|
|
|
|
|
doc_parser = Parser()
|
|
|
|
doc_generator = Generator(doc_parser)
|
|
|
|
doc_generator.render()
|
2020-12-31 13:16:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
AnsibleDoctor()
|