blob: e967e3b07c9b84dcc25bf9581a5da91c8f3967ee (
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
51
52
53
54
55
56
57
58
|
# 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
# password=SsJ2pICe226jM
#
# [mysql]
# database=bar
#
mysql() {
local config
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() {
# Only makes sense for first argument
((COMP_CWORD == 1)) || return 1
# Bail if directory doesn't exist
local dirname
dirname=$HOME/.mysql
[[ -d $dirname ]] || return 1
# Return the names of the .cnf files sans prefix as completions
local db
while IFS= read -d '' -r db ; do
COMPREPLY[${#COMPREPLY[@]}]=$db
done < <(
# Set options so that globs expand correctly
shopt -s dotglob nullglob
# Collect all the config file names, strip off leading path and .cnf
local -a cnfs
cnfs=("$dirname"/"${COMP_WORDS[COMP_CWORD]}"*.cnf)
cnfs=("${cnfs[@]#$dirname/}")
cnfs=("${cnfs[@]%.cnf}")
# Bail if no files to prevent empty output
((${#cnfs[@]})) || exit 1
# Print the conf names, null-delimited
printf '%q\0' "${cnfs[@]}"
)
}
complete -F _mysql -o default mysql
|