source: trunk/confman_completion.in @ 330

Revision 330, 5.2 KB checked in by ccowart, 3 years ago (diff)

Re-enabling bash completions in Makefile. Telling completions how to find
modules now. Fixing up svn:ignore.

  • Property svn:keywords set to Id
Line 
1#!@BASH@
2#
3# Copyright (c) 2008, Christopher Cowart and contributors
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# * Redistributions of source code must retain the above copyright
10#   notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above copyright
12#   notice, this list of conditions and the following disclaimer in the
13#   documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# $Id$
28#
29#                                               TODO
30#
31#               * Right now this won't update module listings until they
32#                 get added to the recipe file.
33
34_confman()
35{
36        local cur prev cmds twoback cmds fd_cmds mod_cmds chkpt_cmds
37        local CONFMAN CONFMAN_CONF CONFMAN_RC RECIPE_PATH WORK_PATH
38
39        # Setup ConfMan specific vars
40        CONFMAN="@sbindir@/confman"
41        CONFMAN_CONF="@sysconfdir@/confman.conf"
42        CONFMAN_RC=~/.confmanrc
43        if [ -e ${CONFMAN_RC} ] ; then
44                RECIPE_PATH=$(grep '^RECIPE_PATH' ${CONFMAN_RC}|cut -d'"' -f2)
45                WORK_PATH=$(grep '^WORK_PATH' ${CONFMAN_RC}|cut -d'"' -f2)
46        elif [ -e ${CONFMAN_CONF} ] ; then
47                RECIPE_PATH=$(grep '^RECIPE_PATH' ${CONFMAN_CONF}|cut -d'"' -f2)
48                WORK_PATH=$(grep '^WORK_PATH' ${CONFMAN_CONF}|cut -d'"' -f2)
49        fi
50        RECIPE_PATH=$(eval echo $RECIPE_PATH)
51        WORK_PATH=$(eval echo $WORK_PATH)
52
53        # Setup Standard command completion vars
54        COMPREPLY=()
55        cur="${COMP_WORDS[COMP_CWORD]}"
56        prev="${COMP_WORDS[COMP_CWORD-1]}"
57        if [ ${#COMP_WORDS[*]} -gt 3 ] ; then
58            twoback="${COMP_WORDS[COMP_CWORD-2]}"
59        fi
60        if [ ${#COMP_WORDS[*]} -gt 4 ] ; then
61            threeback="${COMP_WORDS[COMP_CWORD-3]}"
62        fi
63        [ ! -x $CONFMAN ] && echo "confman support not available!!" && exit 1
64       
65        # These vars are to better organize things to complete to as well as
66        # provide loose documention about what this does.
67        #
68        #       fd_cmds:        commands that take files and directories as args
69        #       mod_cmds:       commands that take modules as args
70        #       chkpt_cmds:     commands that take modules that take checkpoints as args
71        #                               Ex. confman checknew MODULE CHECKPOINT
72        #                                                               /\ A chkpt_cmd
73        #       cmds:           commands that confman can take as the first arg
74        #
75        fd_cmds="rm cp mv mkdir ls chown chgrp chmod chcom log install revert ln chln"
76        mod_cmds="create rmmod import checklook checknew checkclear rollback"
77        # 'rollback' is also a chkpt_cmd, but needs to be handled separately
78        # because it's the only one that handles dated checkpoints.
79        chkpt_cmds="checklook checknew checkclear"
80        cmds="help setup update commit diff status rollback $fd_cmds $mod_cmds"
81       
82        if [ "${prev}" == "confman" -o "${prev}" == "help" ] ; then
83                COMPREPLY=($(compgen -W "${cmds}" -- ${cur})) 
84                return 0
85        elif [ `expr "${fd_cmds}" : ".*\(${prev}\).*"` ] ; then
86                COMPREPLY=( $(compgen -A directory -A file ${cur}) )
87                return 0
88        elif [ `expr "${mod_cmds}" : ".*\(${prev}\).*"` ] ; then
89                local modules=$(confman recipe print $(confman recipe get) | \
90            grep -vE '^#|^$' $RECIPE_PATH | tr '\n' ' ')
91                COMPREPLY=($(compgen -W "${modules}" -- ${cur}))
92                return 0
93        elif [ `expr "import ${fd_cmds}" : ".*\(${twoback}\).*"` ] ; then
94                COMPREPLY=($(compgen -A directory -A file ${cur}))
95                return 0
96        elif [ `expr "${chkpt_cmds}" : ".*\(${twoback}\).*"` ] ; then
97                local module=${prev}
98                local checkpoints=$(confman checklook ${module}|tr '\n' ' ')
99                COMPREPLY=($(compgen -W "${checkpoints}" -- ${cur}))
100                return 0
101        elif [ "rollback" == "${twoback}" ] ; then
102                local module=${prev}
103                local module_path="${WORK_PATH}/${module}"
104                local named_checkpoints=$(confman checklook ${module}|\
105                                                tr '\n' ' '
106                                        )
107                local numbered_checkpoints=$(confman log $module_path|grep '^r[0-9][0-9]*' |\
108                        sed 's/.*\(20[0-9][0-9]\)-\([0-9][0-9]\)-\([0-9][0-9]\).*/"\1\2\3"/'|\
109                        tr '\n' ' ')
110                checkpoints="${named_checkpoints} ${numbered_checkpoints}"
111                COMPREPLY=($(compgen -W "${checkpoints}" -- ${cur}))
112                return 0
113        elif [ "rollback" == "${threeback}" ] ; then
114                local module=${twoback}
115                local module_path="${WORK_PATH}/${module}"
116                local day=${prev:0:4}-${prev:4:2}-${prev:6:2}
117                local hoursmins=$(confman log $module_path|grep '^r[0-9][0-9]*' |\
118                                        grep 2006-04-06|\
119                                        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"/'|\
120                                        tr '\n' ' '
121                                )
122                COMPREPLY=($(compgen -W "${hoursmins}" -- ${cur}))
123                return 0
124        fi
125
126        return 0
127}
128
129# It's necessary to not define a catch-all completion above so that we
130# can use the '-o filenames' switch to best handle {file,dir}name
131# completions.
132complete -F _confman -o filenames confman
133
134# vim:ts=4
Note: See TracBrowser for help on using the repository browser.