diff options
| author | Stefan Kreutz <mail@skreutz.com> | 2026-04-16 01:07:28 +0200 |
|---|---|---|
| committer | Stefan Kreutz <mail@skreutz.com> | 2026-04-16 01:07:28 +0200 |
| commit | e9bd990587227b29dc6e41e3d43a5cf2ec9dcd5b (patch) | |
| tree | d9524ecacf291f802f28bc14b7ed058344e70773 /temp-postgres.sh | |
| parent | b1735044d222e7173447d6b72d6bc90085994d32 (diff) | |
| download | temp-postgres-e9bd990587227b29dc6e41e3d43a5cf2ec9dcd5b.tar.gz | |
Add wrapped command option
Diffstat (limited to 'temp-postgres.sh')
| -rwxr-xr-x | temp-postgres.sh | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/temp-postgres.sh b/temp-postgres.sh index 6064b66..8504257 100755 --- a/temp-postgres.sh +++ b/temp-postgres.sh @@ -20,8 +20,7 @@ set -o nounset trap 'quit' INT quit() { code="${1:-0}" - trap '' INT TERM - kill -TERM 0 + [ -z "$postgres_pid" ] || kill -TERM "$postgres_pid" wait rm -rf "${tmpdir-}" || { ( >&2 printf "temp-postgres: failed to remove temporary directory: %s\\n" "${tmpdir}" ) @@ -36,7 +35,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>]" + echo "Usage: temp-postgres [-h|--help] [-d|--dbname <dbname>] [-u|--username <username>] [--] [<command>]" exit ;; -d|--dbname) @@ -60,10 +59,6 @@ while [ $# -gt 0 ] ; do ;; esac done -if [ $# -gt 0 ] ; then - ( >&2 echo "too many arguments" ) - exit 2 -fi # Create a temporary directory tmpdir="$( mktemp -d )" || { @@ -79,6 +74,7 @@ initdb --pgdata="${tmpdir}" --username="${username}" || { # Serve the directory ( postgres -k "${tmpdir}" -D "${tmpdir}" </dev/null ) & +postgres_pid=$! # Test the connection sleep 1 @@ -96,8 +92,23 @@ createdb --host="${tmpdir}" --username="${username}" --no-password "${dbname}" | printf ' Connect with the following command: -\tpsql --host=localhost "%s" "%s" +\tpsql --host "%s" --dbname "%s" --username "%s" + +' "$tmpdir" "$dbname" "$username" -' "${dbname}" "${username}" +if [ $# -eq 0 ] ; then + wait +else + export PGHOST="$tmpdir" + export PGDATABASE="$dbname" + export PGUSER="$username" + export DATABASE_URI="postgresql:${dbname}?host=${tmpdir}&user=${username}" + export DATABASE_URL="$DATABASE_URI" -wait + "$@" || { + code=$? + ( >&2 echo "temp-postgres: command exited ${code}" ) + quit "$code" + } + quit +fi |