Skip to content

Commit 3a15fee

Browse files
TMHSDigitalclaude
andcommitted
fix(cli): complete file flags in every shell; context-aware PowerShell
- fileFlags gains simulate-zone, seeds, resume and state (zsh offered nothing, fish disabled file completion); a test fails when a flag whose usage mentions a file is missing. - bash and fish complete shell names after `completion`. - The PowerShell completer looks at the previous word: file and value flags fall back to path completion, -format/-type/completion offer their values, subcommands only in first position. - CI syntax-checks every generated script with bash, zsh, fish and pwsh. Fixes #117 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ef5cc03 commit 3a15fee

4 files changed

Lines changed: 83 additions & 6 deletions

File tree

‎.github/workflows/go.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ jobs:
6969
- name: Test
7070
run: go test -v -race ./...
7171

72+
# Every generated completion script must parse in the shell it targets
73+
# (#117); pwsh is preinstalled on the runner.
74+
- name: Check completion scripts
75+
if: matrix.go == 'stable'
76+
run: |
77+
sudo apt-get update -qq
78+
sudo apt-get install -y -qq --no-install-recommends zsh fish
79+
dir=$(mktemp -d)
80+
go run . generate-assets "$dir"
81+
bash -n "$dir/completions/subenum.bash"
82+
zsh -n "$dir/completions/_subenum"
83+
fish --no-execute "$dir/completions/subenum.fish"
84+
pwsh -NoProfile -Command "\$null = [ScriptBlock]::Create((Get-Content -Raw '$dir/completions/subenum.ps1'))"
85+
7286
# Lint on the stable leg: golangci-lint must be built with a Go at least as
7387
# new as the toolchain it analyses (v2.12 could not type-check Go 1.27).
7488
- name: Run golangci-lint

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
- The minimum Go version is now 1.26 (`go 1.26.0` in go.mod), required by current `golang.org/x/sys` and `golang.org/x/text`; both were bumped and `govulncheck -show verbose` now reports no vulnerable modules. Dependabot also updates indirect modules, and CI lints with errorlint, nilerr, contextcheck, noctx, copyloopvar, usestdlibvars and revive, with errcheck exemptions narrowed to stdout prints (#83).
3131

3232
### Fixed
33+
- Shell completions: `-simulate-zone`, `-seeds`, `-resume` and `-state` complete file names in every shell (fish previously disabled file completion for them), bash and fish complete the shell name after `completion`, and the PowerShell completer reads the previous word, so file flags fall back to path completion and `-format`/`-type` offer their values. A test fails when a flag that takes a file is missing from the list, and CI checks that every generated script parses in its shell (#117).
3334
- New `-o` and `-stats` files get 0666 less the umask, as `os.Create` would, instead of a fixed 0644, so results stay private under `umask 077`; an `-o` path that is a symlink keeps the link and replaces the file it points at, instead of turning into a regular file (#119).
3435
- Takeover hints resolve each CNAME target once per scan instead of once per result, so hundreds of names aliased to the same CDN or SaaS host no longer spend a query and a `-rate` slot each; concurrent results for one target share the lookup, and an inconclusive one is retried by the next name (#121).
3536
- DoT connection reuse is accurate: a pooled connection the server already closed is detected before reuse instead of failing a query that had already been counted and paced (a server closing after each answer cost 19 dials for 10 lookups; now 10). Up to 128 connections are kept, matching the default `-t`, and a scan closes its idle DoT and DoH connections when it ends, so a program that scans repeatedly through `pkg/subenum` does not hold them open (#113).

‎main_completion.go‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ import (
1515
// (#88). `subenum completion <shell>` and `subenum man` print them;
1616
// release builds ship them via `subenum generate-assets <dir>`.
1717

18-
// fileFlags take a path, so shells should complete file names for them.
19-
var fileFlags = map[string]bool{"w": true, "dL": true, "o": true, "stats": true, "diff": true, "r": true, "exclude-file": true}
18+
// fileFlags take a path, so shells should complete file names for them. A
19+
// test fails when a flag whose usage mentions a file is missing here (#117).
20+
var fileFlags = map[string]bool{
21+
"w": true, "dL": true, "o": true, "stats": true, "diff": true, "r": true, "exclude-file": true,
22+
"simulate-zone": true, "seeds": true, "resume": true, "state": true,
23+
}
24+
25+
// completionShells are the shells `subenum completion` writes scripts for.
26+
var completionShells = []string{"bash", "zsh", "fish", "powershell"}
2027

2128
// valueChoices lists the fixed values some flags accept.
2229
var valueChoices = map[string][]string{
@@ -115,6 +122,7 @@ func writeBash(w io.Writer) {
115122
p(" local cur=\"${COMP_WORDS[COMP_CWORD]}\" prev=\"${COMP_WORDS[COMP_CWORD-1]}\"\n")
116123
p(" case \"$prev\" in\n")
117124
p(" %s) COMPREPLY=( $(compgen -f -- \"$cur\") ); return ;;\n", strings.Join(files, "|"))
125+
p(" completion) [[ $COMP_CWORD -eq 2 ]] && COMPREPLY=( $(compgen -W %q -- \"$cur\") ); return ;;\n", strings.Join(completionShells, " "))
118126
for _, name := range sortedKeys(valueChoices) {
119127
p(" -%s) COMPREPLY=( $(compgen -W %q -- \"$cur\") ); return ;;\n", name, strings.Join(valueChoices[name], " "))
120128
}
@@ -154,6 +162,7 @@ func writeFish(w io.Writer) {
154162
p := func(format string, a ...any) { _, _ = fmt.Fprintf(w, format, a...) }
155163
p("# fish completion for subenum. Generated by `subenum completion fish`.\n")
156164
p("complete -c subenum -f -n '__fish_use_subcommand' -a '%s'\n", strings.Join(subcommands, " "))
165+
p("complete -c subenum -f -n '__fish_seen_subcommand_from completion' -a '%s'\n", strings.Join(completionShells, " "))
157166
for _, f := range flagInfos() {
158167
line := "complete -c subenum -o " + f.name
159168
switch {
@@ -169,17 +178,53 @@ func writeFish(w io.Writer) {
169178
}
170179
}
171180

181+
// writePowerShell completes by the previous word: a file flag returns
182+
// nothing so PowerShell falls back to path completion, a flag with fixed
183+
// values offers them, and otherwise flags (and, first, subcommands) are
184+
// offered (#117).
172185
func writePowerShell(w io.Writer) {
173-
var items []string
186+
quote := func(ss []string) string {
187+
q := make([]string, len(ss))
188+
for i, s := range ss {
189+
q[i] = "'" + strings.ReplaceAll(s, "'", "''") + "'"
190+
}
191+
return "@(" + strings.Join(q, ", ") + ")"
192+
}
193+
var flags, files, valued []string
174194
for _, f := range flagInfos() {
175-
items = append(items, "'-"+f.name+"'")
195+
flags = append(flags, "-"+f.name)
196+
switch {
197+
case fileFlags[f.name]:
198+
files = append(files, "-"+f.name)
199+
case !f.isBool && valueChoices[f.name] == nil:
200+
valued = append(valued, "-"+f.name)
201+
}
176202
}
177203
p := func(format string, a ...any) { _, _ = fmt.Fprintf(w, format, a...) }
178204
p("# PowerShell completion for subenum. Generated by `subenum completion powershell`.\n")
179205
p("Register-ArgumentCompleter -Native -CommandName subenum -ScriptBlock {\n")
180206
p(" param($wordToComplete, $commandAst, $cursorPosition)\n")
181-
p(" $flags = @(%s)\n", strings.Join(items, ", "))
182-
p(" $flags + @('%s') | Where-Object { $_ -like \"$wordToComplete*\" } | ForEach-Object {\n", strings.Join(subcommands, "', '"))
207+
p(" $words = @($commandAst.CommandElements | Where-Object { $_.Extent.EndOffset -lt $cursorPosition } | ForEach-Object { $_.ToString() })\n")
208+
p(" $prev = if ($words.Count -gt 0) { $words[-1] } else { '' }\n")
209+
p(" $fileFlags = %s\n", quote(files))
210+
p(" $valueFlags = %s\n", quote(valued))
211+
p(" if ($fileFlags -contains $prev -or $valueFlags -contains $prev) { return }\n")
212+
p(" $values = $null\n")
213+
p(" switch ($prev) {\n")
214+
for _, name := range sortedKeys(valueChoices) {
215+
p(" '-%s' { $values = %s }\n", name, quote(valueChoices[name]))
216+
}
217+
p(" 'completion' { if ($words.Count -eq 2) { $values = %s } }\n", quote(completionShells))
218+
p(" }\n")
219+
p(" if ($null -ne $values) {\n")
220+
p(" $values | Where-Object { $_ -like \"$wordToComplete*\" } | ForEach-Object {\n")
221+
p(" [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)\n")
222+
p(" }\n")
223+
p(" return\n")
224+
p(" }\n")
225+
p(" $candidates = %s\n", quote(flags))
226+
p(" if ($words.Count -le 1) { $candidates += %s }\n", quote(subcommands))
227+
p(" $candidates | Where-Object { $_ -like \"$wordToComplete*\" } | ForEach-Object {\n")
183228
p(" [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterName', $_)\n")
184229
p(" }\n")
185230
p("}\n")

‎main_config_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"os"
77
"path/filepath"
8+
"slices"
89
"strings"
910
"testing"
1011
)
@@ -197,3 +198,19 @@ func TestE2EAttemptsPrecedence(t *testing.T) {
197198
t.Errorf("SUBENUM_RETRIES + -attempts: exit %d\n%s", code, out)
198199
}
199200
}
201+
202+
// TestFileFlagsAreComplete covers #117: every flag that takes a path is in
203+
// fileFlags, so shells complete file names for it. A new flag whose usage
204+
// mentions a file fails here until it is added.
205+
func TestFileFlagsAreComplete(t *testing.T) {
206+
for _, f := range flagInfos() {
207+
if strings.Contains(strings.ToLower(f.usage), "file") && !f.isBool && !fileFlags[f.name] {
208+
t.Errorf("-%s takes a path (%q) but is not in fileFlags", f.name, f.usage)
209+
}
210+
}
211+
for name := range fileFlags {
212+
if !slices.ContainsFunc(flagInfos(), func(f flagInfo) bool { return f.name == name }) {
213+
t.Errorf("fileFlags lists -%s, which is not a flag", name)
214+
}
215+
}
216+
}

0 commit comments

Comments
 (0)