Repository navigation
Expand file tree
/
Copy pathcompletion.go
More file actions
146 lines (129 loc) · 4.58 KB
/
Copy pathcompletion.go
File metadata and controls
146 lines (129 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"io"
"os"
"strings"
)
// ---- shell completion --------------------------------------------------------
// The lists below are the single source of truth for both shell scripts: when
// a subcommand or flag is added (to main's dispatch, parseCLI, or runCert),
// extend the matching slice here and every shell picks it up.
var (
completionSubcommands = []string{"cert", "curl", "completion", "version", "help"}
completionMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
// Flags of a plain `weeb [METHOD] URL …` invocation (see parseCLI).
completionMainFlags = []string{
"-H", "--header", "-d", "--data", "-X", "--request", "--timeout",
"-v", "--stats", "--pretty", "--raw", "--no-tui", "--headless",
"-q", "--quiet", "--persona", "--to-curl", "-o", "--output",
"--max-body", "-h", "--help",
}
// Flags accepted after `weeb cert` (see runCert).
completionCertFlags = []string{
"-k", "--insecure", "--json", "--pem", "--showcerts", "--brief",
"--short", "--sni", "--servername", "--starttls", "--alpn", "--tls",
"--client-cert", "--cert", "--client-key", "--key", "--timeout",
"--persona",
}
completionPersonas = []string{"plain", "dere", "tsun", "yan"}
)
// runCompletion handles `weeb completion zsh|bash`: print a static completion
// script to w (stdout in main), ready to eval (bash) or drop in $fpath (zsh).
func runCompletion(args []string, w io.Writer) int {
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "weeb: completion needs a shell: weeb completion zsh|bash")
return 2
}
switch args[0] {
case "zsh":
fmt.Fprint(w, zshCompletion())
return 0
case "bash":
fmt.Fprint(w, bashCompletion())
return 0
default:
fmt.Fprintf(os.Stderr, "weeb: unsupported shell %q: weeb completion zsh|bash\n", args[0])
return 2
}
}
// zshCompletion renders the zsh script with the shared lists spliced in.
func zshCompletion() string {
return fmt.Sprintf(`#compdef weeb
# zsh completion for weeb — generated by 'weeb completion zsh'.
# The command/flag lists are emitted from completion.go; update them there.
_weeb() {
local -a subcmds methods mainflags certflags personas
subcmds=(%[1]s)
methods=(%[2]s)
mainflags=(%[3]s)
certflags=(%[4]s)
personas=(%[5]s)
# Flags whose value we can complete.
case "${words[CURRENT-1]}" in
--persona) compadd -a personas; return ;;
-X|--request) compadd -a methods; return ;;
-o|--output|-d|--data|--client-cert|--cert|--client-key|--key) _files; return ;;
esac
# First word: a subcommand, a method, or straight to flags/URL.
if (( CURRENT == 2 )); then
compadd -a subcmds methods mainflags
return
fi
case "${words[2]}" in
cert|tls) _arguments $certflags '*:host:_hosts' ;;
completion) (( CURRENT == 3 )) && compadd zsh bash ;;
curl|import|version|help) ;;
*) _arguments $mainflags '*:url:_urls' ;;
esac
}
_weeb "$@"
`,
strings.Join(completionSubcommands, " "),
strings.Join(completionMethods, " "),
strings.Join(completionMainFlags, " "),
strings.Join(completionCertFlags, " "),
strings.Join(completionPersonas, " "),
)
}
// bashCompletion renders the bash script with the shared lists spliced in.
func bashCompletion() string {
return fmt.Sprintf(`# bash completion for weeb — generated by 'weeb completion bash'.
# The command/flag lists are emitted from completion.go; update them there.
_weeb() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
local subcmds="%[1]s"
local methods="%[2]s"
local mainflags="%[3]s"
local certflags="%[4]s"
local personas="%[5]s"
# Flags whose value we can complete.
case "$prev" in
--persona) COMPREPLY=($(compgen -W "$personas" -- "$cur")); return ;;
-X|--request) COMPREPLY=($(compgen -W "$methods" -- "$cur")); return ;;
-o|--output|-d|--data|--client-cert|--cert|--client-key|--key)
COMPREPLY=($(compgen -f -- "$cur")); return ;;
esac
# First word: a subcommand, a method, or straight to flags/URL.
if (( COMP_CWORD == 1 )); then
COMPREPLY=($(compgen -W "$subcmds $methods $mainflags" -- "$cur"))
return
fi
case "${COMP_WORDS[1]}" in
cert|tls) COMPREPLY=($(compgen -W "$certflags" -- "$cur")) ;;
completion) COMPREPLY=($(compgen -W "zsh bash" -- "$cur")) ;;
curl|import|version|help) ;;
*) COMPREPLY=($(compgen -W "$mainflags" -- "$cur")) ;;
esac
}
complete -F _weeb weeb
`,
strings.Join(completionSubcommands, " "),
strings.Join(completionMethods, " "),
strings.Join(completionMainFlags, " "),
strings.Join(completionCertFlags, " "),
strings.Join(completionPersonas, " "),
)
}