blob: d04b189d0f3cbd66befaec3cda26720f66e84d60 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# Bail if no mysql(1)
if ! hash mysql 2>/dev/null ; then
return
fi
# 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 config="$HOME"/.mysql/"$1".cnf
if [[ -r $config ]] ; then
shift
command mysql --defaults-extra-file="$config" "$@"
else
command mysql "$@"
fi
}
# Completion setup for MySQL for configured databases
_mysql() {
local word=${COMP_WORDS[COMP_CWORD]}
# 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
COMPREPLY=()
return 1
fi
# Return the names of the .cnf files sans prefix as completions
local -a items
items=("$dir"/*.cnf)
items=("${items[@]##*/}")
items=("${items[@]%%.cnf}")
COMPREPLY=( $(compgen -W "${items[*]}" -- "$word") )
}
complete -F _mysql -o default mysql
|