Skip to content

Commit

Permalink
New parameter to conf_path to allow the method to create the PG servi…
Browse files Browse the repository at this point in the history
…ce file at the obtained location if it does not yet exist
  • Loading branch information
gacarrillor committed Jan 9, 2025
1 parent 297c014 commit 403c87d
Showing 1 changed file with 12 additions and 1 deletion.
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

0 comments on commit 403c87d

Please sign in to comment.