forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremovecmd.go
More file actions
64 lines (57 loc) · 1.59 KB
/
removecmd.go
File metadata and controls
64 lines (57 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/v2/internal/chezmoi"
)
func (c *Config) newRemoveCmd() *cobra.Command {
removeCmd := &cobra.Command{
Use: "remove target...",
Aliases: []string{"rm"},
Short: "Remove a target from the source state and the destination directory",
Long: mustLongHelp("remove"),
Example: example("remove"),
Args: cobra.MinimumNArgs(1),
RunE: c.makeRunEWithSourceState(c.runRemoveCmd),
Annotations: map[string]string{
modifiesDestinationDirectory: "true",
modifiesSourceDirectory: "true",
},
}
return removeCmd
}
func (c *Config) runRemoveCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
targetRelPaths, err := c.targetRelPaths(sourceState, args, targetRelPathsOptions{
mustBeInSourceState: true,
})
if err != nil {
return err
}
for _, targetRelPath := range targetRelPaths {
destAbsPath := c.destDirAbsPath.Join(targetRelPath)
sourceAbsPath := c.sourceDirAbsPath.Join(sourceState.MustEntry(targetRelPath).SourceRelPath().RelPath())
if !c.force {
choice, err := c.promptChoice(fmt.Sprintf("Remove %s and %s", destAbsPath, sourceAbsPath), yesNoAllQuit)
if err != nil {
return err
}
switch choice {
case "yes":
case "no":
continue
case "all":
c.force = true
case "quit":
return nil
}
}
if err := c.destSystem.RemoveAll(destAbsPath); err != nil && !os.IsNotExist(err) {
return err
}
if err := c.sourceSystem.RemoveAll(sourceAbsPath); err != nil && !os.IsNotExist(err) {
return err
}
}
return nil
}