diff options
Diffstat (limited to 'bash/bashrc.d/mysql.bash')
-rw-r--r-- | bash/bashrc.d/mysql.bash | 45 |
1 files changed, 45 insertions, 0 deletions
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 + |