Skip to content

Commit e49aca4

Browse files
TMHSDigitalclaude
andcommitted
fix(cli): -silent hides the -retries warning; named exit codes; -resume= parsing (#166)
- the -retries deprecation warning now goes through out.Info after the writer exists, so -silent suppresses it (resolveAttempts is folded into effectiveAttempts) - run() returns exitOK/exitFailure/exitUsage instead of literals - resumeArgs uses strings.CutPrefix, stops at "--", does not read the state file path as a flag, and rejects an empty -resume= - the make tidy help line names go 1.26.0 tui.Start still returns literal 0/1; it lives in another package and is left as is. Fixes #166 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LpkeXSPwrzd7ZCuX3no7gD
1 parent 56769a5 commit e49aca4

5 files changed

Lines changed: 73 additions & 39 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ help:
129129
@echo " make test-short - Run short tests"
130130
@echo " make clean - Clean build artifacts"
131131
@echo " make lint - Run linter"
132-
@echo " make tidy - go mod tidy (keeps go 1.24.2)"
132+
@echo " make tidy - go mod tidy (keeps go 1.26.0)"
133133
@echo " make tui - Launch the interactive terminal UI (no arguments needed)"
134134
@echo ""
135135
@echo " LIVE MODE (performs real DNS queries):"

‎main.go‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,9 @@ func run() (code int) {
567567
if parseErr != nil {
568568
// The FlagSet has already printed the error and usage.
569569
if errors.Is(parseErr, flag.ErrHelp) {
570-
return 0
570+
return exitOK
571571
}
572-
return 2
572+
return exitUsage
573573
}
574574

575575
// -tui counts only as a real flag, not as the value of another flag
@@ -589,14 +589,17 @@ func run() (code int) {
589589
// -version answers before anything else prints, banners included (#58).
590590
if f.showVersion {
591591
fmt.Println(formatVersion())
592-
return 0
592+
return exitOK
593593
}
594594

595595
format, formatErr := output.ParseFormat(f.format)
596596
recordTypes, typesErr := dns.ParseTypes(f.recordTypes)
597-
maxAttempts, err := resolveAttempts(f.attempts, f.retries, defaults.source(fs, "attempts"), defaults.source(fs, "retries"))
597+
maxAttempts, fromRetries, err := effectiveAttempts(f.attempts, f.retries, defaults.source(fs, "attempts"), defaults.source(fs, "retries"))
598598
out := output.New(nil, f.testMode, format)
599599
out.SetQuiet(f.silent)
600+
if fromRetries {
601+
out.Info("Warning: -retries is deprecated, use -attempts instead") // quiet under -silent (#166)
602+
}
600603
if formatErr != nil {
601604
out.Error("%v", formatErr)
602605
return exitUsage
@@ -660,7 +663,7 @@ func run() (code int) {
660663

661664
targets, ok := loadTargets(f, domain, out)
662665
if !ok {
663-
return 1
666+
return exitFailure
664667
}
665668
if f.simZone != "" {
666669
stop, ok := startLabZone(&f, targets, out)
@@ -699,11 +702,11 @@ func run() (code int) {
699702
wordLines, err := readWordlist(f)
700703
if err != nil {
701704
out.Error("reading wordlist file: %v", err)
702-
return 1
705+
return exitFailure
703706
}
704707
if probe, _, _ := wordlist.Build(wordLines, ""); len(probe) == 0 {
705708
out.Error("wordlist %s has no valid entries", f.wordlistFile)
706-
return 1
709+
return exitFailure
707710
}
708711
if resume != nil {
709712
// The bundled wordlist changes with the binary, so the file digests
@@ -730,7 +733,7 @@ func run() (code int) {
730733

731734
combined, outFile, ok := openOutputFile(f.outputFile, f.testMode, format, out)
732735
if !ok {
733-
return 1
736+
return exitFailure
734737
}
735738
var outWriter *bufio.Writer
736739
if outFile != nil {
@@ -1181,20 +1184,11 @@ func scanTarget(ctx context.Context, f cliFlags, domain string, entries []string
11811184
return res
11821185
}
11831186

1184-
// resolveAttempts merges the -attempts and deprecated -retries flags. When
1187+
// effectiveAttempts merges the -attempts and deprecated -retries flags. When
11851188
// both are set, the one from the higher-precedence source wins (a flag over
11861189
// an env value over the config file); both at the same level is an error
1187-
// (#116).
1188-
func resolveAttempts(attempts, retries int, attemptsSrc, retriesSrc string) (int, error) {
1189-
n, fromRetries, err := effectiveAttempts(attempts, retries, attemptsSrc, retriesSrc)
1190-
if fromRetries {
1191-
fmt.Fprintln(os.Stderr, "Warning: -retries is deprecated, use -attempts instead")
1192-
}
1193-
return n, err
1194-
}
1195-
1196-
// effectiveAttempts is resolveAttempts without the deprecation warning. It
1197-
// also reports whether the value came from -retries.
1190+
// (#116). fromRetries reports that the value came from -retries, so the
1191+
// caller can print the deprecation warning.
11981192
func effectiveAttempts(attempts, retries int, attemptsSrc, retriesSrc string) (n int, fromRetries bool, err error) {
11991193
attemptsSet := attempts != 0
12001194
retriesSet := retries != 0

‎main_resume.go‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"slices"
1111
"sort"
1212
"strconv"
13+
"strings"
1314

1415
"github.com/TMHSDigital/subenum/internal/output"
1516
"github.com/TMHSDigital/subenum/internal/scan"
@@ -155,24 +156,35 @@ func (st *resumeState) checkSettings(fs *flag.FlagSet, d *defaultsLoader) error
155156
func resumeArgs(args []string) ([]string, *resumeState, error) {
156157
idx := -1
157158
path := ""
158-
for i, a := range args {
159-
switch {
160-
case a == "-resume" || a == "--resume":
161-
if i+1 < len(args) {
162-
idx, path = i, args[i+1]
163-
} else {
159+
scan:
160+
for i := 0; i < len(args); i++ {
161+
a := args[i]
162+
if a == "--" {
163+
break scan // everything after is a positional argument (#166)
164+
}
165+
if a == "-resume" || a == "--resume" {
166+
rest := args[i+1:]
167+
if len(rest) == 0 {
164168
return nil, nil, fmt.Errorf("-resume needs a state file")
165169
}
166-
case len(a) > 8 && (a[:8] == "-resume=" || (len(a) > 9 && a[:9] == "--resume=")):
167-
idx, path = i, a[len("-resume="):]
168-
if a[1] == '-' {
169-
path = a[len("--resume="):]
170-
}
170+
idx, path = i, rest[0]
171+
i++ // the state file is a value, not another flag
172+
continue
173+
}
174+
v, ok := strings.CutPrefix(a, "--resume=")
175+
if !ok {
176+
v, ok = strings.CutPrefix(a, "-resume=")
177+
}
178+
if ok {
179+
idx, path = i, v
171180
}
172181
}
173182
if idx < 0 {
174183
return args, nil, nil
175184
}
185+
if path == "" {
186+
return nil, nil, fmt.Errorf("-resume needs a state file")
187+
}
176188
if len(args) > 2 || (len(args) == 2 && path != args[1]) {
177189
return nil, nil, fmt.Errorf("-resume takes no other arguments; the state file holds the original ones")
178190
}

‎main_resume_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ func TestE2EResumePermute(t *testing.T) {
133133
}
134134
}
135135

136+
// TestResumeArgsParsing covers #166: an empty -resume= is an error, and
137+
// -resume after "--" is a positional argument, not the flag.
138+
func TestResumeArgsParsing(t *testing.T) {
139+
for _, args := range [][]string{{"-resume="}, {"--resume="}, {"-resume"}} {
140+
if _, _, err := resumeArgs(args); err == nil || !strings.Contains(err.Error(), "needs a state file") {
141+
t.Errorf("%q: err = %v, want \"needs a state file\"", args, err)
142+
}
143+
}
144+
args := []string{"-simulate", "--", "-resume"}
145+
got, st, err := resumeArgs(args)
146+
if err != nil || st != nil || len(got) != len(args) {
147+
t.Errorf("-resume after --: got %q, state %v, err %v; want the arguments unchanged", got, st, err)
148+
}
149+
path := writeState(t, resumeState{Args: []string{"-simulate", "example.com"}})
150+
for _, args := range [][]string{{"-resume", path}, {"--resume=" + path}} {
151+
if got, st, err := resumeArgs(args); err != nil || st == nil || len(got) != 2 {
152+
t.Errorf("%q: got %q, err %v", args, got, err)
153+
}
154+
}
155+
}
156+
136157
// TestE2EResumeCountsRefoundNamesOnce covers #156: the resume point lags the
137158
// saved results by up to a worker pool's worth of names, so a resumed scan
138159
// finds some of them again; they are counted once.

‎main_test.go‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,28 @@ func TestParseFlagsInvalidAfterDomain(t *testing.T) {
124124
fs.SetOutput(io.Discard)
125125
}
126126

127-
func TestResolveAttempts(t *testing.T) {
127+
func TestEffectiveAttempts(t *testing.T) {
128+
resolve := func(attempts, retries int, aSrc, rSrc string) (int, error) {
129+
n, _, err := effectiveAttempts(attempts, retries, aSrc, rSrc)
130+
return n, err
131+
}
132+
if _, fromRetries, _ := effectiveAttempts(0, 3, sourceDefault, sourceFlag); !fromRetries {
133+
t.Error("-retries=3: fromRetries is false, want the deprecation warning")
134+
}
128135
d, fl := sourceDefault, sourceFlag
129-
got, err := resolveAttempts(0, 0, d, d)
136+
got, err := resolve(0, 0, d, d)
130137
if err != nil || got != 1 {
131138
t.Errorf("default: got %d, err %v; want 1, nil", got, err)
132139
}
133-
got, err = resolveAttempts(5, 0, fl, d)
140+
got, err = resolve(5, 0, fl, d)
134141
if err != nil || got != 5 {
135142
t.Errorf("-attempts=5: got %d, err %v; want 5, nil", got, err)
136143
}
137-
got, err = resolveAttempts(0, 3, d, fl)
144+
got, err = resolve(0, 3, d, fl)
138145
if err != nil || got != 3 {
139146
t.Errorf("-retries=3: got %d, err %v; want 3, nil", got, err)
140147
}
141-
_, err = resolveAttempts(5, 3, fl, fl)
148+
_, err = resolve(5, 3, fl, fl)
142149
if err == nil {
143150
t.Error("both set: expected error, got nil")
144151
}
@@ -152,12 +159,12 @@ func TestResolveAttempts(t *testing.T) {
152159
{2, 2, sourceFlag, sourceEnv, 2},
153160
{4, 2, sourceEnv, sourceConfig, 4},
154161
} {
155-
got, err := resolveAttempts(c.attempts, c.retries, c.aSrc, c.rSrc)
162+
got, err := resolve(c.attempts, c.retries, c.aSrc, c.rSrc)
156163
if err != nil || got != c.want {
157164
t.Errorf("%+v: got %d, err %v; want %d", c, got, err, c.want)
158165
}
159166
}
160-
if _, err := resolveAttempts(3, 2, sourceEnv, sourceEnv); err == nil {
167+
if _, err := resolve(3, 2, sourceEnv, sourceEnv); err == nil {
161168
t.Error("both from env: expected error")
162169
}
163170
}

0 commit comments

Comments
 (0)