Last active
July 6, 2021 16:56
-
-
Save Eun/f68a0d41880fd14f972973d309951cfa to your computer and use it in GitHub Desktop.
Revisions
-
Eun revised this gist
Jul 6, 2021 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -60,14 +60,15 @@ RAW_CONNECTIONS=$(echo -ne "$DATA" | $JQ -r \ $JQ -s "add" ) if [ -z "${RAW_CONNECTIONS+x}" ] || [ "$RAW_CONNECTIONS" == "null" ]; then echo "unable to get connections for $SERVICE" exit 1 fi CONNECTIONS=$(echo -ne "$RAW_CONNECTIONS" | $JQ -r 'keys | .[]') if [ -z "${CONNECTIONS+x}" ] || [ "$CONNECTIONS" == "null" ]; then echo "unable to get connections for $SERVICE" exit 1 fi -
Eun revised this gist
Jul 6, 2021 . 1 changed file with 10 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,9 +11,17 @@ set -euo pipefail AVN=avn FZF=fzf JQ=jq # default PGCLIENT to use, if the environment variable PGCLIENT is set, avn-pg will use its contents # to connect to the database # example: $ PGCLIENT="psql \$PGDATABASE" avn-pg DEFAULT_PGCLIENT="docker run --rm -ti --net host --env PGDATABASE --name pgcli$$ dbcliorg/pgcli:latest" ###################################################### if [ -z "${PGCLIENT+x}" ]; then PGCLIENT=$DEFAULT_PGCLIENT fi if [ "$#" -lt 1 ]; then echo "Fetching projects..." PROJECT=$( \ @@ -92,4 +100,4 @@ PGDATABASE=$(echo -ne "$RAW_CONNECTIONS" | $JQ -r ".$CONNECTION") export PGDATABASE=$(echo -ne "$PGDATABASE" | $JQ -R -r "sub(\"(?<conn>postgres://.*/)(?<db>[_\\\-a-zA-Z0-9]+)(?<params>\\\??.+)\"; \"\(.conn)$DATABASE\(.params)\")") echo "Connecting to $PROJECT $SERVICE $CONNECTION $DATABASE" eval $PGCLIENT -
Eun revised this gist
Jul 6, 2021 . 1 changed file with 41 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,28 +14,36 @@ JQ=jq PGCLIENT="docker run --rm -ti --net host --env PGDATABASE --name pgcli$$ dbcliorg/pgcli:latest" ###################################################### if [ "$#" -lt 1 ]; then echo "Fetching projects..." PROJECT=$( \ $AVN project list --json | \ $JQ -r '.[] | .project_name' | \ $FZF --prompt "project> " ) else PROJECT=$1 fi if [ -z "${PROJECT+x}" ]; then exit 1 fi echo "Fetching services for $PROJECT..." DATA=$($AVN service list --project=$PROJECT --json) if [ "$#" -lt 2 ]; then SERVICE=$( \ echo -ne "$DATA" | \ $JQ -r '.[] | select(.state == "RUNNING") | select(.service_type == "pg") | select(.connection_info.pg | length > 0) | .service_name' | \ $FZF --prompt "service> " ) else SERVICE=$2 fi if [ -z "${SERVICE+x}" ]; then exit 1 fi @@ -44,34 +52,44 @@ RAW_CONNECTIONS=$(echo -ne "$DATA" | $JQ -r \ $JQ -s "add" ) if [ -z "${RAW_CONNECTIONS+x}" ]; then echo "unable to get connections for $SERVICE" exit 1 fi CONNECTIONS=$(echo -ne "$RAW_CONNECTIONS" | $JQ -r 'keys | .[]') if [ -z "${CONNECTIONS+x}" ]; then echo "unable to get connections for $SERVICE" exit 1 fi if [ "$#" -lt 3 ]; then CONNECTION=$(echo "$CONNECTIONS" | $FZF --prompt "database-server> ") else CONNECTION=$3 fi if [ -z "${CONNECTION+x}" ]; then exit 1 fi if [ "$#" -lt 4 ]; then DATABASE=$(echo -ne "$DATA" | $JQ -r \ ".[] | select(.service_name == \"$SERVICE\") | .databases[]" | $FZF --prompt "database> " ) else DATABASE=$4 fi if [ -z "${DATABASE+x}" ]; then exit 1 fi PGDATABASE=$(echo -ne "$RAW_CONNECTIONS" | $JQ -r ".$CONNECTION") export PGDATABASE=$(echo -ne "$PGDATABASE" | $JQ -R -r "sub(\"(?<conn>postgres://.*/)(?<db>[_\\\-a-zA-Z0-9]+)(?<params>\\\??.+)\"; \"\(.conn)$DATABASE\(.params)\")") echo "Connecting to $PROJECT $SERVICE $CONNECTION $DATABASE" $PGCLIENT -
Eun revised this gist
Jun 22, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -56,7 +56,7 @@ if [ -z "$CONNECTIONS" ]; then exit 1 fi CONNECTION=$(echo "$CONNECTIONS" | $FZF --prompt "database-server> ") if [ -z "$CONNECTION" ]; then exit 1 fi -
Eun revised this gist
Jun 22, 2021 . 1 changed file with 17 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,9 +27,10 @@ if [ -z "$PROJECT" ]; then fi echo "Fetching services for $PROJECT..." DATA=$($AVN service list --project=$PROJECT --json) SERVICE=$( \ echo -ne "$DATA" | \ $JQ -r '.[] | select(.state == "RUNNING") | select(.service_type == "pg") | select(.connection_info.pg | length > 0) | .service_name' | \ $FZF --prompt "service> " ) @@ -38,9 +39,7 @@ if [ -z "$SERVICE" ]; then exit 1 fi RAW_CONNECTIONS=$(echo -ne "$DATA" | $JQ -r \ ".[] | select(.service_name == \"$SERVICE\") | .connection_info | (.pg | range(0;length) as \$i | {(\"master\" + (\$i | tostring)):.[\$i]}),{\"replica0\":.pg_replica_uri}" | \ $JQ -s "add" ) @@ -62,6 +61,17 @@ if [ -z "$CONNECTION" ]; then exit 1 fi DATABASE=$(echo -ne "$DATA" | $JQ -r \ ".[] | select(.service_name == \"$SERVICE\") | .databases[]" | $FZF --prompt "database> " ) if [ -z "$DATABASE" ]; then exit 1 fi PGDATABASE=$(echo -ne "$RAW_CONNECTIONS" | $JQ -r ".$CONNECTION") export PGDATABASE=$(echo -ne "$PGDATABASE" | $JQ -R -r "sub(\"(?<conn>postgres://.*/)(?<db>[_\\\-a-zA-Z0-9]+)(?<params>\\\??.+)\"; \"\(.conn)$DATABASE\(.params)\")") echo "Connecting to $CONNECTION ($DATABASE)" $PGCLIENT -
Eun revised this gist
Apr 27, 2021 . 1 changed file with 11 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,20 +3,22 @@ set -euo pipefail ###################################################### ## REQUIREMENTS ## * proper configured and running avn cli: (https://github.com/aiven/aiven-client) ## * installed fzf (https://github.com/junegunn/fzf) ## * installed jq (https://github.com/stedolan/jq) ## * docker ###################################################### ## CHANGE BELOW IF NECESSARY AVN=avn FZF=fzf JQ=jq PGCLIENT="docker run --rm -ti --net host --env PGDATABASE --name pgcli$$ dbcliorg/pgcli:latest" ###################################################### echo "Fetching projects..." PROJECT=$( \ $AVN project list --json | \ $JQ -r '.[] | .project_name' | \ $FZF --prompt "project> " ) @@ -28,7 +30,7 @@ echo "Fetching services for $PROJECT..." SERVICE=$( \ $AVN service list --project=$PROJECT --json | \ $JQ -r '.[] | select(.state == "RUNNING") | select(.service_type == "pg") | select(.connection_info.pg | length > 0) | .service_name' | \ $FZF --prompt "service> " ) @@ -38,17 +40,17 @@ fi echo "Fetching connections for $SERVICE..." RAW_CONNECTIONS=$($AVN service list --project=$PROJECT --json | $JQ -r \ ".[] | select(.service_name == \"$SERVICE\") | .connection_info | (.pg | range(0;length) as \$i | {(\"master\" + (\$i | tostring)):.[\$i]}),{\"replica0\":.pg_replica_uri}" | \ $JQ -s "add" ) if [ -z "$RAW_CONNECTIONS" ]; then echo "unable to get connections for $SERVICE" exit 1 fi CONNECTIONS=$(echo -ne "$RAW_CONNECTIONS" | $JQ -r 'keys | .[]') if [ -z "$CONNECTIONS" ]; then echo "unable to get connections for $SERVICE" @@ -61,5 +63,5 @@ if [ -z "$CONNECTION" ]; then fi echo "Connecting to $CONNECTION" export PGDATABASE=$(echo -ne "$RAW_CONNECTIONS" | $JQ -r ".$CONNECTION") $PGCLIENT -
Eun created this gist
Apr 26, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ #!/bin/bash set -euo pipefail ###################################################### ## REQUIREMENTS ## * proper configured and running avn cli: (https://github.com/aiven/aiven-client) ## * installed fzf https://github.com/junegunn/fzf ## * docker ###################################################### ### CHANGE BELOW IF NECESSARY AVN=avn FZF=fzf PGCLIENT="docker run --rm -ti --net host --env PGDATABASE --name pgcli$$ dbcliorg/pgcli:latest" ###################################################### echo "Fetching projects..." PROJECT=$( \ $AVN project list --json | \ jq -r '.[] | .project_name' | \ $FZF --prompt "project> " ) if [ -z "$PROJECT" ]; then exit 1 fi echo "Fetching services for $PROJECT..." SERVICE=$( \ $AVN service list --project=$PROJECT --json | \ jq -r '.[] | select(.state == "RUNNING") | select(.service_type == "pg") | select(.connection_info.pg | length > 0) | .service_name' | \ $FZF --prompt "service> " ) if [ -z "$SERVICE" ]; then exit 1 fi echo "Fetching connections for $SERVICE..." RAW_CONNECTIONS=$($AVN service list --project=$PROJECT --json | jq -r \ ".[] | select(.service_name == \"$SERVICE\") | .connection_info | (.pg | range(0;length) as \$i | {(\"master\" + (\$i | tostring)):.[\$i]}),{\"replica0\":.pg_replica_uri}" | \ jq -s "add" ) if [ -z "$RAW_CONNECTIONS" ]; then echo "unable to get connections for $SERVICE" exit 1 fi CONNECTIONS=$(echo -ne "$RAW_CONNECTIONS" | jq -r 'keys | .[]') if [ -z "$CONNECTIONS" ]; then echo "unable to get connections for $SERVICE" exit 1 fi CONNECTION=$(echo "$CONNECTIONS" | $FZF --prompt "db> ") if [ -z "$CONNECTION" ]; then exit 1 fi echo "Connecting to $CONNECTION" export PGDATABASE=$(echo -ne "$RAW_CONNECTIONS" | jq -r ".$CONNECTION") $PGCLIENT