Skip to content

Commit f9be10d

Browse files
committed
Support infrahub.yml or infrahub.yaml configuration files
- Add find_repository_config_file() function to search for both .yml and .yaml extensions - Update all modules to use dynamic config file discovery - Prefer .yml over .yaml when both exist for backward compatibility - Update pytest plugin to support both extensions - Improve error messages to mention both possible file extensions
1 parent 760cf3a commit f9be10d

File tree

7 files changed

+93
-13
lines changed

7 files changed

+93
-13
lines changed

infrahub_sdk/ctl/check.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ..ctl import config
1515
from ..ctl.client import initialize_client
1616
from ..ctl.exceptions import QueryNotFoundError
17-
from ..ctl.repository import get_repository_config
17+
from ..ctl.repository import find_repository_config_file, get_repository_config
1818
from ..ctl.utils import catch_exception, execute_graphql_query
1919
from ..exceptions import ModuleImportError
2020

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

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

6464
if list_available:
6565
list_checks(repository_config=repository_config)

infrahub_sdk/ctl/cli_commands.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from ..ctl.object import app as object_app
3131
from ..ctl.render import list_jinja2_transforms, print_template_errors
3232
from ..ctl.repository import app as repository_app
33-
from ..ctl.repository import get_repository_config
33+
from ..ctl.repository import find_repository_config_file, get_repository_config
3434
from ..ctl.schema import app as schema_app
3535
from ..ctl.transform import list_transforms
3636
from ..ctl.utils import (
@@ -260,7 +260,7 @@ async def render(
260260
"""Render a local Jinja2 Transform for debugging purpose."""
261261

262262
variables_dict = parse_cli_vars(variables)
263-
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
263+
repository_config = get_repository_config(find_repository_config_file())
264264

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

@@ -310,7 +310,7 @@ def transform(
310310
"""Render a local transform (TransformPython) for debugging purpose."""
311311

312312
variables_dict = parse_cli_vars(variables)
313-
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
313+
repository_config = get_repository_config(find_repository_config_file())
314314

315315
if list_available or not transform_name:
316316
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ..ctl import config
1010
from ..ctl.client import initialize_client
11-
from ..ctl.repository import get_repository_config
11+
from ..ctl.repository import find_repository_config_file, get_repository_config
1212
from ..ctl.utils import execute_graphql_query, init_logging, parse_cli_vars
1313
from ..exceptions import ModuleImportError
1414
from ..node import InfrahubNode
@@ -26,7 +26,7 @@ async def run(
2626
variables: Optional[list[str]] = None,
2727
) -> None:
2828
init_logging(debug=debug)
29-
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))
29+
repository_config = get_repository_config(find_repository_config_file())
3030

3131
if list_available or not generator_name:
3232
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,45 @@
66
from .exceptions import FileNotValidError
77

88

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

0 commit comments

Comments
 (0)