Created
September 17, 2020 16:21
-
-
Save AlexVanderbist/29af47f71f8273e4c91507b6c36e1446 to your computer and use it in GitHub Desktop.
`opendb` command - opens the database for a Laravel app in your GUI
This file contains 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
opendb () { | |
[ ! -f .env ] && { echo "No .env file found."; exit 1; } | |
DB_CONNECTION=$(grep DB_CONNECTION .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_HOST=$(grep DB_HOST .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_PORT=$(grep DB_PORT .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_DATABASE=$(grep DB_DATABASE .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_USERNAME=$(grep DB_USERNAME .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_PASSWORD=$(grep DB_PASSWORD .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_URL="${DB_CONNECTION}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}" | |
echo "Opening ${DB_URL}" | |
open $DB_URL | |
} |
Double quote "$DB_URL" to prevent globbing and word splitting. 😉
I was trying to come up with a similar function for launching the given (.env) postgres db in pgAdmin4 but couldn't get anywhere with it.
Has anyone tried that?
The script doesn’t work if the strings in your .env
file are wrapped with quotes, but my grep
and cut
skills aren’t good enough to improve on that.
Nice trick though, didn’t know about mysql://
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks bro you are a genius <3