Skip to content

Instantly share code, notes, and snippets.

@Eun
Last active July 6, 2021 16:56
Show Gist options
  • Select an option

  • Save Eun/f68a0d41880fd14f972973d309951cfa to your computer and use it in GitHub Desktop.

Select an option

Save Eun/f68a0d41880fd14f972973d309951cfa to your computer and use it in GitHub Desktop.

Revisions

  1. Eun revised this gist Jul 6, 2021. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions avn-pg
    Original 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}" ]; then

    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}" ]; then
    if [ -z "${CONNECTIONS+x}" ] || [ "$CONNECTIONS" == "null" ]; then
    echo "unable to get connections for $SERVICE"
    exit 1
    fi
  2. Eun revised this gist Jul 6, 2021. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions avn-pg
    Original file line number Diff line number Diff line change
    @@ -11,9 +11,17 @@ set -euo pipefail
    AVN=avn
    FZF=fzf
    JQ=jq
    PGCLIENT="docker run --rm -ti --net host --env PGDATABASE --name pgcli$$ dbcliorg/pgcli:latest"
    # 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"
    $PGCLIENT
    eval $PGCLIENT
  3. Eun revised this gist Jul 6, 2021. 1 changed file with 41 additions and 23 deletions.
    64 changes: 41 additions & 23 deletions avn-pg
    Original 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"
    ######################################################

    echo "Fetching projects..."

    PROJECT=$( \
    $AVN project list --json | \
    $JQ -r '.[] | .project_name' | \
    $FZF --prompt "project> "
    )
    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" ]; then
    if [ -z "${PROJECT+x}" ]; then
    exit 1
    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> "
    )
    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" ]; then
    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" ]; then
    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" ]; then
    if [ -z "${CONNECTIONS+x}" ]; then
    echo "unable to get connections for $SERVICE"
    exit 1
    fi

    CONNECTION=$(echo "$CONNECTIONS" | $FZF --prompt "database-server> ")
    if [ -z "$CONNECTION" ]; then
    if [ "$#" -lt 3 ]; then
    CONNECTION=$(echo "$CONNECTIONS" | $FZF --prompt "database-server> ")
    else
    CONNECTION=$3
    fi

    if [ -z "${CONNECTION+x}" ]; then
    exit 1
    fi

    DATABASE=$(echo -ne "$DATA" | $JQ -r \
    if [ "$#" -lt 4 ]; then
    DATABASE=$(echo -ne "$DATA" | $JQ -r \
    ".[] | select(.service_name == \"$SERVICE\") | .databases[]" |
    $FZF --prompt "database> "
    )
    if [ -z "$DATABASE" ]; then
    $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 $CONNECTION ($DATABASE)"
    echo "Connecting to $PROJECT $SERVICE $CONNECTION $DATABASE"
    $PGCLIENT
  4. Eun revised this gist Jun 22, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion avn-pg
    Original 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 "db> ")
    CONNECTION=$(echo "$CONNECTIONS" | $FZF --prompt "database-server> ")
    if [ -z "$CONNECTION" ]; then
    exit 1
    fi
  5. Eun revised this gist Jun 22, 2021. 1 changed file with 17 additions and 7 deletions.
    24 changes: 17 additions & 7 deletions avn-pg
    Original 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=$( \
    $AVN service list --project=$PROJECT --json | \
    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

    echo "Fetching connections for $SERVICE..."

    RAW_CONNECTIONS=$($AVN service list --project=$PROJECT --json | $JQ -r \
    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

    echo "Connecting to $CONNECTION"
    export PGDATABASE=$(echo -ne "$RAW_CONNECTIONS" | $JQ -r ".$CONNECTION")
    $PGCLIENT
    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
  6. Eun revised this gist Apr 27, 2021. 1 changed file with 11 additions and 9 deletions.
    20 changes: 11 additions & 9 deletions avn-pg
    Original 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 fzf (https://github.com/junegunn/fzf)
    ## * installed jq (https://github.com/stedolan/jq)
    ## * docker
    ######################################################
    ### CHANGE BELOW IF NECESSARY
    ## 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' | \
    $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' | \
    $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 \
    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"
    $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 | .[]')
    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
    export PGDATABASE=$(echo -ne "$RAW_CONNECTIONS" | $JQ -r ".$CONNECTION")
    $PGCLIENT
  7. Eun created this gist Apr 26, 2021.
    65 changes: 65 additions & 0 deletions avn-pg
    Original 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