Skip to content

Instantly share code, notes, and snippets.

@lirsacc
Created November 25, 2015 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lirsacc/1906f19c69fee881efea to your computer and use it in GitHub Desktop.
Save lirsacc/1906f19c69fee881efea to your computer and use it in GitHub Desktop.
ZSH database connection shortcuts
#compdef db
local -a dbs
dbs=$(ls $HOME/.dbs)
_alternative "files:dbs:($dbs)"
database=db_name
username=username
host=127.0.0.1
password=pass
port=3306
# ZSH function to quickly connect to known databases (with completion)
#
# `db connection.file [d] [arguments]`
#
# Store connection files in $HOME/.dbs (see database.credential for an example)
# By default uses the `mycli` client (http://mycli.net/), add `d` at the end to use
# standard `mysql`. All arguments are forwarded.
function db () {
if [ ! -f $HOME/.dbs/$1 ]; then
available_databases=$(ls $HOME/.dbs)
echo "No available db $1, choose from:"
echo $available_databases
return 1
fi
. $HOME/.dbs/$1
shift
[[ $1 == "d" ]] && cmd="mysql" && shift || cmd="mycli"
$cmd -h $host -P $port -u $username --password=$password $@ $database
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment