mirror of
https://github.com/onyx-and-iris/exclude.git
synced 2026-04-16 06:03:39 +00:00
remove the type assertions update the tests: the tests now check output as well as file contents separately.
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// resetCmd represents the reset command
|
|
var resetCmd = &cobra.Command{
|
|
Use: "reset",
|
|
Short: "Reset the exclude file",
|
|
Long: `The reset command clears all patterns from the .git/info/exclude file.
|
|
This is useful for starting fresh or removing all exclusions at once.`,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
ctx, ok := ContextObjectFromContext(cmd.Context())
|
|
if !ok {
|
|
return fmt.Errorf("no exclude file found in context")
|
|
}
|
|
return resetAndWriteExcludeFile(ctx.Out, ctx.File)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(resetCmd)
|
|
}
|
|
|
|
// resetAndWriteExcludeFile truncates and resets the file, then writes the default content
|
|
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 := f.Seek(0, 0); err != nil {
|
|
return fmt.Errorf("error seeking to the beginning of exclude file: %w", err)
|
|
}
|
|
if err := writeDefaultExcludeContent(f); err != nil {
|
|
return fmt.Errorf("error writing default exclude content: %w", err)
|
|
}
|
|
fmt.Fprintf(out, "Exclude file reset successfully.\n")
|
|
return nil
|
|
}
|