Skip to content

Instantly share code, notes, and snippets.

@jasonmp85
Last active March 15, 2019 23:17
Show Gist options
  • Save jasonmp85/218f3bba809ec086343a to your computer and use it in GitHub Desktop.
Save jasonmp85/218f3bba809ec086343a to your computer and use it in GitHub Desktop.
chruby for PostgreSQL
require "formula"
class Chpg < Formula
desc "PostgreSQL environment tool"
homepage "https://gist.github.com/jasonmp85/218f3bba809ec086343a"
url "https://gist.githubusercontent.com/jasonmp85/218f3bba809ec086343a/raw/db7e002ba593512b3a9ae27eb52762d104dae416/chpg.sh"
sha256 "4a3d5165364e3f8289613dfe37d8e2a7615116b8a09c6a940c3207fe30e30d76"
version "0.1.1"
depends_on "petere/postgresql/postgresql-common"
def install
share.install "chpg.sh"
end
def caveats; <<~EOS
Add the following to your ~/.bashrc or ~/.zshrc file:
source #{opt_share}/chpg.sh
EOS
end
end
CHPG_VERSION="0.1.1"
function chpg_reset()
{
[[ -z "$PGCLUSTER" ]] && return
unset PGCLUSTER PGHOST PGPORT
}
function chpg_use()
{
[[ -n "$PGCLUSTER" ]] && chpg_reset
pghost=`pg_conftool $1 $2 show -s unix_socket_directories | tr -d "'"`
pgport=`pg_conftool $1 $2 show -s port`
export PGCLUSTER="$1/$2"
export PGHOST="${pghost}"
export PGPORT="${pgport}"
}
function chpg()
{
CLUSTERS=(`pg_lsclusters -h | awk '{print $1"/"$2}'`)
case "$1" in
-h|--help)
echo "usage: chpg [cluster-version cluster-name | system]"
;;
-V|--version)
echo "chpg: $CHPG_VERSION"
;;
"")
local cluster star
for cluster in "${CLUSTERS[@]}"; do
if [[ "$cluster" == "$PGCLUSTER" ]]; then star="*"
else star=" "
fi
echo " $star $cluster"
done
;;
system) chpg_reset ;;
*)
local cluster match
for cluster in "${CLUSTERS[@]}"; do
case "${cluster}" in
"$1/$2") match="$cluster" && break ;;
esac
done
if [[ -z "$match" ]]; then
echo "chpg: unknown PostgreSQL cluster: $1/$2" >&2
return 1
fi
chpg_use $*
;;
esac
}
@jasonmp85
Copy link
Author

Shamelessly copied from chruby.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment