#!/bin/bash # # This script is to be sourced into your bash shell to provide command-line TAB completion # of options, resources and commands for the service_mgr script. # # To source this script, you can: # # 1. source service_mgr_completion # 2. . service_mgr_completion # # If you want this to be available at all times, you can add one of the source commands # above into your .bashrc. # # # See if the any of the words currently on the command line are one of the valid resources. # # The first argument is a list of words currently on the # command line, the second+ arguments are the valid resources. # # If a match is found, the matching resource is echoed to stdout and the result code is set # to 0. # svc_mgr_find_match () { local resource local word for word in $1 do for resource in ${@:2} do [[ "$word" == "${resource}" ]] && echo $word && return 0 done done return 1 } # # This is the function return to perform completion analysis (see the 'complete' statement at # the end of this script. # _ServiceMgrComplete() { local cur COMPREPLY=() # Array storing the possible completions cur=${COMP_WORDS[COMP_CWORD]} # Store the current word on the command line case "$cur" in -*) # The current word on the command line starts with dash. Check against all # the valid command line options. COMPREPLY=( $( compgen -W '-h --help -s --server -n --no-ssl -l --list -t --token -p --third-party-id --d --service-definition-id -a --configured-account-id' -- $cur ) ) ;; *) # The current word on the command line is anything but a '-'. We need # to determine the appropriate completions based on what has already # been entered resources=$(valid_resources) # Get the list of valid resources current_word_list=${COMP_WORDS[@]} # Create a list of the current words on the # command line # Determine if one of the words currently on the command line is one # of the valid resources matched_name=$(svc_mgr_find_match "$current_word_list" ${resources}) resource_on_command_line=$? if [[ $resource_on_command_line == 0 ]] then # One of the words on the command line was one of our resources. Now # get the list of valid commands for that resource and complete against them... commands=$(valid_commands $matched_name) COMPREPLY=( $( compgen -W '$commands' -- $cur ) ) else # There was not a resource name on the command line. Complete the current # word against the list of valid resources COMPREPLY=( $( compgen -W '$resources' -- $cur ) ) fi ;; esac return 0 } # Register the _ServiceMgrComplete function as a completion function for # all executables named 'service_mgr'. complete -F _ServiceMgrComplete -o filenames service_mgr