diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2013-09-18 15:49:34 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2013-09-18 15:49:34 +1200 |
commit | 510ae0e33157653e15efb4c1b4ff6e4c1567ed83 (patch) | |
tree | 9936d8cc04614f64426b88c88661596c8b84c299 | |
parent | Use consistent negative test syntax (diff) | |
download | dotfiles-510ae0e33157653e15efb4c1b4ff6e4c1567ed83.tar.gz dotfiles-510ae0e33157653e15efb4c1b4ff6e4c1567ed83.zip |
Add MySQL shortcuts and completion function
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | bash/bashrc.d/mysql.bash | 45 |
2 files changed, 46 insertions, 0 deletions
@@ -6,6 +6,7 @@ bash/bashrc.d/* !bash/bashrc.d/ed.bash !bash/bashrc.d/grep.bash !bash/bashrc.d/ls.bash +!bash/bashrc.d/mysql.bash !bash/bashrc.d/options.bash !bash/bashrc.d/prompt.bash !bash/bashrc.d/pushd.bash diff --git a/bash/bashrc.d/mysql.bash b/bash/bashrc.d/mysql.bash new file mode 100644 index 00000000..b9f46096 --- /dev/null +++ b/bash/bashrc.d/mysql.bash @@ -0,0 +1,45 @@ +# If a file ~/.mysql/$1.cnf exists, call mysql(1) using that file. Otherwise +# just run MySQL with given args. Use restrictive permissions on these files. +# Examples: +# +# [client] +# host=dbhost.example.com +# user=foo +# database=bar +# password=SsJ2pICe226jM +# +mysql() { + local database=$1 + local config="$HOME"/.mysql/"$database".cnf + if [[ -f $config ]]; then + shift + command mysql --defaults-extra-file="$config" "$@" + else + command mysql "$@" + fi +} + +# Completion setup for MySQL for configured databases +_mysql() { + + # Check directory exists and has at least one .cnf file + local dir="$HOME"/.mysql + if [[ ! -d $dir ]] || ( + declare -a files + shopt -s nullglob dotglob + files=("$dir"/*.cnf) + ((! ${#files[@]})) + ); then + return 1 + fi + + # Return the names of the .cnf files sans prefix as completions + local word=${COMP_WORDS[COMP_CWORD]} + local -a items + items=("$dir"/*.cnf) + items=("${items[@]##*/}") + items=("${items[@]%%.cnf}") + COMPREPLY=( $(compgen -W "${items[*]}" -- "$word") ) +} +complete -F _mysql mysql + |