diff --git a/src/update_tf_modules/clients/github_api.py b/src/update_tf_modules/clients/github_api.py index 8c0d5e3..e63a1c5 100644 --- a/src/update_tf_modules/clients/github_api.py +++ b/src/update_tf_modules/clients/github_api.py @@ -3,6 +3,8 @@ import requests from ..config import GITHUB_API +import logging +logger = logging.getLogger(__name__) def build_github_session() -> requests.Session: @@ -67,10 +69,10 @@ def get_latest_github_tag( raise ValueError(f"Unsupported GitHub lookup strategy: {lookup}") except requests.HTTPError as error: - print(f"[ERROR] Failed to fetch latest GitHub version for '{repo}': {error}") + logger.error(f"Failed to fetch latest GitHub version for '{repo}': {error}") return None except Exception as error: - print(f"[ERROR] Unexpected error fetching GitHub version for '{repo}': {error}") + logger.error(f"Unexpected error fetching GitHub version for '{repo}': {error}") return None def get_commit_hash_for_tag( @@ -99,12 +101,12 @@ def get_commit_hash_for_tag( return tag_response.json().get("object", {}).get("sha") return obj.get("sha") except requests.HTTPError as error: - print( - f"[ERROR] Failed to fetch commit hash for tag '{tag}' in repo '{repo}': {error}" + logger.error( + f"Failed to fetch commit hash for tag '{tag}' in repo '{repo}': {error}" ) return None except Exception as error: - print( - f"[ERROR] Unexpected error fetching commit hash for tag '{tag}' in repo '{repo}': {error}" + logger.error( + f"Unexpected error fetching commit hash for tag '{tag}' in repo '{repo}': {error}" ) return None diff --git a/src/update_tf_modules/clients/registry_api.py b/src/update_tf_modules/clients/registry_api.py index fba5afa..aec5ace 100644 --- a/src/update_tf_modules/clients/registry_api.py +++ b/src/update_tf_modules/clients/registry_api.py @@ -3,6 +3,8 @@ import requests from ..config import TERRAFORM_REGISTRY_API +import logging +logger = logging.getLogger(__name__) def build_registry_session() -> requests.Session: """Create an HTTP session configured for Terraform Registry requests. @@ -43,13 +45,13 @@ def get_latest_registry_version( return None return max(version_numbers, key=semver_key) except requests.HTTPError as error: - print( - f"[ERROR] Failed to fetch latest version for registry module '{source}': {error}" + logger.error( + f"Failed to fetch latest version for registry module '{source}': {error}" ) return None except Exception as error: - print( - f"[ERROR] Unexpected error fetching version for registry module '{source}': {error}" + logger.error( + f"Unexpected error fetching version for registry module '{source}': {error}" ) return None diff --git a/src/update_tf_modules/discovery.py b/src/update_tf_modules/discovery.py index bf3046e..0762d57 100644 --- a/src/update_tf_modules/discovery.py +++ b/src/update_tf_modules/discovery.py @@ -2,6 +2,8 @@ from .config import TERRAFORM_ROOT from .models import GitHubModule, Module +import logging +logger = logging.getLogger(__name__) def normalize_discovered_source(source: str) -> str: """Normalize discovered module sources for manifest key comparison. @@ -81,6 +83,6 @@ def warn_on_unmanaged_modules(modules: list[Module]) -> None: unmanaged = sorted(discovered - managed) if unmanaged: - print("[WARN] Terraform modules were found in the repo but are not represented in the manifest:") + logger.warning("[WARN] Terraform modules were found in the repo but are not represented in the manifest:") for source in unmanaged: - print(f" - {source}") + logger.warning(f" - {source}") diff --git a/src/update_tf_modules/updaters/github_source.py b/src/update_tf_modules/updaters/github_source.py index a0d8631..6b5e2b4 100644 --- a/src/update_tf_modules/updaters/github_source.py +++ b/src/update_tf_modules/updaters/github_source.py @@ -2,6 +2,8 @@ import re from ..config import ROOT +import logging +logger = logging.getLogger(__name__) def update_github_module( file_path: Path, @@ -32,6 +34,6 @@ def update_github_module( if count > 0 and new_content != content: file_path.write_text(new_content, encoding="utf-8") - print(f"Updated GitHub module in {file_path.relative_to(ROOT)} to {new_ref}") + logger.info(f"Updated GitHub module in {file_path.relative_to(ROOT)} to {new_ref}") return count \ No newline at end of file diff --git a/src/update_tf_modules/updaters/registry_source.py b/src/update_tf_modules/updaters/registry_source.py index c76106c..ed24df8 100644 --- a/src/update_tf_modules/updaters/registry_source.py +++ b/src/update_tf_modules/updaters/registry_source.py @@ -2,6 +2,8 @@ import re from ..config import ROOT +import logging +logger = logging.getLogger(__name__) def update_registry_module( file_path: Path, @@ -89,7 +91,7 @@ def update_registry_module( old_content = file_path.read_text(encoding="utf-8") if new_content != old_content: file_path.write_text(new_content, encoding="utf-8") - print( + logger.info( f"Updated registry module '{source}' in {file_path.relative_to(ROOT)} to {new_version}" ) diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 747e675..ec907c9 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -57,10 +57,9 @@ def test_managed_source_keys(): assert "git::https://github.com/org/repo.git?ref=" in result assert "registry.terraform.io/org/module/aws" in result -def test_warn_on_unmanaged_modules(monkeypatch: MonkeyPatch, capsys: pytest.CaptureFixture[str]): +def test_warn_on_unmanaged_modules(monkeypatch, caplog): monkeypatch.setattr(discovery, "discover_module_sources", lambda: {"source1", "source2"}) monkeypatch.setattr(discovery, "managed_source_keys", lambda _: {"source1"}) warn_on_unmanaged_modules([]) - captured = capsys.readouterr() - assert "[WARN] Terraform modules were found in the repo but are not represented in the manifest:" in captured.out - assert " - source2" in captured.out \ No newline at end of file + assert "Terraform modules were found in the repo but are not represented in the manifest:" in caplog.text + assert " - source2" in caplog.text \ No newline at end of file