mirror of
https://github.com/onyx-and-iris/exclude.git
synced 2026-04-16 14:13:39 +00:00
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:
26
cmd/reset.go
26
cmd/reset.go
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user