# bash completion for vpn-router-role -*- shell-script -*- # The service accounts, the users, the groups and the roles of a resource of the router's directory, read as root; # nothing for other users. _vpn_router_role_resources() { vpn-router-user --json list 2>/dev/null | python3 -c 'import json, sys; print("\n".join(user["name"] for user in json.load(sys.stdin)["users"] if user["service"]))' 2>/dev/null } _vpn_router_role_accounts() { vpn-router-user --json list 2>/dev/null | python3 -c 'import json, sys; print("\n".join(user["name"] for user in json.load(sys.stdin)["users"]))' 2>/dev/null } _vpn_router_role_groups() { vpn-router-group --json list 2>/dev/null | python3 -c 'import json, sys; print("\n".join(group["name"] for group in json.load(sys.stdin)["groups"]))' 2>/dev/null } _vpn_router_role_names() { vpn-router-role --json list "$1" 2>/dev/null | python3 -c 'import json, sys; print("\n".join(role["name"] for role in json.load(sys.stdin)["roles"]))' 2>/dev/null } _vpn_router_role() { local cur prev words cword split _init_completion -s || return # The command is the first word that is not an option; the arguments are the words after it but option values. local i command='' args=0 resource='' for ((i = 1; i < cword; i++)); do if [[ -z $command ]]; then [[ ${words[i]} != -* ]] && command=${words[i]} continue fi case ${words[i]} in --description | --group | --account) ((i++)) ;; -*) ;; *) ((args == 0)) && resource=${words[i]} ((args++)) ;; esac done if [[ -z $command ]]; then if [[ $cur == -* ]]; then COMPREPLY=($(compgen -W '--json --help' -- "$cur")) else COMPREPLY=($(compgen -W 'list add remove member-add member-remove' -- "$cur")) fi return fi case $prev in --group) COMPREPLY=($(compgen -W "$(_vpn_router_role_groups)" -- "$cur")) return ;; --account) COMPREPLY=($(compgen -W "$(_vpn_router_role_accounts)" -- "$cur")) return ;; --description) return ;; esac $split && return if [[ $cur == -* ]]; then local options='--help' case $command in add) options+=' --description' ;; member-add | member-remove) options+=' --group --account' ;; esac COMPREPLY=($(compgen -W "$options" -- "$cur")) return fi # The resource, then the role, which add takes new. if ((args == 0)); then COMPREPLY=($(compgen -W "$(_vpn_router_role_resources)" -- "$cur")) elif ((args == 1)) && [[ $command != @(list|add) ]]; then COMPREPLY=($(compgen -W "$(_vpn_router_role_names "$resource")" -- "$cur")) fi } && complete -F _vpn_router_role vpn-router-role # ex: filetype=sh