Commit d2f024c
authored
[Repo Assist] refactor: extract SortedSetKeys and atomicWriteFile to eliminate code duplication (#8042)
🤖 *This is an automated PR from Repo Assist.*
Addresses duplicate code patterns identified in #8021, specifically
sub-issues #8022 and #8023.
## What Changed
### 1. `strutil.SortedSetKeys` — new utility (closes #8022)
Added `SortedSetKeys(set map[string]struct{}) []string` to
`internal/strutil/strutil.go`. This eliminates the identical 5-line
map-to-sorted-slice block that appeared in three places:
- `internal/urlutil/domains.go` (×2: `ExtractURLDomainsFromValue`,
`ExtractURLDomains`)
- `internal/logger/observed_url_domains_logger.go` (×1: `writeToFile`)
### 2. `atomicWriteFile` — shared logger helper + bug fix (closes #8023)
Added `atomicWriteFile(filePath string, data []byte, perm os.FileMode)
error` to `internal/logger/common.go`. This helper:
- Writes data atomically via temp-file + rename
- Includes the `os.IsNotExist` guard on cleanup that was **already
present** in `observed_url_domains_logger.go` but **missing** in
`tools_logger.go` (latent bug fixed)
- Logs a WARNING on unexpected cleanup failures
Both `ToolsLogger.writeToFile` and
`ObservedURLDomainsLogger.writeToFile` now delegate to this helper.
## Root Cause
The `writeToFile` methods in `tools_logger.go` and
`observed_url_domains_logger.go` were written independently and diverged
in error handling. The `os.Remove` call in `tools_logger.go` silently
discarded cleanup errors, while `observed_url_domains_logger.go`
correctly guarded against `os.IsNotExist`. `atomicWriteFile` adopts the
correct variant for both callers.
## Impact
| Metric | Before | After |
|--------|--------|-------|
| Duplicated map→slice blocks | 3 | 0 |
| Duplicated atomic-write blocks | 2 | 0 |
| Lines removed (net) | — | −18 |
| Latent bug (missing IsNotExist guard) | present | fixed |
## Test Status
6 files changed
Lines changed: 62 additions & 42 deletions
File tree
- internal
- logger
- strutil
- urlutil
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
393 | 393 | | |
394 | 394 | | |
395 | 395 | | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
396 | 413 | | |
397 | 414 | | |
398 | 415 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
11 | 10 | | |
| 11 | + | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
117 | 118 | | |
118 | 119 | | |
119 | 120 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
| 121 | + | |
126 | 122 | | |
127 | 123 | | |
128 | 124 | | |
| |||
131 | 127 | | |
132 | 128 | | |
133 | 129 | | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
| 130 | + | |
145 | 131 | | |
146 | 132 | | |
147 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 115 | + | |
128 | 116 | | |
129 | 117 | | |
130 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
8 | 19 | | |
9 | 20 | | |
10 | 21 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 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 | + | |
9 | 35 | | |
10 | 36 | | |
11 | 37 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
| 29 | + | |
34 | 30 | | |
35 | 31 | | |
36 | 32 | | |
| |||
91 | 87 | | |
92 | 88 | | |
93 | 89 | | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
| 90 | + | |
99 | 91 | | |
100 | 92 | | |
101 | 93 | | |
0 commit comments