-
-
Save arnested/a27bcaec237ba087315ededf9ab516f1 to your computer and use it in GitHub Desktop.
Open mycli with database from current docker compose project
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 characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| if ! command -v mycli >/dev/null 2>&1; then | |
| echo "mycli is not installed. Please install it to use this script." >&2 | |
| exit 127 | |
| fi | |
| if ! command -v docker >/dev/null 2>&1; then | |
| echo "docker is not installed. Please install it to use this script." >&2 | |
| exit 127 | |
| fi | |
| if ! command -v jq >/dev/null 2>&1; then | |
| echo "jq is not installed. Please install it to use this script." >&2 | |
| exit 127 | |
| fi | |
| function cleanup() { | |
| rv=$? | |
| if [[ -z "${PROJECT}" ]]; then | |
| echo "No Docker Compose project found in the current directory" >&2 | |
| exit 1 | |
| fi | |
| exit "${rv}" | |
| } | |
| trap cleanup EXIT | |
| PROJECT=$(docker compose config --format json 2>/dev/null | jq -r .name) | |
| CONTAINER_ID=$(docker container ls --filter expose=3306 --filter health=healthy --filter "label=com.docker.compose.project=${PROJECT}" --quiet --no-trunc) | |
| if [[ -z "${CONTAINER_ID}" ]]; then | |
| echo "No healthy MySQL container found for project '${PROJECT}'" >&2 | |
| exit 2 | |
| fi | |
| CONTAINER_INFO=$(docker container inspect "${CONTAINER_ID}") | |
| NAME=$(jq -r '.[0].Name' <<<"${CONTAINER_INFO}") | |
| NETWORK_MODE=$(jq -r '.[0].HostConfig.NetworkMode' <<<"${CONTAINER_INFO}") | |
| IP_ADDRESS=$(jq -r ".[0].NetworkSettings.Networks.${NETWORK_MODE}.IPAddress" <<<"${CONTAINER_INFO}") | |
| ENV=$(jq -r '.[0].Config.Env' <<<"${CONTAINER_INFO}") | |
| USER=$(echo "${ENV}" | grep 'MYSQL_USER=' | cut -d '=' -f 2 | cut -d '"' -f 1) | |
| PASSWORD=$(echo "${ENV}" | grep 'MYSQL_PASSWORD=' | cut -d '=' -f 2 | cut -d '"' -f 1) | |
| DATABASE=$(echo "${ENV}" | grep 'MYSQL_DATABASE=' | cut -d '=' -f 2 | cut -d '"' -f 1) | |
| mkdir -p "${HOME}/.mycli-history.d" | |
| export MYCLI_HISTFILE="${HOME}/.mycli-history.d/${NAME:1}" | |
| mycli --user "${USER}" --password "${PASSWORD}" --host "${IP_ADDRESS}" --prompt "${NAME#/}> " --database "${DATABASE}" "$@" |
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 characters
| --- .myclirc 2025-11-15 09:21:50.494350721 +0100 | |
| +++ .myclirc-recommended 2025-11-15 09:22:58.576972954 +0100 | |
| @@ -71,7 +71,7 @@ | |
| prompt_continuation = '->' | |
| # Skip intro info on startup and outro info on exit | |
| -less_chatty = False | |
| +less_chatty = True | |
| # Use alias from --login-path instead of host name in prompt | |
| login_path_as_host = False | |
| @@ -81,10 +81,10 @@ | |
| auto_vertical_output = False | |
| # keyword casing preference. Possible values "lower", "upper", "auto" | |
| -keyword_casing = auto | |
| +keyword_casing = upper | |
| # disabled pager on startup | |
| -enable_pager = True | |
| +enable_pager = False | |
| # Custom colors for the completion menu, toolbar, etc. | |
| [colors] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment