# bash completion for vpn-router-site -*- shell-script -*- # The names of the sites, of the groups and of the general-purpose issuing CAs, read as root; nothing for other users. _vpn_router_site_names() { vpn-router-site --json list 2>/dev/null | python3 -c 'import json, sys; print("\n".join(site["name"] for site in json.load(sys.stdin)["sites"]))' 2>/dev/null } _vpn_router_site_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_site_cas() { python3 -c 'import sys; sys.path.insert(0, "/usr/lib/vpn-router"); import vpnrouter print("\n".join(vpnrouter.load_config()["pki"]["issuing_cas"]))' 2>/dev/null } _vpn_router_site() { 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 --host | --port | --listen | --tls | --ca | --certificate | --key | --index | --editors) ((i++)) ;; -*) ;; *) ((args++)) ;; esac done if [[ -z $command ]]; then if [[ $cur == -* ]]; then COMPREPLY=($(compgen -W '--json --help' -- "$cur")) else COMPREPLY=($(compgen -W 'list show add update remove' -- "$cur")) fi return fi case $prev in --listen) COMPREPLY=($(compgen -W 'internal external both' -- "$cur")) return ;; --tls) COMPREPLY=($(compgen -W 'none acme ca file' -- "$cur")) return ;; --ca) COMPREPLY=($(compgen -W "$(_vpn_router_site_cas)" -- "$cur")) return ;; --editors) COMPREPLY=($(compgen -W "$(_vpn_router_site_groups)" -- "$cur")) return ;; --certificate | --key) _filedir return ;; --host | --port | --index) return ;; esac $split && return if [[ $cur == -* ]]; then local options='--help' case $command in add | update) options+=' --host --no-hosts --port --listen --tls --ca --certificate --key --index --spa --no-spa --enabled --no-enabled --editors' ;; remove) options+=' --content' ;; esac COMPREPLY=($(compgen -W "$options" -- "$cur")) return fi # The site, which add takes new. if ((args == 0)) && [[ $command != @(list|add) ]]; then COMPREPLY=($(compgen -W "$(_vpn_router_site_names)" -- "$cur")) fi } && complete -F _vpn_router_site vpn-router-site # ex: filetype=sh