Skip to content

Commit bf9571b

Browse files
authored
Merge pull request #492 from opsmill/pmc-yaml-2
Support infrahub.yml or infrahub.yaml configuration files
2 parents c1d1bd4 + 06905fa commit bf9571b

File tree

7 files changed

+95
-16
lines changed

7 files changed

+95
-16
lines changed

infrahub_sdk/ctl/check.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
from rich.console import Console
1212
from rich.logging import RichHandler
1313

14-
from ..ctl import config
1514
from ..ctl.client import initialize_client
1615
from ..ctl.exceptions import QueryNotFoundError
17-
from ..ctl.repository import get_repository_config
16+
from ..ctl.repository import find_repository_config_file, get_repository_config
1817
from ..ctl.utils import catch_exception, execute_graphql_query
1918
from ..exceptions import ModuleImportError
2019

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

62-
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
61+
repository_config = get_repository_config(find_repository_config_file())
6362

6463
if list_available:
6564
list_checks(repository_config=repository_config)

infrahub_sdk/ctl/cli_commands.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from .. import __version__ as sdk_version
2222
from ..async_typer import AsyncTyper
23-
from ..ctl import config
2423
from ..ctl.branch import app as branch_app
2524
from ..ctl.check import run as run_check
2625
from ..ctl.client import initialize_client, initialize_client_sync
@@ -30,7 +29,7 @@
3029
from ..ctl.object import app as object_app
3130
from ..ctl.render import list_jinja2_transforms, print_template_errors
3231
from ..ctl.repository import app as repository_app
33-
from ..ctl.repository import get_repository_config
32+
from ..ctl.repository import find_repository_config_file, get_repository_config
3433
from ..ctl.schema import app as schema_app
3534
from ..ctl.transform import list_transforms
3635
from ..ctl.utils import (
@@ -260,7 +259,7 @@ async def render(
260259
"""Render a local Jinja2 Transform for debugging purpose."""
261260

262261
variables_dict = parse_cli_vars(variables)
263-
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
262+
repository_config = get_repository_config(find_repository_config_file())
264263

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

@@ -310,7 +309,7 @@ def transform(
310309
"""Render a local transform (TransformPython) for debugging purpose."""
311310

312311
variables_dict = parse_cli_vars(variables)
313-
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
312+
repository_config = get_repository_config(find_repository_config_file())
314313

315314
if list_available or not transform_name:
316315
list_transforms(config=repository_config)

infrahub_sdk/ctl/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
DEFAULT_CONFIG_FILE = "infrahubctl.toml"
1313
ENVVAR_CONFIG_FILE = "INFRAHUBCTL_CONFIG"
1414
INFRAHUB_REPO_CONFIG_FILE = ".infrahub.yml"
15+
INFRAHUB_REPO_CONFIG_FILE_ALT = ".infrahub.yaml"
1516

1617

1718
class Settings(BaseSettings):

infrahub_sdk/ctl/generator.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
import typer
77
from rich.console import Console
88

9-
from ..ctl import config
109
from ..ctl.client import initialize_client
11-
from ..ctl.repository import get_repository_config
10+
from ..ctl.repository import find_repository_config_file, get_repository_config
1211
from ..ctl.utils import execute_graphql_query, init_logging, parse_cli_vars
1312
from ..exceptions import ModuleImportError
1413
from ..node import InfrahubNode
@@ -26,7 +25,7 @@ async def run(
2625
variables: Optional[list[str]] = None,
2726
) -> None:
2827
init_logging(debug=debug)
29-
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
28+
repository_config = get_repository_config(find_repository_config_file())
3029

3130
if list_available or not generator_name:
3231
list_generators(repository_config=repository_config)

infrahub_sdk/ctl/repository.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,49 @@
2424
console = Console()
2525

2626

27+
def find_repository_config_file(base_path: Path | None = None) -> Path:
28+
"""Find the repository config file, checking for both .yml and .yaml extensions.
29+
30+
Args:
31+
base_path: Base directory to search in. If None, uses current directory.
32+
33+
Returns:
34+
Path to the config file.
35+
36+
Raises:
37+
FileNotFoundError: If neither .infrahub.yml nor .infrahub.yaml exists.
38+
"""
39+
if base_path is None:
40+
base_path = Path()
41+
42+
yml_path = base_path / ".infrahub.yml"
43+
yaml_path = base_path / ".infrahub.yaml"
44+
45+
# Prefer .yml if both exist
46+
if yml_path.exists():
47+
return yml_path
48+
if yaml_path.exists():
49+
return yaml_path
50+
# For backward compatibility, return .yml path for error messages
51+
return yml_path
52+
53+
2754
def get_repository_config(repo_config_file: Path) -> InfrahubRepositoryConfig:
55+
# If the file doesn't exist, try to find it with alternate extension
56+
if not repo_config_file.exists():
57+
if repo_config_file.name == ".infrahub.yml":
58+
alt_path = repo_config_file.parent / ".infrahub.yaml"
59+
if alt_path.exists():
60+
repo_config_file = alt_path
61+
elif repo_config_file.name == ".infrahub.yaml":
62+
alt_path = repo_config_file.parent / ".infrahub.yml"
63+
if alt_path.exists():
64+
repo_config_file = alt_path
65+
2866
try:
2967
config_file_data = load_repository_config_file(repo_config_file)
3068
except FileNotFoundError as exc:
31-
console.print(f"[red]File not found {exc}")
69+
console.print(f"[red]File not found {exc} (also checked for .infrahub.yml and .infrahub.yaml)")
3270
raise typer.Exit(1) from exc
3371
except FileNotValidError as exc:
3472
console.print(f"[red]{exc.message}")

infrahub_sdk/pytest_plugin/plugin.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .. import InfrahubClientSync
1010
from ..utils import is_valid_url
1111
from .loader import InfrahubYamlFile
12-
from .utils import load_repository_config
12+
from .utils import find_repository_config_file, load_repository_config
1313

1414

1515
def pytest_addoption(parser: Parser) -> None:
@@ -18,9 +18,9 @@ def pytest_addoption(parser: Parser) -> None:
1818
"--infrahub-repo-config",
1919
action="store",
2020
dest="infrahub_repo_config",
21-
default=".infrahub.yml",
21+
default=None,
2222
metavar="INFRAHUB_REPO_CONFIG_FILE",
23-
help="Infrahub configuration file for the repository (default: %(default)s)",
23+
help="Infrahub configuration file for the repository (.infrahub.yml or .infrahub.yaml)",
2424
)
2525
group.addoption(
2626
"--infrahub-address",
@@ -63,7 +63,10 @@ def pytest_addoption(parser: Parser) -> None:
6363

6464

6565
def pytest_sessionstart(session: Session) -> None:
66-
session.infrahub_config_path = Path(session.config.option.infrahub_repo_config) # type: ignore[attr-defined]
66+
if session.config.option.infrahub_repo_config:
67+
session.infrahub_config_path = Path(session.config.option.infrahub_repo_config) # type: ignore[attr-defined]
68+
else:
69+
session.infrahub_config_path = find_repository_config_file() # type: ignore[attr-defined]
6770

6871
if session.infrahub_config_path.is_file(): # type: ignore[attr-defined]
6972
session.infrahub_repo_config = load_repository_config(repo_config_file=session.infrahub_config_path) # type: ignore[attr-defined]

infrahub_sdk/pytest_plugin/utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24

35
import yaml
@@ -6,7 +8,45 @@
68
from .exceptions import FileNotValidError
79

810

11+
def find_repository_config_file(base_path: Path | None = None) -> Path:
12+
"""Find the repository config file, checking for both .yml and .yaml extensions.
13+
14+
Args:
15+
base_path: Base directory to search in. If None, uses current directory.
16+
17+
Returns:
18+
Path to the config file.
19+
20+
Raises:
21+
FileNotFoundError: If neither .infrahub.yml nor .infrahub.yaml exists.
22+
"""
23+
if base_path is None:
24+
base_path = Path()
25+
26+
yml_path = base_path / ".infrahub.yml"
27+
yaml_path = base_path / ".infrahub.yaml"
28+
29+
# Prefer .yml if both exist
30+
if yml_path.exists():
31+
return yml_path
32+
if yaml_path.exists():
33+
return yaml_path
34+
# For backward compatibility, return .yml path for error messages
35+
return yml_path
36+
37+
938
def load_repository_config(repo_config_file: Path) -> InfrahubRepositoryConfig:
39+
# If the file doesn't exist, try to find it with alternate extension
40+
if not repo_config_file.exists():
41+
if repo_config_file.name == ".infrahub.yml":
42+
alt_path = repo_config_file.parent / ".infrahub.yaml"
43+
if alt_path.exists():
44+
repo_config_file = alt_path
45+
elif repo_config_file.name == ".infrahub.yaml":
46+
alt_path = repo_config_file.parent / ".infrahub.yml"
47+
if alt_path.exists():
48+
repo_config_file = alt_path
49+
1050
if not repo_config_file.is_file():
1151
raise FileNotFoundError(repo_config_file)
1252

0 commit comments

Comments
 (0)