Skip to content

Commit

Permalink
feat: Rename remove command to destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jun 10, 2024
1 parent f781976 commit ca643ce
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 48 deletions.
17 changes: 17 additions & 0 deletions assets/chezmoi.io/docs/reference/commands/destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# `destroy` *target*...

!!! warning

The `destroy` command permanently removes files both from your home directory and chezmoi's source directory.

Only run `chezmoi destroy` if you have a separate backup of your home directory and your source directory.

If you want chezmoi to stop managing the file use [`forget`](forget.md) instead.

If you want to remove all traces of chezmoi from your system use [`purge`](purge.md) instead.

Remove *target* from the source state, the destination directory, and the state.

## `-f`, `--force`

Destroy without prompting.
5 changes: 0 additions & 5 deletions assets/chezmoi.io/docs/reference/commands/forget.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,3 @@ have entries in the source state. They cannot be externals.
```console
$ chezmoi forget ~/.bashrc
```

!!! info

To remove targets from both the source state and destination directory, use
[`remove`](remove.md).
11 changes: 0 additions & 11 deletions assets/chezmoi.io/docs/reference/commands/remove.md

This file was deleted.

2 changes: 1 addition & 1 deletion assets/chezmoi.io/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ nav:
- completion: reference/commands/completion.md
- data: reference/commands/data.md
- decrypt: reference/commands/decrypt.md
- destroy: reference/commands/destroy.md
- diff: reference/commands/diff.md
- doctor: reference/commands/doctor.md
- dump: reference/commands/dump.md
Expand All @@ -166,7 +167,6 @@ nav:
- merge: reference/commands/merge.md
- merge-all: reference/commands/merge-all.md
- purge: reference/commands/purge.md
- remove: reference/commands/remove.md
- re-add: reference/commands/re-add.md
- rm: reference/commands/rm.md
- secret: reference/commands/secret.md
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ type Config struct {
apply applyCmdConfig
archive archiveCmdConfig
chattr chattrCmdConfig
destroy destroyCmdConfig
dump dumpCmdConfig
executeTemplate executeTemplateCmdConfig
ignored ignoredCmdConfig
Expand All @@ -201,7 +202,6 @@ type Config struct {
mergeAll mergeAllCmdConfig
purge purgeCmdConfig
reAdd reAddCmdConfig
remove removeCmdConfig
secret secretCmdConfig
state stateCmdConfig
unmanaged unmanagedCmdConfig
Expand Down Expand Up @@ -1643,6 +1643,7 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {
c.newCompletionCmd(),
c.newDataCmd(),
c.newDecryptCommand(),
c.newDestroyCmd(),
c.newDiffCmd(),
c.newDoctorCmd(),
c.newDumpCmd(),
Expand All @@ -1666,7 +1667,6 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {
c.newMergeAllCmd(),
c.newPurgeCmd(),
c.newReAddCmd(),
c.newRemoveCmd(),
c.newSecretCmd(),
c.newSourcePathCmd(),
c.newStateCmd(),
Expand Down
39 changes: 19 additions & 20 deletions internal/cmd/removecmd.go → internal/cmd/destroycmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,34 @@ import (
"github.com/twpayne/chezmoi/v2/internal/chezmoi"
)

type removeCmdConfig struct {
type destroyCmdConfig struct {
recursive bool
}

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"),
func (c *Config) newDestroyCmd() *cobra.Command {
destroyCmd := &cobra.Command{
Use: "destroy target...",
Short: "Permanently delete an entry from the source state, the destination directory, and the state",
Long: mustLongHelp("destroy"),
Example: example("destroy"),
ValidArgsFunction: c.targetValidArgs,
Args: cobra.MinimumNArgs(1),
RunE: c.makeRunEWithSourceState(c.runRemoveCmd),
RunE: c.makeRunEWithSourceState(c.runDestroyCmd),
Annotations: newAnnotations(
modifiesDestinationDirectory,
modifiesSourceDirectory,
persistentStateModeReadWrite,
),
}

removeCmd.Flags().BoolVarP(&c.remove.recursive, "recursive", "r", c.remove.recursive, "Recurse into subdirectories")
destroyCmd.Flags().BoolVarP(&c.destroy.recursive, "recursive", "r", c.destroy.recursive, "Recurse into subdirectories")

return removeCmd
return destroyCmd
}

func (c *Config) runRemoveCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
func (c *Config) runDestroyCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
targetRelPaths, err := c.targetRelPaths(sourceState, args, &targetRelPathsOptions{
recursive: c.remove.recursive,
recursive: c.destroy.recursive,
})
if err != nil {
return err
Expand All @@ -48,13 +47,13 @@ func (c *Config) runRemoveCmd(cmd *cobra.Command, args []string, sourceState *ch
destAbsPath := c.DestDirAbsPath.Join(targetRelPath)
// Find the path of the entry in the source state, if any.
//
// chezmoi remove might be called on an entry in an exact_ directory
// chezmoi destroy might be called on an entry in an exact_ directory
// that is not present in the source directory. The entry is still
// managed by chezmoi because chezmoi apply will remove it. Therefore,
// chezmoi remove should remove such entries from the target state, even
// if they are not present in the source state. So, when calling chezmoi
// remove on entries like this, we should only remove the entry from the
// target state, not the source state.
// chezmoi destroy should remove such entries from the target state,
// even if they are not present in the source state. So, when calling
// chezmoi destroy on entries like this, we should only remove the entry
// from the target state, not the source state.
//
// For entries in exact_ directories in the target state that are not
// present in the source state, we generate SourceStateRemove entries.
Expand All @@ -68,9 +67,9 @@ func (c *Config) runRemoveCmd(cmd *cobra.Command, args []string, sourceState *ch
if !c.force {
var prompt string
if sourceAbsPath.Empty() {
prompt = fmt.Sprintf("Remove %s", destAbsPath)
prompt = fmt.Sprintf("Destroy %s", destAbsPath)
} else {
prompt = fmt.Sprintf("Remove %s and %s", destAbsPath, sourceAbsPath)
prompt = fmt.Sprintf("Destroy %s and %s", destAbsPath, sourceAbsPath)
}
choice, err := c.promptChoice(prompt, choicesYesNoAllQuit)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
mkhomedir
mksourcedir

# test that chezmoi remove file removes a file
# test that chezmoi destroy file destroys a file
exec chezmoi apply --force
exists $HOME/.file
exec chezmoi remove --force $HOME${/}.file
exec chezmoi destroy --force $HOME${/}.file
! exists $HOME/.file
exec chezmoi state get --bucket=entryState --key=$WORK/home/user/.file
! stdout .

# test that chezmoi remove dir removes a directory
# test that chezmoi destroy dir destroys a directory
exists $HOME/.dir
exec chezmoi remove --force $HOME${/}.dir
exec chezmoi destroy --force $HOME${/}.dir
! exists $HOME/.dir

# test that if any chezmoi remove stops on any error
# test that if any chezmoi destroy stops on any error
exists $HOME/.executable
! exec chezmoi remove --force $HOME${/}.newfile $HOME${/}.executable
! exec chezmoi destroy --force $HOME${/}.newfile $HOME${/}.executable
stderr 'not managed'
exists $HOME/.executable

chhome home2/user

# test that chezmoi apply removes a file and a directory
# test that chezmoi apply destroys a file and a directory
exists $HOME/.file
exists $HOME/.dir
exec chezmoi apply
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/testdata/scripts/issue2954.txtar
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# test that chezmoi remove does not delete the source directory when removing a file in an exact directory
# test that chezmoi destroy does not delete the source directory when removing a file in an exact directory
mkdir $HOME/.dir
mkfile $HOME/.dir/test.file
exec chezmoi remove --force $HOME${/}.dir/test.file
exec chezmoi destroy --force $HOME${/}.dir/test.file
exists $CHEZMOISOURCEDIR

-- home/user/.local/share/chezmoi/exact_dot_dir/.keep --

0 comments on commit ca643ce

Please sign in to comment.