source: tags/confman-1.0/bash_completions @ 92

Revision 92, 3.8 KB checked in by yontege, 6 years ago (diff)

Adding '# vim:ts=4' because I like those kind of tabstops.

Line 
1#!/bin/bash
2#
3#                                               TODO
4#
5#                       Can't think of any.             -yontege
6_confman()
7{
8        local cur prev cmds twoback cmds fd_cmds mod_cmds chkpt_cmds
9        local CONFMAN CONFMAN_CONF CONFMAN_RC RECIPE_PATH WORK_PATH
10
11        # Setup ConfMan specific vars
12        CONFMAN="/usr/local/rescomp/sbin/confman"
13        CONFMAN_CONF="/usr/local/rescomp/etc/confman.conf"
14        CONFMAN_RC="~/.confmanrc"
15        if [ -e ${CONFMAN_RC} ] ; then
16                RECIPE_PATH=$(grep '^RECIPE_PATH' ${CONFMAN_RC}|cut -d'"' -f2)
17                WORK_PATH=$(grep '^WORK_PATH' ${CONFMAN_RC}|cut -d'"' -f2)
18        elif [ -e ${CONFMAN_CONF} ] ; then
19                RECIPE_PATH=$(grep '^RECIPE_PATH' ${CONFMAN_CONF}|cut -d'"' -f2)
20                WORK_PATH=$(grep '^WORK_PATH' ${CONFMAN_CONF}|cut -d'"' -f2)
21        fi
22        RECIPE_PATH=$(eval echo $RECIPE_PATH)
23        WORK_PATH=$(eval echo $WORK_PATH)
24
25        # Setup Standard command completion vars
26        COMPREPLY=()
27        cur="${COMP_WORDS[COMP_CWORD]}"
28        prev="${COMP_WORDS[COMP_CWORD-1]}"
29        if [ ${#COMP_WORDS[*]} -gt 3 ] ; then
30            twoback="${COMP_WORDS[COMP_CWORD-2]}"
31        fi
32        if [ ${#COMP_WORDS[*]} -gt 4 ] ; then
33            threeback="${COMP_WORDS[COMP_CWORD-3]}"
34        fi
35        [ ! -x $CONFMAN ] && echo "confman support not available!!" && exit 1
36       
37        # These vars are to better organize things to complete to as well as
38        # provide loose documention about what this does.
39        #
40        #       fd_cmds:        commands that take files and directories as args
41        #       mod_cmds:       commands that take modules as args
42        #       chkpt_cmds:     commands that take modules that take checkpoints as args
43        #                               Ex. confman checknew MODULE CHECKPOINT
44        #                                                               /\ A chkpt_cmd
45        #       cmds:           commands that confman can take as the first arg
46        #
47        fd_cmds="rm cp mv mkdir ls chown chgrp chmod chcom log install revert"
48        mod_cmds="create rmmod import checklook checknew checkclear rollback"
49        # 'rollback' is also a chkpt_cmd, but needs to be handled separately
50        # because it's the only one that handles dated checkpoints.
51        chkpt_cmds="checklook checknew checkclear"
52        cmds="help setup update commit diff status rollback $fd_cmds $mod_cmds"
53       
54        if [ "${prev}" == "confman" -o "${prev}" == "help" ] ; then
55                COMPREPLY=($(compgen -W "${cmds}" -- ${cur})) 
56                return 0
57        elif [ `expr "${fd_cmds}" : ".*\(${prev}\).*"` ] ; then
58                COMPREPLY=( $(compgen -A directory -A file ${cur}) )
59                return 0
60        elif [ `expr "${mod_cmds}" : ".*\(${prev}\).*"` ] ; then
61                local modules=$(grep -vE '^#|^$' $RECIPE_PATH | tr '\n' ' ')
62                COMPREPLY=($(compgen -W "${modules}" -- ${cur}))
63                return 0
64        elif [ `expr "import ${fd_cmds}" : ".*\(${twoback}\).*"` ] ; then
65                COMPREPLY=($(compgen -A directory -A file ${cur}))
66                return 0
67        elif [ `expr "${chkpt_cmds}" : ".*\(${twoback}\).*"` ] ; then
68                local module=${prev}
69                local checkpoints=$(confman checklook ${module}|tr '\n' ' ')
70                COMPREPLY=($(compgen -W "${checkpoints}" -- ${cur}))
71                return 0
72        elif [ "rollback" == "${twoback}" ] ; then
73                local module=${prev}
74                local module_path="${WORK_PATH}/${module}"
75                local named_checkpoints=$(confman checklook ${module}|\
76                                                tr '\n' ' '
77                                        )
78                local numbered_checkpoints=$(confman log $module_path|grep '^r[0-9][0-9]*' |\
79                        sed 's/.*\(20[0-9][0-9]\)-\([0-9][0-9]\)-\([0-9][0-9]\).*/"\1\2\3"/'|\
80                        tr '\n' ' ')
81                checkpoints="${named_checkpoints} ${numbered_checkpoints}"
82                COMPREPLY=($(compgen -W "${checkpoints}" -- ${cur}))
83                return 0
84        elif [ "rollback" == "${threeback}" ] ; then
85                local module=${twoback}
86                local module_path="${WORK_PATH}/${module}"
87                local day=${prev:0:4}-${prev:4:2}-${prev:6:2}
88                local hoursmins=$(confman log $module_path|grep '^r[0-9][0-9]*' |\
89                                        grep 2006-04-06|\
90                                        sed 's/.*20[0-9][0-9]-[0-9][0-9]-[0-9][0-9] \([0-9][0-9]\):\([0-9][0-9]\).*/"\1\2"/'|\
91                                        tr '\n' ' '
92                                )
93                COMPREPLY=($(compgen -W "${hoursmins}" -- ${cur}))
94                return 0
95        fi
96
97        return 0
98}
99
100# It's necessary to not define a catch-all completion above so that we
101# can use the '-o filenames' switch to best handle {file,dir}name
102# completions.
103complete -F _confman -o filenames confman
104
105# vim:ts=4
Note: See TracBrowser for help on using the repository browser.