github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/internal/fs/completion/bash.template (about) 1 _choria_bash_autocomplete() { 2 local cur prev opts base 3 COMPREPLY=() 4 cur="${COMP_WORDS[COMP_CWORD]}" 5 6 if ( _array_contains COMP_WORDS "req" || _array_contains COMP_WORDS "rpc" ) && [[ ${COMP_WORDS[$COMP_CWORD]} != "-"* ]] ; then 7 _choria_req_bash_autocomplete 8 else 9 opts=$( ${COMP_WORDS[0]} --completion-bash ${COMP_WORDS[@]:1:$COMP_CWORD} ) 10 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) 11 fi 12 return 0 13 } 14 15 # https://stackoverflow.com/a/14367368 16 _array_contains() { 17 local array="$1[@]" 18 local seeking=$2 19 local in=1 20 for element in "${!array}"; do 21 if [[ $element == "$seeking" ]]; then 22 in=0 23 break 24 fi 25 done 26 return $in 27 } 28 29 _choria_req_bash_autocomplete() { 30 # Assume for completion to work, the command line must match 31 # choria [options] <req|rpc> [agent [action]] 32 # Options are not alloed to appear between req and the action 33 34 # Find the index of req/rpc in the input to serve as the anchor point for where the agent/action appear 35 for index in "${!COMP_WORDS[@]}"; do 36 if [[ "${COMP_WORDS[$index]}" = "req" ]] || [[ "${COMP_WORDS[$index]}" = "rpc" ]] ; then 37 BASE_INDEX=$index 38 break 39 fi 40 done 41 42 AGENT_INDEX=$(expr $BASE_INDEX + 1) 43 ACTION_INDEX=$(expr $BASE_INDEX + 2) 44 45 # If the agent/action are already selected, present the inputs and long-options as further completions 46 if [[ "${#COMP_WORDS[@]}" -gt $(expr $ACTION_INDEX + 1) ]] ; then 47 local INPUTS=$(choria completion --list inputs --agent ${COMP_WORDS[$AGENT_INDEX]} --action ${COMP_WORDS[$ACTION_INDEX]} 2>/dev/null | sed -e 's/$/=/') 48 49 # Prevent inputs from having a space added to them, since they need to be in KEY=VALUE format 50 compopt -o nospace 51 52 COMPREPLY=($(compgen -W "${INPUTS}" -- ${COMP_WORDS[$COMP_CWORD]})) 53 54 # If the agent is selected, present the available actions on the selected agent as completions 55 elif [[ "${#COMP_WORDS[@]}" -gt $(expr $AGENT_INDEX + 1) ]]; then 56 local ACTIONS=$(choria completion --list actions --agent ${COMP_WORDS[$AGENT_INDEX]} 2>/dev/null) 57 COMPREPLY=($(compgen -W "${ACTIONS}" -- ${COMP_WORDS[$COMP_CWORD]})) 58 59 # If nothing is selected, present the available agents as completions 60 elif [[ "${#COMP_WORDS[@]}" -gt $(expr $BASE_INDEX + 1) ]] ; then 61 local AGENTS=$(choria completion --list agents 2>/dev/null) 62 COMPREPLY=($(compgen -W "${AGENTS}" -- ${COMP_WORDS[$COMP_CWORD]})) 63 fi 64 } 65 66 complete -F _choria_bash_autocomplete choria