Skip to content

Commit e03f752

Browse files
committed
feat: add completions command
1 parent 51edb41 commit e03f752

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

cmd/doggo/cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ var (
2323
)
2424

2525
func main() {
26+
if len(os.Args) > 1 && os.Args[1] == "completions" {
27+
completionsCommand()
28+
return
29+
}
30+
2631
app := app.New(logger, buildVersion)
2732
f := setupFlags()
2833

cmd/doggo/completions.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
var (
9+
bashCompletion = `
10+
_doggo() {
11+
local cur prev opts
12+
COMPREPLY=()
13+
cur="${COMP_WORDS[COMP_CWORD]}"
14+
prev="${COMP_WORDS[COMP_CWORD-1]}"
15+
16+
opts="-v --version -h --help -q --query -t --type -n --nameserver -c --class -r --reverse --strategy --ndots --search --timeout -4 --ipv4 -6 --ipv6 --tls-hostname --skip-hostname-verification -J --json --short --color --debug --time"
17+
18+
case "${prev}" in
19+
-t|--type)
20+
COMPREPLY=( $(compgen -W "A AAAA CAA CNAME HINFO MX NS PTR SOA SRV TXT" -- ${cur}) )
21+
return 0
22+
;;
23+
-c|--class)
24+
COMPREPLY=( $(compgen -W "IN CH HS" -- ${cur}) )
25+
return 0
26+
;;
27+
--strategy)
28+
COMPREPLY=( $(compgen -W "all random first" -- ${cur}) )
29+
return 0
30+
;;
31+
--search|--color|--debug)
32+
COMPREPLY=( $(compgen -W "true false" -- ${cur}) )
33+
return 0
34+
;;
35+
esac
36+
37+
if [[ ${cur} == -* ]]; then
38+
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
39+
return 0
40+
fi
41+
}
42+
43+
complete -F _doggo doggo
44+
`
45+
46+
zshCompletion = `
47+
#compdef _doggo doggo
48+
49+
_doggo() {
50+
_arguments \
51+
"(- 1 *)"{-v,--version}"[Show version of doggo]" \
52+
"(- 1 *)"{-h,--help}"[Show list of command-line options]" \
53+
{-q,--query=}"[Hostname to query the DNS records for]::_hosts" \
54+
{-t,--type=}"[Type of the DNS Record]:(record type):(A AAAA CAA CNAME HINFO MX NS PTR SOA SRV TXT)" \
55+
{-n,--nameserver=}"[Address of a specific nameserver to send queries to]::_hosts;" \
56+
{-c,--class=}"[Network class of the DNS record being queried]:(network class):(IN CH HS)" \
57+
{-r,--reverse}"[Performs a DNS Lookup for an IPv4 or IPv6 address]" \
58+
--strategy="[Strategy to query nameserver listed in etc/resolv.conf]:(strategy):(all random first)" \
59+
--ndots="[Number of requred dots in hostname to assume FQDN]:(number of dots):()" \
60+
--search"[Defaults to true. Set --search=false to not use the search list defined in resolve.conf]:(setting):(true false)" \
61+
--timeout"[Timeout (in seconds) for the resolver to return a response]:(seconds):()" \
62+
{-4,--ipv4}"[Use IPv4 only]" \
63+
{-6,--ipv6}"[Use IPv6 only]" \
64+
--tls-hostname="[Hostname used for verification of certificate incase the provided DoT nameserver is an IP]::_hosts" \
65+
--skip-hostname-verification"[Skip TLS hostname verification in case of DoT lookups]" \
66+
{-J,--json}"[Format the output as JSON]" \
67+
--short"[Shows only the response section in the output]" \
68+
--color="[Defaults to true. Set --color=false to disable colored output]:(setting):(true false)" \
69+
--debug"[Enable debug logging]:(setting):(true false)" \
70+
--time"[Shows how long the response took from the server]" \
71+
'*:hostname:_hosts'
72+
}
73+
`
74+
75+
fishCompletion = `
76+
# Meta options
77+
complete -c doggo -l 'version' -d "Show version of doggo"
78+
complete -c doggo -l 'help' -d "Show list of command-line options"
79+
80+
# Single line all options
81+
complete -c doggo -x -a "(__fish_print_hostnames) A AAAA CAA CNAME HINFO MX NS PTR SOA SRV TXT IN CH HS"
82+
83+
# Query options
84+
complete -c doggo -s 'q' -l 'query' -d "Hostname to query the DNS records for" -x -a "(__fish_print_hostnames)"
85+
complete -c doggo -s 't' -l 'type' -d "Type of the DNS Record" -x -a "A AAAA CAA CNAME HINFO MX NS PTR SOA SRV TXT"
86+
complete -c doggo -s 'n' -l 'nameserver' -d "Address of a specific nameserver to send queries to" -x -a "1.1.1.1 8.8.8.8 9.9.9.9 (__fish_print_hostnames)"
87+
complete -c doggo -s 'c' -l 'class' -d "Network class of the DNS record being queried" -x -a "IN CH HS"
88+
89+
# Transport options
90+
complete -c doggo -x -a "@udp:// @tcp:// @https:// @tls:// @sdns://" -d "Select the protocol for resolving queries"
91+
92+
# Resolver options
93+
complete -c doggo -l 'ndots' -d "Specify ndots parameter. Takes value from /etc/resolv.conf if using the system namesever or 1 otherwise"
94+
complete -c doggo -l 'search' -d "Use the search list defined in resolv.conf. Defaults to true. Set --search=false to disable search list"
95+
complete -c doggo -l 'timeout' -d "Specify timeout (in seconds) for the resolver to return a response"
96+
complete -c doggo -s '-4' -l 'ipv4' -d "Use IPv4 only"
97+
complete -c doggo -s '-6' -l 'ipv6' -d "Use IPv6 only"
98+
99+
# Output options
100+
complete -c doggo -s 'J' -l 'json' -d "Format the output as JSON"
101+
complete -c doggo -l 'color' -d "Defaults to true. Set --color=false to disable colored output"
102+
complete -c doggo -l 'debug' -d "Enable debug logging"
103+
complete -c doggo -l 'time' -d "Shows how long the response took from the server"
104+
`
105+
)
106+
107+
func completionsCommand() {
108+
if len(os.Args) < 3 {
109+
fmt.Println("Usage: doggo completions [bash|zsh|fish]")
110+
os.Exit(1)
111+
}
112+
113+
shell := os.Args[2]
114+
switch shell {
115+
case "bash":
116+
fmt.Println(bashCompletion)
117+
case "zsh":
118+
fmt.Println(zshCompletion)
119+
case "fish":
120+
fmt.Println(fishCompletion)
121+
default:
122+
fmt.Printf("Unsupported shell: %s\n", shell)
123+
os.Exit(1)
124+
}
125+
}

0 commit comments

Comments
 (0)