From ca8142f0f0b448503abf56982aa2041c4ba9d534 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 14 Nov 2022 22:23:09 +0100 Subject: [PATCH] fix: Make re-add command respect --interactive flag --- pkg/cmd/readdcmd.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pkg/cmd/readdcmd.go b/pkg/cmd/readdcmd.go index eb4c8deaf0f..89db13d9e97 100644 --- a/pkg/cmd/readdcmd.go +++ b/pkg/cmd/readdcmd.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "fmt" "io/fs" "sort" @@ -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 { @@ -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, }