# bash completion for vpn-router-group -*- shell-script -*- # The names of the users and of the groups of the router's directory, read as root; nothing for other users. _vpn_router_group_users() { 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_group_names() { 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_group() { 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 for ((i = 1; i < cword; i++)); do if [[ -z $command ]]; then [[ ${words[i]} != -* ]] && command=${words[i]} continue fi case ${words[i]} in --member | --description | --name) ((i++)) ;; -*) ;; *) ((args++)) ;; esac done if [[ -z $command ]]; then if [[ $cur == -* ]]; then COMPREPLY=($(compgen -W '--json --help' -- "$cur")) else COMPREPLY=($(compgen -W 'list add update remove member-add member-remove' -- "$cur")) fi return fi case $prev in --member) COMPREPLY=($(compgen -W "$(_vpn_router_group_users)" -- "$cur")) return ;; --description | --name) return ;; esac $split && return if [[ $cur == -* ]]; then local options='--help' case $command in add) options+=' --member --description' ;; update) options+=' --name --description' ;; esac COMPREPLY=($(compgen -W "$options" -- "$cur")) return fi # The group, which add takes new, then the user of member-add and member-remove. if ((args == 0)) && [[ $command != @(list|add) ]]; then COMPREPLY=($(compgen -W "$(_vpn_router_group_names)" -- "$cur")) elif ((args == 1)) && [[ $command == member-@(add|remove) ]]; then COMPREPLY=($(compgen -W "$(_vpn_router_group_users)" -- "$cur")) fi } && complete -F _vpn_router_group vpn-router-group # ex: filetype=sh