Clean up code reuse, consistency, and efficiency issues

Merge readExistingSerial and readExistingContent into a single
readExisting function to eliminate duplicate file I/O. Extract dateBase
helper to deduplicate serial formula between defaultSerial and
bumpSerial. Cache hash results during collision check to avoid
recomputing per member. Normalize error prefixes (remove "error:" from
fmt.Errorf, add uniformly at print sites). Use filepath.Join instead of
manual "/" concatenation. Replace trivial containsStr wrapper with
strings.Contains. Simplify tokenize to a single return. Use writeTestFile
and fixedTime helpers consistently in tests.
This commit is contained in:
2026-03-01 17:38:26 -08:00
parent 1f2f39f40c
commit 0a460b975d
7 changed files with 75 additions and 117 deletions

View File

@@ -54,7 +54,7 @@ func parseLine(line, file string, lineNum int) (ZoneEntry, error) {
// Split on whitespace first, then handle comma separation within tokens
tokens := tokenize(line)
if len(tokens) < 2 {
return ZoneEntry{}, fmt.Errorf("error: %s:%d: expected zone name followed by at least one catalog name", file, lineNum)
return ZoneEntry{}, fmt.Errorf("%s:%d: expected zone name followed by at least one catalog name", file, lineNum)
}
entry := ZoneEntry{
@@ -69,16 +69,16 @@ func parseLine(line, file string, lineNum int) (ZoneEntry, error) {
switch strings.ToLower(key) {
case "group":
if value == "" {
return ZoneEntry{}, fmt.Errorf("error: %s:%d: empty group value", file, lineNum)
return ZoneEntry{}, fmt.Errorf("%s:%d: empty group value", file, lineNum)
}
entry.Group = value
case "coo":
if value == "" {
return ZoneEntry{}, fmt.Errorf("error: %s:%d: empty coo value", file, lineNum)
return ZoneEntry{}, fmt.Errorf("%s:%d: empty coo value", file, lineNum)
}
entry.COO = normalizeFQDN(value)
default:
return ZoneEntry{}, fmt.Errorf("error: %s:%d: unknown property %q", file, lineNum, key)
return ZoneEntry{}, fmt.Errorf("%s:%d: unknown property %q", file, lineNum, key)
}
} else {
// Bare name = catalog assignment
@@ -87,7 +87,7 @@ func parseLine(line, file string, lineNum int) (ZoneEntry, error) {
}
if len(entry.Catalogs) == 0 {
return ZoneEntry{}, fmt.Errorf("error: %s:%d: no catalog assignment for zone %s", file, lineNum, entry.Zone)
return ZoneEntry{}, fmt.Errorf("%s:%d: no catalog assignment for zone %s", file, lineNum, entry.Zone)
}
return entry, nil
@@ -95,10 +95,7 @@ func parseLine(line, file string, lineNum int) (ZoneEntry, error) {
// tokenize splits a line on whitespace and commas, stripping empty tokens.
func tokenize(line string) []string {
// Replace commas with spaces, then split on whitespace
line = strings.ReplaceAll(line, ",", " ")
fields := strings.Fields(line)
return fields
return strings.Fields(strings.ReplaceAll(line, ",", " "))
}
func buildCatalogMembers(entries []ZoneEntry, cfg *Config) (CatalogMembers, error) {
@@ -110,14 +107,14 @@ func buildCatalogMembers(entries []ZoneEntry, cfg *Config) (CatalogMembers, erro
for _, entry := range entries {
for _, catName := range entry.Catalogs {
if _, ok := cfg.Catalogs[catName]; !ok {
return nil, fmt.Errorf("error: %s:%d: unknown catalog %q", entry.File, entry.Line, catName)
return nil, fmt.Errorf("%s:%d: unknown catalog %q", entry.File, entry.Line, catName)
}
if seen[catName] == nil {
seen[catName] = make(map[string]int)
}
if prevLine, dup := seen[catName][entry.Zone]; dup {
return nil, fmt.Errorf("error: %s:%d: zone %s already assigned to catalog %q at line %d",
return nil, fmt.Errorf("%s:%d: zone %s already assigned to catalog %q at line %d",
entry.File, entry.Line, entry.Zone, catName, prevLine)
}
seen[catName][entry.Zone] = entry.Line