simplify del/reset commands by using readWriteTruncater interface.

remove the type assertions

update the tests:
the tests now check output as well as file contents separately.
This commit is contained in:
2026-04-07 01:22:56 +01:00
parent c2eab8576e
commit a149ce347b
7 changed files with 86 additions and 89 deletions

View File

@@ -27,33 +27,15 @@ func init() {
RootCmd.AddCommand(resetCmd)
}
// Truncate and seek to beginning
type truncater interface{ Truncate(size int64) error }
// resetAndWriteExcludeFile truncates and resets the file, then writes the default content
func resetAndWriteExcludeFile(out io.Writer, f any) error {
// Try to assert to io.ReadWriteSeeker for file operations
rws, ok := f.(io.ReadWriteSeeker)
if !ok {
// If not a file, try as io.Writer (for test buffers)
if w, ok := f.(io.Writer); ok {
return writeDefaultExcludeContent(w)
}
return fmt.Errorf("provided file does not support ReadWriteSeeker or Writer")
}
t, ok := f.(truncater)
if !ok {
return fmt.Errorf("provided file does not support Truncate")
}
if err := t.Truncate(0); err != nil {
func resetAndWriteExcludeFile(out io.Writer, f readWriteTruncater) error {
if err := f.Truncate(0); err != nil {
return fmt.Errorf("error truncating exclude file: %w", err)
}
if _, err := rws.Seek(0, 0); err != nil {
if _, err := f.Seek(0, 0); err != nil {
return fmt.Errorf("error seeking to the beginning of exclude file: %w", err)
}
err := writeDefaultExcludeContent(rws)
if err != nil {
if err := writeDefaultExcludeContent(f); err != nil {
return fmt.Errorf("error writing default exclude content: %w", err)
}
fmt.Fprintf(out, "Exclude file reset successfully.\n")