From c92e030a4bf5443c3e0765645ef7bf32130ec4c0 Mon Sep 17 00:00:00 2001 From: Travis LaDuke Date: Fri, 27 Sep 2019 14:50:21 -0700 Subject: [PATCH] Create a bash completion script. Just adding it to the repo, but it still needs to be dealt with during install. Probably put it in $ZT_HOME and then symlink to the proper place for the distro? searches $ZT_HOME/networks.d/ for network IDs searches HISTORY for 16 digit numbers that look like network IDs. --- zerotier-cli-completion.bash | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 zerotier-cli-completion.bash diff --git a/zerotier-cli-completion.bash b/zerotier-cli-completion.bash new file mode 100644 index 000000000..cd6da0d9a --- /dev/null +++ b/zerotier-cli-completion.bash @@ -0,0 +1,57 @@ +#compdef zerotier-cli +#autoload + + +_get_network_ids () +{ + if [[ "$OSTYPE" == "darwin"* ]]; then + COMPREPLY=($(compgen -W "$(ls -1 /Library/Application\ Support/ZeroTier/One/networks.d | cut -c 1-16)" -- ${cur})) + else + COMPREPLY=($(compgen -W "$(ls -1 /var/lib/zerotier-one/networks.d | cut -c 1-16)" -- ${cur})) + fi +} + +_get_network_ids_from_history () +{ + COMPREPLY=($(compgen -W "$(fc -l -1000 -1 | sed -n 's/.*\([[:xdigit:]]\{16\}\).*/\1/p')" -- ${cur})) +} + +_zerotier-cli_completions() +{ + local cur prev + + cur=${COMP_WORDS[COMP_CWORD]} + prev=${COMP_WORDS[COMP_CWORD-1]} + + case ${COMP_CWORD} in + 1) + COMPREPLY=($(compgen -W "info listpeers peers listnetworks join leave set get listmoons orbit deorbit" -- ${cur})) + ;; + 2) + case ${prev} in + leave) + _get_network_ids + ;; + join) + _get_network_ids_from_history + ;; + set) + _get_network_ids + ;; + get) + _get_network_ids + ;; + *) + COMPREPLY=() + ;; + esac + ;; + *) + COMPREPLY=() + ;; + esac +} + +complete -F _zerotier-cli_completions zerotier-cli + +