Skip to content

Commit

Permalink
fix: Make re-add command respect --interactive flag
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 14, 2022
1 parent b6039e7 commit ca8142f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/cmd/readdcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"fmt"
"io/fs"
"sort"

Expand Down Expand Up @@ -49,6 +50,7 @@ func (c *Config) runReAddCmd(cmd *cobra.Command, args []string, sourceState *che
})
sort.Sort(targetRelPaths)

TARGETRELPATH:
for _, targetRelPath := range targetRelPaths {
sourceStateFile, ok := sourceStateEntries[targetRelPath].(*chezmoi.SourceStateFile)
if !ok {
Expand Down Expand Up @@ -93,6 +95,41 @@ func (c *Config) runReAddCmd(cmd *cobra.Command, args []string, sourceState *che
continue
}

if c.interactive {
prompt := fmt.Sprintf("Re-add %s", targetRelPath)
var choices []string
if actualContents != nil || targetContents != nil {
choices = append(choices, "diff")
}
choices = append(choices, choicesYesNoAllQuit...)
FOR:
for {
switch choice, err := c.promptChoice(prompt, choices); {
case err != nil:
return err
case choice == "diff":
if err := c.diffFile(
targetRelPath,
targetContents, targetStateFile.Perm(c.Umask),
actualContents, actualStateFile.Perm(),
); err != nil {
return err
}
case choice == "yes":
break FOR
case choice == "no":
continue TARGETRELPATH
case choice == "all":
c.interactive = false
break FOR
case choice == "quit":
return chezmoi.ExitCodeError(0)
default:
panic(fmt.Sprintf("%s: unexpected choice", choice))
}
}
}

destAbsPathInfos := map[chezmoi.AbsPath]fs.FileInfo{
destAbsPath: destAbsPathInfo,
}
Expand Down

0 comments on commit ca8142f

Please sign in to comment.