Skip to content

WIP: Support more bind9 utilities #1321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 214 additions & 0 deletions completions/bind9
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
# bash completion for nslookup -*- shell-script -*-
# bind9 utilities completion

_comp_cmd_rndc__list_commands()
{
rndc 2>&1 | awk '/^ / {print $1}' | sort -u
}

_comp_cmd_named_checkconf__list_zones()
{
named-checkconf -l | awk '{print $1}'
}

_comp_cmd_rndc__list_parameters()
{
local SUBCMD=$1
rndc 2>&1 | awk "/^ ${SUBCMD} / { print \$2 }"
}

_comp_cmd_rndc()
{
local cur prev words cword comp_args
_comp_initialize -n = -- "$@" || return

case $prev in
-c|-k)
_comp_compgen_filedir
return
;;
-s)
_comp_compgen_known_hosts -- "$cur"
return
;;
esac

local REPLY
_comp_count_args
if ((REPLY == 1)); then
_comp_compgen_split -- "$(_comp_cmd_rndc__list_commands)"
elif ((REPLY == 2)); then
local PARAMS="$(_comp_cmd_rndc__list_parameters $prev)"
if [[ "$PARAMS" == zone ]]; then
_comp_compgen_split -- "$(_comp_cmd_named_checkconf__list_zones)"
return
else
_comp_compgen_split -- "$PARAMS"
fi
fi
if [[ $cur == -* ]]; then
_comp_compgen_usage
return
fi
} && complete -F _comp_cmd_rndc rndc

# Generate list of +example opts from dig help, including negative.
_comp_cmd_dig__list_plusopts()
{
"${comp_args[0]}" -h 2>&1 | awk '/^\s+\+\[no\]/ { sub("+\\[no\\]", "", $1); sub("=##+$", "=", $1); sub("\\[=##+\\]", "", $1); print "+"$1, "+no"$1} /^\s+\+[^[]/ {sub("=##+", "=", $1); print $1}'
}

# Implements common dig usage patterns
_comp_cmd_dig__common()
{
if [[ $cur == -* ]]; then
_comp_compgen_help -- -h
return
elif [[ $cur == @* ]]; then
_comp_compgen_known_hosts -- "$cur"
return
elif [[ $cur == +* ]]; then
if [[ $cur == *= ]]; then
return
fi
_comp_compgen_split -- "$(_comp_cmd_dig__list_plusopts)"
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
return
fi

_comp_compgen_known_hosts -- "$cur"
_comp_compgen -x nslookup queryclass
_comp_compgen -x nslookup querytype
}

_comp_cmd_dig()
{
local cur prev words cword comp_args
_comp_initialize -n = -- "$@" || return

case $prev in
-c)
_comp_compgen -x nslookup queryclass
return
;;
-t)
_comp_compgen -x nslookup querytype
return
;;
-q)
_comp_compgen_known_hosts -- "$cur"
return
;;
-k|-f)
_comp_compgen_filedir
return
;;
-x|-b)
_comp_compgen_ip_addresses -- "$cur"
return
;;
esac

_comp_cmd_dig__common "$@"
} && complete -F _comp_cmd_dig dig

_comp_cmd_mdig()
{
local cur prev words cword comp_args
_comp_initialize -n = -- "$@" || return

case $prev in
-c)
_comp_compgen -x nslookup queryclass
return
;;
-t)
_comp_compgen -x nslookup querytype
return
;;
-q)
_comp_compgen_known_hosts -- "$cur"
return
;;
-f)
_comp_compgen_filedir
return
;;
-x|-b)
_comp_compgen_ip_addresses -- "$cur"
return
;;
esac

_comp_cmd_dig__common "$@"
} && complete -F _comp_cmd_mdig mdig

_comp_cmd_delv()
{
local cur prev words cword comp_args
_comp_initialize -n = -- "$@" || return

case $prev in
-c)
_comp_compgen -x nslookup queryclass
return
;;
-t)
_comp_compgen -x nslookup querytype
return
;;
-q)
_comp_compgen_known_hosts -- "$cur"
return
;;
-a)
_comp_compgen_filedir
return
;;
-x|-b)
_comp_compgen_ip_addresses -- "$cur"
return
;;
esac

_comp_cmd_dig__common "$@"
} && complete -F _comp_cmd_delv delv

# kdig is provided by knot-utils, but is more or less a drop-in replacement for dig
# Reuse common parts for it too.
_comp_cmd_kdig()
{
local cur prev words cword comp_args
_comp_initialize -n = -- "$@" || return

case $prev in
-c)
_comp_compgen -x nslookup queryclass
return
;;
-t)
_comp_compgen -x nslookup querytype
return
;;
-q)
_comp_compgen_known_hosts -- "$cur"
return
;;
-k|-E|-G)
_comp_compgen_filedir
return
;;
-x|-b)
_comp_compgen_ip_addresses -- "$cur"
return
;;
esac

if [[ $cur == -* ]]; then
_comp_compgen_usage
return
fi
_comp_cmd_dig__common "$@"
} && complete -F _comp_cmd_kdig kdig

# ex: filetype=sh
12 changes: 6 additions & 6 deletions completions/nslookup
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# bash completion for nslookup -*- shell-script -*-

_comp_cmd_nslookup__queryclass()
_comp_xfunc_nslookup_compgen_queryclass()
{
_comp_compgen -a -- -W 'IN CH HS ANY'
}

_comp_cmd_nslookup__querytype()
_comp_xfunc_nslookup_compgen_querytype()
{
# https://en.wikipedia.org/wiki/List_of_DNS_record_types
# Resource records
Expand All @@ -31,12 +31,12 @@ _comp_cmd_nslookup()
case $cur in
-class=* | -cl=*)
cur=${cur#*=}
_comp_cmd_nslookup__queryclass
_comp_xfunc_nslookup_compgen_queryclass
return
;;
-querytype=* | -type=* | -q=* | -ty=*)
cur=${cur#*=}
_comp_cmd_nslookup__querytype
_comp_xfunc_nslookup_compgen_querytype
return
;;
-?*=*)
Expand Down Expand Up @@ -68,11 +68,11 @@ _comp_cmd_host()

case $prev in
-c)
_comp_cmd_nslookup__queryclass
_comp_xfunc_nslookup_compgen_queryclass
return
;;
-t)
_comp_cmd_nslookup__querytype
_comp_xfunc_nslookup_compgen_querytype
return
;;
-m)
Expand Down