Skip to content

Commit

Permalink
feat: migrate to dynaconf to handle multi-source configuration (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Jun 7, 2024
1 parent 9b20c11 commit 8e042c7
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 466 deletions.
2 changes: 1 addition & 1 deletion Containerfile.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ENV TZ=UTC

ADD dist/ansible_doctor-*.whl /

RUN apk --update add --virtual .build-deps build-base libffi-dev openssl-dev && \
RUN apk --update add --virtual .build-deps build-base libffi-dev openssl-dev git && \
pip install --upgrade --no-cache-dir pip && \
pip install --no-cache-dir $(find / -name "ansible_doctor-*.whl")[ansible-core] && \
rm -f ansible_doctor-*.whl && \
Expand Down
89 changes: 58 additions & 31 deletions ansibledoctor/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ class AnsibleDoctor:
def __init__(self):
self.log = SingleLog()
self.logger = self.log.logger
self.args = self._cli_args()
self.config = self._get_config()

try:
self.config = SingleConfig()
self.config.load(args=self._parse_args())
self.log.register_hanlers(json=self.config.config.logging.json)
except ansibledoctor.exception.ConfigError as e:
self.log.sysexit_with_message(e)

self._execute()

def _cli_args(self):
def _parse_args(self):
"""
Use argparse for parsing CLI arguments.
Expand All @@ -33,86 +39,107 @@ def _cli_args(self):
description="Generate documentation from annotated Ansible roles using templates"
)
parser.add_argument(
"base_dir", nargs="?", help="base directory (default: current working directory)"
"base_dir",
nargs="?",
default=self.config.config.base_dir,
help="base directory (default: current working directory)",
)
parser.add_argument(
"-c", "--config", dest="config_file", help="path to configuration file"
"-c",
"--config",
dest="config_file",
help="path to configuration file",
)
parser.add_argument(
"-o", "--output", dest="output_dir", action="store", help="output directory"
"-o",
"--output",
dest="renderer__dest",
action="store",
default=self.config.config.renderer.dest,
help="output directory",
metavar="OUTPUT_DIR",
)
parser.add_argument(
"-r",
"--recursive",
dest="recursive",
action="store_true",
default=None,
default=self.config.config.recursive,
help="run recursively over the base directory subfolders",
)
parser.add_argument(
"-f",
"--force",
dest="force_overwrite",
dest="renderer.force_overwrite",
action="store_true",
default=None,
default=self.config.config.renderer.force_overwrite,
help="force overwrite output file",
)
parser.add_argument(
"-d",
"--dry-run",
dest="dry_run",
action="store_true",
default=None,
default=self.config.config.dry_run,
help="dry run without writing",
)
parser.add_argument(
"-n",
"--no-role-detection",
dest="role_detection",
action="store_false",
default=None,
default=self.config.config.role.autodetect,
help="disable automatic role detection",
)
parser.add_argument(
"-v", dest="logging.level", action="append_const", const=-1, help="increase log level"
"-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",
)
parser.add_argument(
"-q", dest="logging.level", action="append_const", const=1, help="decrease log level"
"--version",
action="version",
version=f"%(prog)s {__version__}",
)
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")

return parser.parse_args().__dict__

def _get_config(self):
try:
config = SingleConfig(args=self.args)
except ansibledoctor.exception.ConfigError as e:
self.log.sysexit_with_message(e)

return config

def _execute(self):
cwd = self.config.base_dir
cwd = os.path.abspath(self.config.config.base_dir)
walkdirs = [cwd]

if self.config.recursive:
if self.config.config.recursive:
walkdirs = [f.path for f in os.scandir(cwd) if f.is_dir()]

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"])
self.config.load(root_path=os.getcwd())
self.log.register_hanlers(json=self.config.config.logging.json)
except ansibledoctor.exception.ConfigError as e:
self.log.sysexit_with_message(e)

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{e!s}")
self.logger.info(f"Using config file: {self.config.config_file}")
self.logger.info(f"Using config file: {self.config.config_files}")

self.logger.debug(f"Using working dir: {item}")
self.logger.debug(f"Using working directory: {os.path.relpath(item, self.log.ctx)}")

if self.config.config["role_detection"]:
if self.config.is_role:
self.logger.info(f"Ansible role detected: {self.config.config['role_name']}")
if self.config.config.role.autodetect:
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:
Expand Down
Loading

0 comments on commit 8e042c7

Please sign in to comment.