Skip to content

Commit eb0193f

Browse files
TMHSDigitalclaude
andcommitted
refactor: name each setting in one table across front ends (#160)
internal/scan/settings.go now holds one table giving every engine Setting its CLI flag (also the config file key and SUBENUM_* suffix), its pkg/subenum Config field and its TUI form label. FlagName, the new FieldName and Label, and the Settings list are all derived from it, so pkg/subenum's configFields map, the TUI's settingsWithoutField map and the FlagName switch are gone. TUI form fields with a setting take their label and flag from that table; the others carry their own flag. Each field points at its savedConfig slot, so initial values, and SUBENUM_*/config file seeding in withSettings, are derived from formFields instead of a separate 17-case switch that silently ignored unknown flags. New tests fail when a setting, flag or form field is added in one place but not the others: every setting has a unique flag, field and label; every CLI flag is either wired into the TUI or listed as deliberately omitted; every flag the TUI reads exists; and each flag seeds exactly its own field. Flag names, config keys, messages and TUI labels are unchanged. Not done here: collapsing the Options-to-Config copy and deriving completion metadata from flags (issue steps 2 and 4). Fixes #160 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LpkeXSPwrzd7ZCuX3no7gD
1 parent 6d7b539 commit eb0193f

9 files changed

Lines changed: 290 additions & 161 deletions

File tree

‎docs/ARCHITECTURE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal/scan/options.go - Options: user-facing settings shared by CLI and
5959
internal/scan/runner.go - Scan engine: Config, Event/EventKind/NoticeKind, Stats, Run
6060
internal/scan/dispatcher.go - dispatcher: lazy candidate generation, budgets, resume watermark
6161
internal/scan/scope.go - -exclude patterns (ValidateExcludes, IsExcluded)
62-
internal/scan/settings.go - Setting tokens in engine messages, rendered per front end (Render, FlagName)
62+
internal/scan/settings.go - Setting tokens and the one table naming each setting as flag, Config field and form label (Render, Names)
6363
internal/scan/verdict.go - Run-quality verdicts (Stats.Verdict, VerdictRank)
6464
internal/scan/verify.go - Verify: re-check names against the trusted resolver (used by -diff)
6565
internal/dns/resolver.go - NewResolver, ResolveTypes, ResolveDomainWithRetry, Classify,

‎internal/scan/settings.go‎

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
// Setting names a scan setting inside an engine message (#100). The engine
99
// has no idea whether it runs under the CLI (flags), the TUI (form fields) or
1010
// pkg/subenum (Config fields), so messages carry a setting as a {token}, and
11-
// each front end renders tokens in its own terms with Render.
11+
// each front end renders tokens in its own terms with Render, naming the
12+
// setting from settingTable.
1213
type Setting string
1314

1415
// Settings the engine refers to in messages.
@@ -26,10 +27,51 @@ const (
2627
SettingForce Setting = "force"
2728
)
2829

29-
// Settings lists every Setting, for front ends to check their name tables.
30-
var Settings = []Setting{
31-
SettingConcurrency, SettingTimeout, SettingAttempts, SettingHitRate, SettingDepth,
32-
SettingRate, SettingMaxQueries, SettingMaxDuration, SettingExclude, SettingRecursive, SettingForce,
30+
// SettingNames is how each front end names a Setting (#160).
31+
type SettingNames struct {
32+
Flag string // CLI flag without the dash; also the config file key and SUBENUM_* suffix
33+
Field string // pkg/subenum Config field
34+
Label string // TUI form label
35+
}
36+
37+
// settingTable is the one place a Setting is named. Adding a Setting means
38+
// adding a row here; tests in each front end check that the flag, Config
39+
// field and form row it names exist.
40+
var settingTable = []struct {
41+
Setting
42+
SettingNames
43+
}{
44+
{SettingConcurrency, SettingNames{"t", "Concurrency", "Concurrency"}},
45+
{SettingTimeout, SettingNames{"timeout", "Timeout", "Timeout (ms)"}},
46+
{SettingAttempts, SettingNames{"attempts", "Attempts", "Attempts"}},
47+
{SettingHitRate, SettingNames{"hit-rate", "HitRate", "Hit Rate (%)"}},
48+
{SettingDepth, SettingNames{"depth", "Depth", "Depth"}},
49+
{SettingRate, SettingNames{"rate", "Rate", "Rate (qps)"}},
50+
{SettingMaxQueries, SettingNames{"max-queries", "MaxQueries", "Max Names"}},
51+
{SettingMaxDuration, SettingNames{"max-duration", "MaxDuration", "Max Duration"}},
52+
{SettingExclude, SettingNames{"exclude", "Exclude", "Exclude"}},
53+
{SettingRecursive, SettingNames{"recursive", "Recursive", "Recursive"}},
54+
{SettingForce, SettingNames{"force", "Force", "Force"}},
55+
}
56+
57+
// Settings lists every Setting, in settingTable order.
58+
var Settings = func() []Setting {
59+
out := make([]Setting, len(settingTable))
60+
for i, row := range settingTable {
61+
out[i] = row.Setting
62+
}
63+
return out
64+
}()
65+
66+
// Names returns the setting's names, or zero names for a setting it does
67+
// not know.
68+
func (s Setting) Names() SettingNames {
69+
for _, row := range settingTable {
70+
if row.Setting == s {
71+
return row.SettingNames
72+
}
73+
}
74+
return SettingNames{}
3375
}
3476

3577
// Ref is the setting's token, to put in a message.
@@ -54,17 +96,14 @@ func Render(msg string, name func(Setting) string) string {
5496
// FlagName names a setting as the command-line flag that sets it, or ""
5597
// for a setting it does not know, which Render then leaves as a token.
5698
func FlagName(s Setting) string {
57-
switch s {
58-
case SettingConcurrency:
59-
return "-t"
60-
case SettingHitRate:
61-
return "-hit-rate"
62-
case SettingMaxQueries:
63-
return "-max-queries"
64-
case SettingMaxDuration:
65-
return "-max-duration"
66-
case SettingTimeout, SettingAttempts, SettingDepth, SettingRate, SettingExclude, SettingRecursive, SettingForce:
67-
return "-" + string(s)
99+
if f := s.Names().Flag; f != "" {
100+
return "-" + f
68101
}
69102
return ""
70103
}
104+
105+
// FieldName names a setting as the pkg/subenum Config field that sets it.
106+
func FieldName(s Setting) string { return s.Names().Field }
107+
108+
// Label names a setting as the TUI form field that sets it.
109+
func Label(s Setting) string { return s.Names().Label }

‎internal/scan/settings_test.go‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,28 @@ func TestRender(t *testing.T) {
1616
if got := Render("no tokens", FlagName); got != "no tokens" {
1717
t.Errorf("Render changed a message without tokens: %q", got)
1818
}
19+
}
20+
21+
// TestSettingTable covers #160: every setting has a flag, a Config field and
22+
// a form label, and no two settings share one.
23+
func TestSettingTable(t *testing.T) {
24+
seen := map[string]Setting{}
1925
for _, s := range Settings {
20-
if FlagName(s) == "" {
21-
t.Errorf("setting %q has no flag name", s)
26+
n := s.Names()
27+
for kind, name := range map[string]string{"flag": n.Flag, "field": n.Field, "label": n.Label} {
28+
if name == "" {
29+
t.Errorf("setting %q has no %s", s, kind)
30+
continue
31+
}
32+
if prev, dup := seen[kind+":"+name]; dup {
33+
t.Errorf("settings %q and %q share the %s %q", prev, s, kind, name)
34+
}
35+
seen[kind+":"+name] = s
2236
}
2337
}
38+
if n := Setting("unknown").Names(); n != (SettingNames{}) || FlagName("unknown") != "" {
39+
t.Errorf("unknown setting has names %+v", n)
40+
}
2441
}
2542

2643
// TestEngineMessagesUseSettingTokens covers #100: no string literal in the

‎internal/tui/config.go‎

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"os"
66
"path/filepath"
7-
"strconv"
87
"strings"
98
)
109

@@ -106,47 +105,26 @@ func saveConfig(fv formValues) error {
106105
}
107106

108107
// withSettings overlays flag values from SUBENUM_* variables or the config
109-
// file (keyed by flag name) on the remembered form values (#118).
108+
// file (keyed by flag name) on the remembered form values (#118), using each
109+
// form field's flag (#160).
110110
func withSettings(sc savedConfig, settings map[string]string) savedConfig {
111-
for name, v := range settings {
112-
n, _ := strconv.Atoi(v)
113-
b, _ := strconv.ParseBool(v)
114-
switch name {
115-
case "w":
116-
sc.Wordlist = v
117-
case "dns-server":
118-
sc.DNSServer = v
119-
case "t":
120-
sc.Concurrency = n
121-
case "timeout":
122-
sc.TimeoutMs = n
123-
case "attempts":
124-
sc.Attempts = n
125-
case "hit-rate":
126-
sc.HitRate = n
127-
case "type":
128-
sc.Types = v
129-
case "depth":
130-
sc.Depth = n
131-
case "rate":
132-
sc.Rate = n
133-
case "max-queries":
134-
sc.MaxQueries = n
135-
case "exclude":
136-
sc.Exclude = v
137-
case "o":
138-
sc.Output = v
139-
case "format":
140-
sc.Format = v
141-
case "simulate":
142-
sc.Simulate = b
143-
case "force":
144-
sc.Force = b
145-
case "recursive":
146-
sc.Recursive = b
147-
case "no-abort":
148-
sc.NoAbort = b
111+
for _, spec := range formFields {
112+
if v, ok := settings[spec.flagName()]; ok && spec.flagName() != "" {
113+
spec.set(&sc, v)
149114
}
150115
}
151116
return sc
152117
}
118+
119+
// Flags lists the flags whose SUBENUM_* or config file value seeds the form,
120+
// so the CLI can check that every flag is either wired here or deliberately
121+
// left out of the TUI.
122+
func Flags() []string {
123+
var out []string
124+
for _, spec := range formFields {
125+
if f := spec.flagName(); f != "" {
126+
out = append(out, f)
127+
}
128+
}
129+
return out
130+
}

0 commit comments

Comments
 (0)