# bash completion for continuum-party, a party's client of a federation -*- shell-script -*- # # Completes its commands and options, and from the node it works with (the options given # before the command: --node, --federation, --config and the rest) the proposals on the # current version by hash, the parties, the places holding a value; the federations in # ~/.config/continuum/federations; and the revisions' features it knows. # The client, with the options given before the command, its errors left out. _continuum_party_asking() { "${_continuum_party_client[@]}" "$@" 2>/dev/null } # The proposals on the current version, by hash. _continuum_party_proposals() { _continuum_party_asking proposals | sed -n 's/^\([0-9a-f]\{64\}\): .*/\1/p' } # The parties of the current version. _continuum_party_parties() { _continuum_party_asking status | awk '/^parties$/ { on = 1; next } on && /^[^ ]/ { on = 0 } on && /^ [^ ]/ { print $1 }' } # The places holding a value, and the paths on their way. _continuum_party_places() { _continuum_party_asking values | cut -d' ' -f1 | awk -F/ '{ path = $1; print path; for (i = 2; i <= NF; i++) { path = path "/" $i; print path } }' | sort -u } # The features of the revisions the client knows, as `--version` names them. _continuum_party_features() { "${_continuum_party_client[0]}" --version 2>/dev/null | grep -o '([a-z0-9-]*)' | tr -d '()' } # The federations, by their directories' names. _continuum_party_federations() { local dir=${XDG_CONFIG_HOME:-$HOME/.config}/continuum/federations entry [[ -d $dir ]] || return for entry in "$dir"/*/; do [[ -d $entry ]] || continue entry=${entry%/} printf '%s\n' "${entry##*/}" done } # Files, or directories to find them in. _continuum_party_files() { if declare -F _filedir >/dev/null; then _filedir else COMPREPLY=($(compgen -f -- "$cur")) fi } _continuum_party_words() { COMPREPLY=($(compgen -W "$1" -- "$cur")) } _continuum_party() { local cur prev words cword if declare -F _init_completion >/dev/null; then _init_completion -n =: || return else words=("${COMP_WORDS[@]}") cword=$COMP_CWORD cur=${words[cword]} prev=${words[cword - 1]} fi local global_options='--node --token --key --record --party --federation --config --unset --help --version' local commands='init write keys nodes next propose vote again restore found root fork check judge proposals show status values get twice help' local intents='admit remove give take keys nodes checkpoint revision' # The command, the intent of `propose` and `fork`, and the arguments after them that # are no options, as far as the word completed. local i command= intent= word local -a positional=() _continuum_party_client=("${words[0]}") for ((i = 1; i < cword; i++)); do word=${words[i]} if [[ -z $command ]]; then case $word in --node | --token | --key | --record | --party | --federation | --config | --unset) _continuum_party_client+=("$word" "${words[i + 1]}") ((i++)) ;; -*) _continuum_party_client+=("$word") ;; *) command=$word ;; esac continue fi case $word in --keys | --nodes | --at | --founder | --rule | --revision | --root | --add | \ --remove | --set) # Options taking values: those up to the next option are theirs. while ((i + 1 < cword)) && [[ ${words[i + 1]} != -* ]]; do ((i++)) case $word in --keys | --nodes | --at | --add | --remove | --set) ;; *) break ;; esac done ;; -*) ;; *) if [[ -z $intent && ${#positional[@]} -eq 0 ]] && [[ $command == propose || $command == fork ]] && [[ " $intents proposal " == *" $word "* ]]; then intent=$word else positional+=("$word") fi ;; esac done # A value of the option before. case $prev in --federation) _continuum_party_words "$(_continuum_party_federations)" return ;; --key | --record | --config | --root | --keys) _continuum_party_files return ;; --revision) _continuum_party_words "$(_continuum_party_features)" return ;; --add | --remove | --set) # Keys, or files of them; nodes by ID. [[ $command == keys ]] && _continuum_party_files return ;; --node | --token | --unset | --nodes | --founder | --rule | --at | --wait | --party) return ;; esac if [[ -z $command ]]; then if [[ $cur == -* ]]; then _continuum_party_words "$global_options" else _continuum_party_words "$commands" fi return fi local n=${#positional[@]} case $command in propose | fork) local own if [[ $command == propose ]]; then own='--yes --print --wait' else own='--at --founder' fi if [[ -z $intent ]]; then if [[ $cur == -* ]]; then [[ $command == propose ]] && _continuum_party_words "$own" elif ((n == 0)); then local offered=$intents [[ $command == fork ]] && offered="${intents/checkpoint/} proposal" _continuum_party_words "$offered" local -a named=("${COMPREPLY[@]}") _continuum_party_files COMPREPLY+=("${named[@]}") fi return fi case $intent in admit) own+=' --keys --nodes' ;; esac if [[ $cur == -* ]]; then _continuum_party_words "$own" return fi case $intent:$n in remove:0 | give:1 | keys:0 | nodes:0) local parties parties=$(_continuum_party_parties) [[ $intent == give ]] && parties+=' {party}' _continuum_party_words "$parties" ;; keys:*) _continuum_party_files ;; revision:0) _continuum_party_words "$(_continuum_party_features)" ;; proposal:0) _continuum_party_words "$(_continuum_party_proposals)" ;; esac ;; vote) if [[ $cur == -* ]]; then _continuum_party_words '--through --wait' elif ((n == 0)); then _continuum_party_words "$(_continuum_party_proposals)" elif ((n == 1)); then _continuum_party_words 'yes no' fi ;; show) ((n == 0)) && _continuum_party_words "$(_continuum_party_proposals)" ;; keys | nodes) if [[ $cur == -* ]]; then _continuum_party_words '--list --add --remove --set --force' fi ;; write | values | get) if [[ $cur == -* ]]; then [[ $command == values ]] && _continuum_party_words '--json' [[ $command == get ]] && _continuum_party_words '-r --raw' elif ((n == 0)); then _continuum_party_words "$(_continuum_party_places)" fi ;; init) [[ $cur == -* ]] && _continuum_party_words '--root' ;; status) [[ $cur == -* ]] && _continuum_party_words '--json' ;; next) [[ $cur == -* ]] && _continuum_party_words '--revision' ;; found) [[ $cur == -* ]] && _continuum_party_words '--nodes --rule --revision' ;; root | check | twice) _continuum_party_files ;; help) ((n == 0)) && _continuum_party_words "$commands" ;; esac } && complete -F _continuum_party continuum-party # ex: filetype=sh