Skip to content
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

WIP: Support more bind9 utilities #1321

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 3 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
198 changes: 198 additions & 0 deletions completions/bind9
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# 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
pemensik marked this conversation as resolved.
Show resolved Hide resolved
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

_comp_cmd_dig__list_plusopts()
{
local CMD="${1:-dig}" # delv and mdig has similar style options
"$CMD" -h 2>&1 | awk '/^\s+\+\[no\]/ { sub("+\\[no\\]", "", $1); sub("=##+$", "=", $1); sub("\\[=##+\\]", "", $1); print "+"$1, "+no"$1} /^\s+\+[^[]/ {sub("=##+", "=", $1); print $1}'
}

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

case $prev in
-c)
_comp_cmd_nslookup__queryclass
return
;;
-t)
_comp_cmd_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

if [[ $cur == -* ]]; then
_comp_compgen_usage
pemensik marked this conversation as resolved.
Show resolved Hide resolved
return
elif [[ $cur == @* ]]; then
_comp_compgen_known_hosts -- "$cur"
return
elif [[ $cur == +* ]]; then
_comp_compgen_split -- "$(_comp_cmd_dig__list_plusopts dig)"
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
return
fi

# TODO: dig is tricky. It can accept hostname, queryclass or querytype without any parameter.
# Not sure how to autocomplete all of them.
_comp_compgen_known_hosts -- "$cur"
} && 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_cmd_nslookup__queryclass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is defined in another file, which may not be loaded in the session. One needs to "export" the function by renaming _comp_cmd_nslookup__queryclass to _comp_xfunc_nslookup_compgen_queryclass and call it using _comp_compgen -x nslookup queryclass.

This means that we need to modify bash-completion too even if we'd include the completion settings to the upstream.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, yes. unless we would rely on named-rrchecker providing list of supported types. That does not come with common package containing utilities only.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

further more, it might be useful also for ldns drill tool or unbound-host. Even though better would be getting actually supported list from the tools itself, we do not have it always at hand.

But the question is, whether it would not make sense to move nslookup functions into the bind9 as a whole. They are after all bind9 utilities in normal circumstances.

Copy link
Collaborator

@akinomyoga akinomyoga Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the question is, whether it would not make sense to move nslookup functions into the bind9 as a whole. They are after all bind9 utilities in normal circumstances.

We haven't been officially doing that when there are multiple implementations for a specific command name, but I think it would make sense. We should still keep our current implementation of completions/nslookup at completions/_nslookup for the other implementations of nslookup. We've been regarded our completion/_<cmd> completion settings as "deprecated" completion which should be replaced by the newer implementation of the upstream completion settings, but the present suggestion is to use completion/_<cmd> as the "default" completion settings when a specific completion setting is not provided by the upstream project. Or we might add even another form of the completion files for the "default" completion or another directory for the "default" completion. Thus, your suggestion involves a reconsideration of our framework about the role of our completion/_<cmd> settings or introduction of the mechanism of defining the "default" completion.

return
;;
-t)
_comp_cmd_nslookup__querytype
return
;;
-q)
_comp_compgen_known_hosts -- "$cur"
return
;;
-f)
_comp_compgen filedir
return
;;
-x|-b)
_comp_compgen_ip_addresses -- "$cur"
return
;;
esac

if [[ $cur == -* ]]; then
_comp_compgen_usage
return
elif [[ $cur == @* ]]; then
_comp_compgen_known_hosts -- "$cur"
return
elif [[ $cur == +* ]]; then
pemensik marked this conversation as resolved.
Show resolved Hide resolved
_comp_compgen_split -- "$(_comp_cmd_dig__list_plusopts mdig)"
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace

fi

# TODO: dig is tricky. It can accept hostname, queryclass or querytype without any parameter.
# Not sure how to autocomplete all of them.
_comp_compgen_known_hosts -- "$cur"
} && 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_cmd_nslookup__queryclass
return
;;
-t)
_comp_cmd_nslookup__querytype
return
;;
-q)
_comp_compgen_known_hosts -- "$cur"
return
;;
-a)
_comp_compgen filedir
return
;;
-x|-b)
_comp_compgen_ip_addresses -- "$cur"
return
;;
esac

if [[ $cur == -* ]]; then
_comp_compgen_usage
return
fi
if [[ $cur == @* ]]; then
_comp_compgen_known_hosts -- "$cur"
return
fi
if [[ $cur == +* ]]; then
_comp_compgen_split -- "$(_comp_cmd_dig__list_plusopts delv)"
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace

fi

# TODO: dig is tricky. It can accept hostname, queryclass or querytype without any parameter.
# Not sure how to autocomplete all of them.
_comp_compgen_known_hosts -- "$cur"
} && complete -F _comp_cmd_delv delv

# ex: filetype=sh
Loading