Debian Codegraph @

debian/clusterssh.bash-completion

raw
1 # -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
2 # ex: ts=8 sw=8 noet filetype=sh
3 #
4 # cssh(1) completion by Aaron Spettl <[email protected]>, adapted from the
5 # Debian GNU/Linux dput(1) completion by Roland Mas <[email protected]>
6 #
7 # On Debian (and Debian based distributions) drop this file into
8 # /etc/bash_completion.d
9 # and source the /etc/bash_completion script - or just restart bash.
10
11 _cssh ()
12 {
13 local cur prev options paroptions clusters extra_cluster_file_line clusters_line extra_cluster_file
14
15 COMPREPLY=()
16 cur=${COMP_WORDS[COMP_CWORD]}
17 prev=${COMP_WORDS[COMP_CWORD-1]}
18
19 # all options understood by cssh
20 options='-c --cluster-file -C --config-file --debug -e --evaluate \
21 -g --tile -G --no-tile -h --help -H --man -l --username \
22 -o --options -p --port -q --autoquit -Q --no-autoquit \
23 -s --show-history -t --term-args -T --title \
24 -u --output-config -v --version'
25
26 # find the extra cluster file line in the .clusterssh/config or, alternatively, /etc/csshrc
27 extra_cluster_file_line="`grep '^[[:space:]]*extra_cluster_file' $HOME/.clusterssh/config 2> /dev/null`"
28 [ -z "$extra_cluster_file_line" ] && extra_cluster_file_line="`grep '^[[:space:]]*extra_cluster_file' /etc/csshrc 2> /dev/null`"
29
30 # find the clusters line in the .csshrc or, alternatively, /etc/csshrc
31 clusters_line="`grep '^[[:space:]]*clusters' $HOME/.clusterssh/config 2> /dev/null`"
32 [ -z "$clusters_line" ] && clusters_line="`grep '^[[:space:]]*clusters' /etc/csshrc 2> /dev/null`"
33
34 # extract the location of the extra cluster file
35 extra_cluster_file="`echo $extra_cluster_file_line | cut -f 2- -d '='`"
36 [ -n "$extra_cluster_file" ] && extra_cluster_file="`eval echo $extra_cluster_file`"
37 # TODO: don't use eval to expand ~ and $HOME
38
39 # get the names of all defined clusters
40 clusters=$(
41 {
42 [ -n "$clusters_line" ] && echo "$clusters_line" | cut -f 2- -d '=' | tr "$IFS" "\n" || /bin/true
43 [ -n "$extra_cluster_file" ] && sed -e 's/^\([a-z0-9.-]\+\).*$/\1/i' "$extra_cluster_file" 2> /dev/null || /bin/true
44 sed -e 's/^\([a-z0-9.-]\+\).*$/\1/i' /etc/clusters 2> /dev/null || /bin/true
45 sed -e 's/^\([a-z0-9.-]\+\).*$/\1/i' $HOME/.clusterssh/clusters 2> /dev/null || /bin/true
46 } | sort -u)
47
48 # use options and clusters for tab completion, except there isn't yet
49 # at least one character to filter by
50 # reason: don't show options if the user types "cssh <tab><tab>"
51 paroptions="$clusters"
52 [ -n "$cur" ] && paroptions="$paroptions $options"
53
54 case $prev in
55 --cluster-file|-c|--config-file|-C)
56 COMPREPLY=( $( compgen -o filenames -G "$cur*" ) )
57 ;;
58 *)
59 COMPREPLY=()
60
61 # also use ssh hosts for tab completion if function _known_hosts is present
62 [ "`type -t _known_hosts`" = "function" ] && _known_hosts -a
63
64 COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "$paroptions" | grep "^$cur") )
65 ;;
66 esac
67
68 return 0
69 } &&
70 complete -F _cssh cssh crsh ctel