Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ Flags and a domain given with `-tui` fill the form, so a prepared command line o
| `/` | 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 |
| `t` | Show takeover candidates only (toggle) |
| `v` | Show each result's records under it (toggle) |
| `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 |
| `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 |
| `esc` | Clear the filter |

</details>
Expand Down
9 changes: 9 additions & 0 deletions internal/tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math/rand/v2"
"os"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -356,6 +357,7 @@ func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) {
case "s":
if m.scanView.done {
m.scanView.saving = true
m.scanView.overwritePath = ""
if m.scanView.savePath == "" {
m.scanView.savePath = m.defaultSavePath()
}
Expand Down Expand Up @@ -561,6 +563,13 @@ func saveFormat(path string, fallback output.Format) output.Format {
// leaves an existing file unchanged and does not change the exit code.
func (m *Model) saveResults(path string) {
sv := &m.scanView
if _, err := os.Lstat(path); err == nil && sv.overwritePath != path {
// The viewport may be filtered or have discarded earlier results.
// Never replace an existing file on the first Enter (#360).
sv.saving, sv.overwritePath = true, path
return
}
sv.overwritePath = ""
results := sv.toSave()
format := saveFormat(path, m.vals.format)
err := func() error {
Expand Down
79 changes: 79 additions & 0 deletions internal/tui/save_confirmation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package tui

import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"

tea "charm.land/bubbletea/v2"

"github.com/TMHSDigital/subenum/internal/output"
)

func TestTUISaveConfirmsExistingFiles(t *testing.T) {
for _, target := range []string{"scan output", "another file"} {
t.Run(target, func(t *testing.T) {
redirectConfig(t)
path := filepath.Join(t.TempDir(), "out.json")
m := runTUIModel(t, path, output.FormatJSON, []string{"A"})
press := func(keys ...tea.KeyPressMsg) {
t.Helper()
for _, key := range keys {
next, _ := m.updateScan(key)
m = next.(Model)
}
}
press(typeText("/"), typeText("www"), keyEnter, typeText("s"))
if target == "another file" {
path = filepath.Join(t.TempDir(), "existing.json")
if err := os.WriteFile(path, []byte("KEEP EXISTING FILE"), 0o600); err != nil {
t.Fatal(err)
}
for range []rune(m.scanView.savePath) {
press(keyBackspace)
}
press(typeText(path))
}
before, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
unchanged := func() {
t.Helper()
got, err := os.ReadFile(path)
if err != nil || string(got) != string(before) {
t.Fatalf("save changed the existing file before confirmation: %q, err %v", got, err)
}
}
press(keyEnter)
unchanged()
if !m.scanView.saving || !strings.Contains(m.scanView.doneHint(), "overwrite") {
t.Fatalf("missing overwrite confirmation: %s", m.scanView.doneHint())
}

// Editing and restoring the same path cancels the previous
// confirmation; escape and reopening the prompt do so as well.
press(keyBackspace, typeText("n"), keyEnter)
unchanged()
press(keyEsc, typeText("s"), keyEnter)
unchanged()
press(keyEnter)
if m.scanView.saving {
t.Fatal("confirmed save left the prompt open")
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var results []output.Result
if err := json.Unmarshal(data, &results); err != nil {
t.Fatal(err)
}
if len(results) != 1 || results[0].Subdomain != "www.example.com" {
t.Fatalf("confirmed save should contain the filtered result: %s", data)
}
})
}
}
9 changes: 9 additions & 0 deletions internal/tui/scan_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ type scanViewModel struct {
records bool
saving bool
savePath string
// An existing destination needs a second Enter. Editing or closing the
// prompt clears this path so confirmation cannot apply to another save.
overwritePath string

// The permutation pass (#274). permute says one follows the main pass;
// main is the main pass's last event once it has finished, and perm the
Expand Down Expand Up @@ -279,6 +282,9 @@ func (m scanViewModel) filterHint() string {
// it is open (#332).
func (m scanViewModel) doneHint() string {
if m.saving {
if m.overwritePath != "" {
return m.hint(" file exists: enter overwrite • esc cancel • " + m.savePath + "█")
}
return m.hint(" save to: " + m.savePath + "█ (enter save • esc cancel)")
}
return m.hint(" r new scan • s save • q quit • " + m.filterHint())
Expand All @@ -287,6 +293,9 @@ func (m scanViewModel) doneHint() string {
// handleSaveKey edits the save prompt's path. It reports whether the user
// pressed enter to save; esc closes the prompt.
func (m *scanViewModel) handleSaveKey(msg tea.KeyPressMsg) (save bool) {
if msg.Code != tea.KeyEnter {
m.overwritePath = ""
}
switch {
case msg.Code == tea.KeyEnter:
m.saving = false
Expand Down
Loading