Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow conf_path() to create the PG service file if it's missing #38

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
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
13 changes: 12 additions & 1 deletion pgserviceparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
from .exceptions import ServiceFileNotFound, ServiceNotFound


def conf_path() -> Path:
def conf_path(create_if_missing: bool = False) -> Path:
"""Returns the path found for the pg_service.conf on the system as string.

Args:
create_if_missing: Whether to create the file (and eventually its parent folders) if the file does not exist.

Returns:
path to the pg_service.conf file as string
"""
Expand All @@ -26,6 +29,14 @@ def conf_path() -> Path:
else: # Linux or Darwin (Mac)
pg_config_path = Path("~/.pg_service.conf").expanduser()

if create_if_missing and not pg_config_path.exists():
# Make sure all parent directories exist
if not pg_config_path.parent.exists():
pg_config_path.parent.mkdir(parents=True, exist_ok=True)

# Finally, make sure the file itself exists
Path.touch(pg_config_path)

return pg_config_path


Expand Down
Loading