Skip to content

Commit

Permalink
Add --templates-path option
Browse files Browse the repository at this point in the history
Signed-off-by: NilashishC <[email protected]>
  • Loading branch information
NilashishC committed Jan 30, 2024
1 parent 896a5e1 commit 04f0a21
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/ansible_creator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ def parse_args(self: Cli) -> argparse.Namespace:
"current working directory.",
)

init_command_parser.add_argument(
"--templates-path",
help="Path to a directory containing custom template for collection skeleton."
" When provided, this will be used to scaffold the collection instead of the"
" in-built template.",
)

init_command_parser.add_argument(
"--force",
default=False,
Expand Down
4 changes: 4 additions & 0 deletions src/ansible_creator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Config:
no_ansi: bool
subcommand: str
verbose: int
templates_path: str

collection: str = ""
force: bool = False
Expand All @@ -36,4 +37,7 @@ def __post_init__(self: Config) -> None:
object.__setattr__(self, "namespace", fqcn[0])
object.__setattr__(self, "collection_name", fqcn[-1])

if self.templates_path:
object.__setattr__(self, "template", expand_path(self.init_path))

object.__setattr__(self, "init_path", expand_path(self.init_path))
35 changes: 35 additions & 0 deletions src/ansible_creator/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import os
import re
import shutil

from typing import TYPE_CHECKING
Expand Down Expand Up @@ -36,6 +37,7 @@ def __init__(
self._force = config.force
self._creator_version = config.creator_version
self._templar = Templar()
self._templates_path = config.templates_path
self.output: Output = output

def run(self: Init) -> None:
Expand All @@ -47,6 +49,38 @@ def run(self: Init) -> None:

self.output.debug(msg=f"final collection path set to {col_path}")

# validate custom templates
if self._templates_path:
full_templates_path = os.path.join(self._templates_path, "new_collection")

# verify that templates path exists and has "new_collection" dir
if not os.path.exists(full_templates_path):
msg = f"could not find templates for new collection at {self._templates_path}."
raise CreatorError(msg)

# ensure that "new_collection" dir is not empty
if len(os.listdir(full_templates_path)) == 0:
msg = (
"please ensure that a directory named `new_collection` exists"
f" at {self._templates_path} and is not empty."
)
raise CreatorError(msg)

# ensure that a template for galaxy file exists in "new_collection" dir
if (
len(
list(
filter(
re.compile("(?:galaxy.)(?:yml|yaml)(?:.j2)?").match,
os.listdir(full_templates_path),
),
),
)
== 0
):
msg = f"template for Ansible galaxy file not found in {full_templates_path!s}."
raise CreatorError(msg)

# check if init_path already exists
if os.path.exists(col_path):
if os.path.isfile(col_path):
Expand Down Expand Up @@ -86,6 +120,7 @@ def run(self: Init) -> None:
source="new_collection",
dest=col_path,
templar=self._templar,
templates_path=self._templates_path,
template_data={
"namespace": self._namespace,
"collection_name": self._collection_name,
Expand Down
18 changes: 15 additions & 3 deletions src/ansible_creator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys

from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING

from ansible_creator.exceptions import CreatorError
Expand Down Expand Up @@ -84,6 +85,7 @@ def copy_container( # noqa: PLR0913
dest: str,
output: Output,
templar: Templar,
templates_path: str,
template_data: dict[str, str],
allow_overwrite: list[str] | None = None,
) -> None:
Expand All @@ -100,10 +102,13 @@ def copy_container( # noqa: PLR0913
output.debug(msg=f"starting recursive copy with source container '{source}'")
output.debug(msg=f"allow_overwrite set to {allow_overwrite}")

def _recursive_copy(root: abc.Traversable) -> None:
if templates_path:
output.debug(msg=f"custom templates path set to {templates_path}")

def _recursive_copy(root: Path | abc.Traversable) -> None:
"""Recursively traverses a resource container and copies content to destination.
:param root: A traversable object representing root of the container to copy.
:param root: A traversable or Path object representing root of the container to copy.
"""
output.debug(msg=f"current root set to {root}")

Expand Down Expand Up @@ -143,4 +148,11 @@ def _recursive_copy(root: abc.Traversable) -> None:
with open(dest_file, "w", encoding="utf-8") as df_handle:
df_handle.write(content)

_recursive_copy(root=resources.files(f"ansible_creator.resources.{source}"))
if templates_path:
# use custom templates path
root = Path(templates_path) / source
else:
# use built-in templates
root = resources.files(f"ansible_creator.resources.{source}")

_recursive_copy(root)

0 comments on commit 04f0a21

Please sign in to comment.