add env var support

add confirmation output for reset command
This commit is contained in:
2026-03-29 21:50:22 +01:00
parent 147f474986
commit 3d33857432
6 changed files with 66 additions and 15 deletions

View File

@@ -19,7 +19,7 @@ This is useful for starting fresh or removing all exclusions at once.`,
if !ok {
return fmt.Errorf("no exclude file found in context")
}
return resetAndWriteExcludeFile(ctx.File)
return resetAndWriteExcludeFile(ctx.Out, ctx.File)
},
}
@@ -31,7 +31,7 @@ func init() {
type truncater interface{ Truncate(size int64) error }
// resetAndWriteExcludeFile truncates and resets the file, then writes the default content
func resetAndWriteExcludeFile(f any) error {
func resetAndWriteExcludeFile(out io.Writer, f any) error {
// Try to assert to io.ReadWriteSeeker for file operations
rws, ok := f.(io.ReadWriteSeeker)
if !ok {
@@ -52,5 +52,10 @@ func resetAndWriteExcludeFile(f any) error {
if _, err := rws.Seek(0, 0); err != nil {
return fmt.Errorf("error seeking to the beginning of exclude file: %w", err)
}
return writeDefaultExcludeContent(rws)
err := writeDefaultExcludeContent(rws)
if err != nil {
return fmt.Errorf("error writing default exclude content: %w", err)
}
fmt.Fprintf(out, "Exclude file reset successfully.\n")
return nil
}

View File

@@ -9,7 +9,7 @@ import (
func TestRunResetCommand(t *testing.T) {
var buf bytes.Buffer
if err := resetAndWriteExcludeFile(&buf); err != nil {
if err := resetAndWriteExcludeFile(&buf, &buf); err != nil {
t.Fatalf("resetAndWriteExcludeFile failed: %v", err)
}

View File

@@ -4,8 +4,10 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const defaultExcludeFileContent = `# git ls-files --others --exclude-from=.git/info/exclude
@@ -30,12 +32,7 @@ by excluding certain files or directories from version control.`,
return fmt.Errorf("this command must be run in a Git repository")
}
path, err := cmd.Flags().GetString("path")
if err != nil {
return fmt.Errorf("error reading path flag: %w", err)
}
f, err := os.OpenFile(filepath.Join(path, "exclude"), os.O_RDWR|os.O_APPEND, 0644)
f, err := os.OpenFile(filepath.Join(viper.GetString("path"), "exclude"), os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
return fmt.Errorf("error opening exclude file: %w", err)
}
@@ -57,4 +54,11 @@ by excluding certain files or directories from version control.`,
func init() {
RootCmd.PersistentFlags().
StringP("path", "p", ".git/info/", "Path the exclude file resides in (default is .git/info/)")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.SetEnvPrefix("EXCLUDE")
viper.AutomaticEnv()
if err := viper.BindPFlag("path", RootCmd.PersistentFlags().Lookup("path")); err != nil {
panic(fmt.Errorf("unable to bind flags: %w", err))
}
}