# bash completion for vpn-config-edit -*- shell-script -*- # Python of the package that answers from its schema (vpnrouter.SCHEMA, vpnrouter.CONNECTION) and, read as root, the # site-to-site connections of the configuration: settings prints SECTION.OPTION of every setting and # site_to_site.NAME.OPTION of every connection, connections site_to_site.NAME, and values the values of a boolean # setting, $1. _vpn_config_edit_python() { python3 -c ' import sys sys.path.insert(0, "/usr/lib/vpn-router") import vpnrouter try: connections = vpnrouter.read_settings().get("site_to_site", {}) except (OSError, vpnrouter.ConfigError): connections = {} if sys.argv[1] == "settings": print("\n".join(f"{section}.{option}" for section, options in vpnrouter.SCHEMA.items() for option in options)) print("\n".join(f"site_to_site.{name}.{option}" for name in connections for option in vpnrouter.CONNECTION)) elif sys.argv[1] == "connections": print("\n".join(f"site_to_site.{name}" for name in connections)) else: section, _, option = sys.argv[2].rpartition(".") defaults = vpnrouter.CONNECTION if section.startswith("site_to_site.") else vpnrouter.SCHEMA.get(section, {}) if isinstance(defaults.get(option), bool): print("true\nfalse") ' "$@" 2>/dev/null } _vpn_config_edit() { 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. local i command='' args=() for ((i = 1; i < cword; i++)); do if [[ -z $command ]]; then [[ ${words[i]} != -* ]] && command=${words[i]} elif [[ ${words[i]} != -* ]]; then args+=("${words[i]}") fi done if [[ $cur == -* ]]; then COMPREPLY=($(compgen -W "--help$([[ $command == @(set|remove) ]] && echo ' --restart')" -- "$cur")) return fi case $command,${#args[@]} in ,*) COMPREPLY=($(compgen -W 'show set remove' -- "$cur")) ;; set,0) COMPREPLY=($(compgen -W "$(_vpn_config_edit_python settings)" -- "$cur")) ;; set,1) COMPREPLY=($(compgen -W "$(_vpn_config_edit_python values "${args[0]}")" -- "$cur")) ;; remove,0) COMPREPLY=($(compgen -W "$(_vpn_config_edit_python connections)" -- "$cur")) ;; esac } && complete -F _vpn_config_edit vpn-config-edit # ex: filetype=sh