Skip to content

Commit b1131cc

Browse files
committed
fix(tui): confirm overwriting saved results
1 parent 0f3b0b6 commit b1131cc

4 files changed

Lines changed: 98 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ Flags and a domain given with `-tui` fill the form, so a prepared command line o
649649
| `/` | Filter the results by name (during or after a scan); `enter` keeps the filter, `esc` clears it. While typing, every key but `ctrl+c` goes to the filter |
650650
| `t` | Show takeover candidates only (toggle) |
651651
| `v` | Show each result's records under it (toggle) |
652-
| `s` | Save the results after the scan: type a path and press `enter` (`esc` cancels). The extension picks the format (`.txt`, `.json`, `.jsonl`, `.csv`; otherwise the form's Format), and with a filter on only the results it shows are saved |
652+
| `s` | Save the results after the scan: type a path and press `enter` (`esc` cancels). An existing file requires a second `enter` to confirm overwriting. The extension picks the format (`.txt`, `.json`, `.jsonl`, `.csv`; otherwise the form's Format), and with a filter on only the results it shows are saved |
653653
| `esc` | Clear the filter |
654654

655655
</details>

‎internal/tui/model.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"math/rand/v2"
9+
"os"
910
"path/filepath"
1011
"strings"
1112
"time"
@@ -356,6 +357,7 @@ func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) {
356357
case "s":
357358
if m.scanView.done {
358359
m.scanView.saving = true
360+
m.scanView.overwritePath = ""
359361
if m.scanView.savePath == "" {
360362
m.scanView.savePath = m.defaultSavePath()
361363
}
@@ -561,6 +563,13 @@ func saveFormat(path string, fallback output.Format) output.Format {
561563
// leaves an existing file unchanged and does not change the exit code.
562564
func (m *Model) saveResults(path string) {
563565
sv := &m.scanView
566+
if _, err := os.Lstat(path); err == nil && sv.overwritePath != path {
567+
// The viewport may be filtered or have discarded earlier results.
568+
// Never replace an existing file on the first Enter (#360).
569+
sv.saving, sv.overwritePath = true, path
570+
return
571+
}
572+
sv.overwritePath = ""
564573
results := sv.toSave()
565574
format := saveFormat(path, m.vals.format)
566575
err := func() error {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package tui
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
10+
tea "charm.land/bubbletea/v2"
11+
12+
"github.com/TMHSDigital/subenum/internal/output"
13+
)
14+
15+
func TestTUISaveConfirmsExistingFiles(t *testing.T) {
16+
for _, target := range []string{"scan output", "another file"} {
17+
t.Run(target, func(t *testing.T) {
18+
redirectConfig(t)
19+
path := filepath.Join(t.TempDir(), "out.json")
20+
m := runTUIModel(t, path, output.FormatJSON, []string{"A"})
21+
press := func(keys ...tea.KeyPressMsg) {
22+
t.Helper()
23+
for _, key := range keys {
24+
next, _ := m.updateScan(key)
25+
m = next.(Model)
26+
}
27+
}
28+
press(typeText("/"), typeText("www"), keyEnter, typeText("s"))
29+
if target == "another file" {
30+
path = filepath.Join(t.TempDir(), "existing.json")
31+
if err := os.WriteFile(path, []byte("KEEP EXISTING FILE"), 0o600); err != nil {
32+
t.Fatal(err)
33+
}
34+
for range []rune(m.scanView.savePath) {
35+
press(keyBackspace)
36+
}
37+
press(typeText(path))
38+
}
39+
before, err := os.ReadFile(path)
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
unchanged := func() {
44+
t.Helper()
45+
got, err := os.ReadFile(path)
46+
if err != nil || string(got) != string(before) {
47+
t.Fatalf("save changed the existing file before confirmation: %q, err %v", got, err)
48+
}
49+
}
50+
press(keyEnter)
51+
unchanged()
52+
if !m.scanView.saving || !strings.Contains(m.scanView.doneHint(), "overwrite") {
53+
t.Fatalf("missing overwrite confirmation: %s", m.scanView.doneHint())
54+
}
55+
56+
// Editing and restoring the same path cancels the previous
57+
// confirmation; escape and reopening the prompt do so as well.
58+
press(keyBackspace, typeText("n"), keyEnter)
59+
unchanged()
60+
press(keyEsc, typeText("s"), keyEnter)
61+
unchanged()
62+
press(keyEnter)
63+
if m.scanView.saving {
64+
t.Fatal("confirmed save left the prompt open")
65+
}
66+
data, err := os.ReadFile(path)
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
var results []output.Result
71+
if err := json.Unmarshal(data, &results); err != nil {
72+
t.Fatal(err)
73+
}
74+
if len(results) != 1 || results[0].Subdomain != "www.example.com" {
75+
t.Fatalf("confirmed save should contain the filtered result: %s", data)
76+
}
77+
})
78+
}
79+
}

‎internal/tui/scan_view.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ type scanViewModel struct {
8787
records bool
8888
saving bool
8989
savePath string
90+
// An existing destination needs a second Enter. Editing or closing the
91+
// prompt clears this path so confirmation cannot apply to another save.
92+
overwritePath string
9093

9194
// The permutation pass (#274). permute says one follows the main pass;
9295
// main is the main pass's last event once it has finished, and perm the
@@ -279,6 +282,9 @@ func (m scanViewModel) filterHint() string {
279282
// it is open (#332).
280283
func (m scanViewModel) doneHint() string {
281284
if m.saving {
285+
if m.overwritePath != "" {
286+
return m.hint(" file exists: enter overwrite • esc cancel • " + m.savePath + "█")
287+
}
282288
return m.hint(" save to: " + m.savePath + "█ (enter save • esc cancel)")
283289
}
284290
return m.hint(" r new scan • s save • q quit • " + m.filterHint())
@@ -287,6 +293,9 @@ func (m scanViewModel) doneHint() string {
287293
// handleSaveKey edits the save prompt's path. It reports whether the user
288294
// pressed enter to save; esc closes the prompt.
289295
func (m *scanViewModel) handleSaveKey(msg tea.KeyPressMsg) (save bool) {
296+
if msg.Code != tea.KeyEnter {
297+
m.overwritePath = ""
298+
}
290299
switch {
291300
case msg.Code == tea.KeyEnter:
292301
m.saving = false

0 commit comments

Comments
 (0)