diff options
| author | Stefan Kreutz <mail@skreutz.com> | 2026-04-16 01:33:34 +0200 |
|---|---|---|
| committer | Stefan Kreutz <mail@skreutz.com> | 2026-04-16 01:33:34 +0200 |
| commit | c3eb3d7264fd77ef88320251f910accd6df8164a (patch) | |
| tree | 634a5aede635443235d04997030b17d498867695 | |
| parent | 803fc1e534a525a242f7c42c303817320897f73b (diff) | |
| download | temp-postgres-c3eb3d7264fd77ef88320251f910accd6df8164a.tar.gz | |
Add --symlink option
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | README.md | 20 | ||||
| -rw-r--r-- | temp-postgres.1 | 20 | ||||
| -rwxr-xr-x | temp-postgres.sh | 17 |
4 files changed, 44 insertions, 14 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e06782a..c39b236 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep A Changelog][] and this project adheres to [Semanti ### Added * Added option to execute a given command when PostgreSQL server is ready +* Added `--symlink` option to enable static client configuration * Added minimal `--help` option * Added changelog * Added experimental Nix flake @@ -13,12 +13,6 @@ Create and serve a temporary database: temp-postgres ``` -Specify a database name, and PostgreSQL user name: - -```sh -temp-postgres --dbname myproject --username alex -``` - Wrap the `psql` command to connect to the temporary database once the server is ready: ```sh @@ -44,6 +38,20 @@ platform = { host = 'cfg(unix)' } run-wrapper = 'temp-postgres' ``` +Set up a symlink to enable static client configuration: + +```sh +temp-postgres --symlink ./db +psql --host "$(realpath ./db)" +``` + +Specify a database name, and PostgreSQL user name: + +```sh +temp-postgres --dbname myproject --username alex +``` + + See the manual page for details. ## Dependencies diff --git a/temp-postgres.1 b/temp-postgres.1 index 324a2f1..94f13cd 100644 --- a/temp-postgres.1 +++ b/temp-postgres.1 @@ -69,19 +69,25 @@ and the .Sh EXAMPLES Create and serve a temporary database: .Bd -literal -offset indent -$ temp-postgres -.Ed -.Pp -Specify a database name, and PostgreSQL user name: -.Bd -literal -offset indent -$ temp-postgres --dbname myproject --username alex +temp-postgres .Ed .Pp Wrap the .Xr psql 1 command to connect to the temporary database once the server is ready: .Bd -literal -offset indent -$ temp-postgres -- psql +temp-postgres -- psql +.Ed +.Pp +Set up a symlink to enable static client configuration: +.Bd -literal -offset indent +temp-postgres --symlink ./db +psql --host "$(realpath ./db)" +.Ed +.Pp +Specify a database name, and PostgreSQL user name: +.Bd -literal -offset indent +temp-postgres --dbname myproject --username alex .Ed .Sh SEE ALSO .Xr postgres 1 , diff --git a/temp-postgres.sh b/temp-postgres.sh index 8504257..96f90d2 100755 --- a/temp-postgres.sh +++ b/temp-postgres.sh @@ -26,6 +26,10 @@ quit() { ( >&2 printf "temp-postgres: failed to remove temporary directory: %s\\n" "${tmpdir}" ) [ "${code}" -ne 0 ] || code=1 } + [ -z "${symlink:-}" ] || ! [ -L "$symlink" ] || unlink "$symlink" || { + ( >&2 printf "temp-postgres: failed to unlink %s\\n" "$symlink" ) + [ "${code}" -ne 0 ] || code=1 + } exit "${code}" } @@ -35,7 +39,7 @@ username="$( id -un )" while [ $# -gt 0 ] ; do case "$1" in -h|--help) - echo "Usage: temp-postgres [-h|--help] [-d|--dbname <dbname>] [-u|--username <username>] [--] [<command>]" + echo "Usage: temp-postgres [-h|--help] [-d|--dbname <dbname>] [-u|--username <username>] [--symlink <path>] [--] [<command>]" exit ;; -d|--dbname) @@ -46,6 +50,10 @@ while [ $# -gt 0 ] ; do username="$2" shift 2 ;; + --symlink) + symlink="$2" + shift 2 + ;; --) shift break @@ -89,6 +97,13 @@ createdb --host="${tmpdir}" --username="${username}" --no-password "${dbname}" | quit 1 } +if [ -n "${symlink:-}" ] ; then + ln -sf "${tmpdir}" "$symlink" || { + ( >&2 printf "temp-postgres: failed to create symlink %s\\n" "$symlink" ) + quit 1 + } +fi + printf ' Connect with the following command: |