Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions infrahub_sdk/ctl/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ..ctl import config
from ..ctl.client import initialize_client
from ..ctl.exceptions import QueryNotFoundError
from ..ctl.repository import get_repository_config
from ..ctl.repository import find_repository_config_file, get_repository_config
from ..ctl.utils import catch_exception, execute_graphql_query
from ..exceptions import ModuleImportError

Expand Down Expand Up @@ -59,7 +59,7 @@ def run(
FORMAT = "%(message)s"
logging.basicConfig(level=log_level, format=FORMAT, datefmt="[%X]", handlers=[RichHandler()])

repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
repository_config = get_repository_config(find_repository_config_file())

if list_available:
list_checks(repository_config=repository_config)
Expand Down
10 changes: 5 additions & 5 deletions infrahub_sdk/ctl/cli_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from ..ctl.object import app as object_app
from ..ctl.render import list_jinja2_transforms, print_template_errors
from ..ctl.repository import app as repository_app
from ..ctl.repository import get_repository_config
from ..ctl.repository import find_repository_config_file, get_repository_config
from ..ctl.schema import app as schema_app
from ..ctl.transform import list_transforms
from ..ctl.utils import (
Expand Down Expand Up @@ -260,7 +260,7 @@ async def render(
"""Render a local Jinja2 Transform for debugging purpose."""

variables_dict = parse_cli_vars(variables)
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
repository_config = get_repository_config(find_repository_config_file())

if list_available or not transform_name:
list_jinja2_transforms(config=repository_config)
Expand All @@ -270,7 +270,7 @@ async def render(
try:
transform_config = repository_config.get_jinja2_transform(name=transform_name)
except KeyError as exc:
console.print(f'[red]Unable to find "{transform_name}" in {config.INFRAHUB_REPO_CONFIG_FILE}')
console.print(f'[red]Unable to find "{transform_name}" in repository config file')
list_jinja2_transforms(config=repository_config)
raise typer.Exit(1) from exc

Expand Down Expand Up @@ -310,7 +310,7 @@ def transform(
"""Render a local transform (TransformPython) for debugging purpose."""

variables_dict = parse_cli_vars(variables)
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
repository_config = get_repository_config(find_repository_config_file())

if list_available or not transform_name:
list_transforms(config=repository_config)
Expand Down Expand Up @@ -469,7 +469,7 @@ def info( # noqa: PLR0915
pretty_model = Pretty(client.config.model_dump(), expand_all=True)
layout["client_info"].update(Panel(pretty_model, title="Client Info"))

# Infrahub information planel
# Infrahub information panel
infrahub_info = Table(show_header=False, box=None)
if info["user_info"]:
infrahub_info.add_row("User:", info["user_info"]["AccountProfile"]["display_label"])
Expand Down
3 changes: 2 additions & 1 deletion infrahub_sdk/ctl/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DEFAULT_CONFIG_FILE = "infrahubctl.toml"
ENVVAR_CONFIG_FILE = "INFRAHUBCTL_CONFIG"
INFRAHUB_REPO_CONFIG_FILE = ".infrahub.yml"
INFRAHUB_REPO_CONFIG_FILE_ALT = ".infrahub.yaml"


class Settings(BaseSettings):
Expand Down Expand Up @@ -69,7 +70,7 @@ def load(self, config_file: str | Path = "infrahubctl.toml", config_data: dict |
def load_and_exit(self, config_file: str | Path = "infrahubctl.toml", config_data: dict | None = None) -> None:
"""Calls load, but wraps it in a try except block.

This is done to handle a ValidationErorr which is raised when settings are specified but invalid.
This is done to handle a ValidationError which is raised when settings are specified but invalid.
In such cases, a message is printed to the screen indicating the settings which don't pass validation.

Args:
Expand Down
4 changes: 2 additions & 2 deletions infrahub_sdk/ctl/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..ctl import config
from ..ctl.client import initialize_client
from ..ctl.repository import get_repository_config
from ..ctl.repository import find_repository_config_file, get_repository_config
from ..ctl.utils import execute_graphql_query, init_logging, parse_cli_vars
from ..exceptions import ModuleImportError
from ..node import InfrahubNode
Expand All @@ -26,7 +26,7 @@ async def run(
variables: Optional[list[str]] = None,
) -> None:
init_logging(debug=debug)
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
repository_config = get_repository_config(find_repository_config_file())

if list_available or not generator_name:
list_generators(repository_config=repository_config)
Expand Down
40 changes: 39 additions & 1 deletion infrahub_sdk/ctl/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,49 @@
console = Console()


def find_repository_config_file(base_path: Path | None = None) -> Path:
"""Find the repository config file, checking for both .yml and .yaml extensions.

Args:
base_path: Base directory to search in. If None, uses current directory.

Returns:
Path to the config file.

Raises:
FileNotFoundError: If neither .infrahub.yml nor .infrahub.yaml exists.
"""
if base_path is None:
base_path = Path()

yml_path = base_path / ".infrahub.yml"
yaml_path = base_path / ".infrahub.yaml"

# Prefer .yml if both exist
if yml_path.exists():
return yml_path
if yaml_path.exists():
return yaml_path
# For backward compatibility, return .yml path for error messages
return yml_path


def get_repository_config(repo_config_file: Path) -> InfrahubRepositoryConfig:
# If the file doesn't exist, try to find it with alternate extension
if not repo_config_file.exists():
if repo_config_file.name == ".infrahub.yml":
alt_path = repo_config_file.parent / ".infrahub.yaml"
if alt_path.exists():
repo_config_file = alt_path
elif repo_config_file.name == ".infrahub.yaml":
alt_path = repo_config_file.parent / ".infrahub.yml"
if alt_path.exists():
repo_config_file = alt_path

try:
config_file_data = load_repository_config_file(repo_config_file)
except FileNotFoundError as exc:
console.print(f"[red]File not found {exc}")
console.print(f"[red]File not found {exc} (also checked for .infrahub.yml and .infrahub.yaml)")
raise typer.Exit(1) from exc
except FileNotValidError as exc:
console.print(f"[red]{exc.message}")
Expand Down
11 changes: 7 additions & 4 deletions infrahub_sdk/pytest_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .. import InfrahubClientSync
from ..utils import is_valid_url
from .loader import InfrahubYamlFile
from .utils import load_repository_config
from .utils import find_repository_config_file, load_repository_config


def pytest_addoption(parser: Parser) -> None:
Expand All @@ -18,9 +18,9 @@ def pytest_addoption(parser: Parser) -> None:
"--infrahub-repo-config",
action="store",
dest="infrahub_repo_config",
default=".infrahub.yml",
default=None,
metavar="INFRAHUB_REPO_CONFIG_FILE",
help="Infrahub configuration file for the repository (default: %(default)s)",
help="Infrahub configuration file for the repository (.infrahub.yml or .infrahub.yaml)",
)
group.addoption(
"--infrahub-address",
Expand Down Expand Up @@ -63,7 +63,10 @@ def pytest_addoption(parser: Parser) -> None:


def pytest_sessionstart(session: Session) -> None:
session.infrahub_config_path = Path(session.config.option.infrahub_repo_config) # type: ignore[attr-defined]
if session.config.option.infrahub_repo_config:
session.infrahub_config_path = Path(session.config.option.infrahub_repo_config) # type: ignore[attr-defined]
else:
session.infrahub_config_path = find_repository_config_file() # type: ignore[attr-defined]

if session.infrahub_config_path.is_file(): # type: ignore[attr-defined]
session.infrahub_repo_config = load_repository_config(repo_config_file=session.infrahub_config_path) # type: ignore[attr-defined]
Expand Down
38 changes: 38 additions & 0 deletions infrahub_sdk/pytest_plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,45 @@
from .exceptions import FileNotValidError


def find_repository_config_file(base_path: Path | None = None) -> Path:
"""Find the repository config file, checking for both .yml and .yaml extensions.

Args:
base_path: Base directory to search in. If None, uses current directory.

Returns:
Path to the config file.

Raises:
FileNotFoundError: If neither .infrahub.yml nor .infrahub.yaml exists.
"""
if base_path is None:
base_path = Path()

yml_path = base_path / ".infrahub.yml"
yaml_path = base_path / ".infrahub.yaml"

# Prefer .yml if both exist
if yml_path.exists():
return yml_path
if yaml_path.exists():
return yaml_path
# For backward compatibility, return .yml path for error messages
return yml_path


def load_repository_config(repo_config_file: Path) -> InfrahubRepositoryConfig:
# If the file doesn't exist, try to find it with alternate extension
if not repo_config_file.exists():
if repo_config_file.name == ".infrahub.yml":
alt_path = repo_config_file.parent / ".infrahub.yaml"
if alt_path.exists():
repo_config_file = alt_path
elif repo_config_file.name == ".infrahub.yaml":
alt_path = repo_config_file.parent / ".infrahub.yml"
if alt_path.exists():
repo_config_file = alt_path

if not repo_config_file.is_file():
raise FileNotFoundError(repo_config_file)

Expand Down
Loading