Skip to content

Commit

Permalink
Merge pull request #653 from twpayne/purge
Browse files Browse the repository at this point in the history
Add purge command
  • Loading branch information
twpayne committed Apr 3, 2020
2 parents e6b1424 + e384392 commit 8d538be
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type Config struct {
_import importCmdConfig
init initCmdConfig
keyring keyringCmdConfig
purge purgeCmdConfig
remove removeCmdConfig
update updateCmdConfig
upgrade upgradeCmdConfig
Expand Down
15 changes: 15 additions & 0 deletions cmd/docs.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions cmd/helps.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions cmd/purge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

var purgeCmd = &cobra.Command{
Use: "purge",
Args: cobra.NoArgs,
Short: "Purge all of chezmoi's configuration and data",
Long: mustGetLongHelp("purge"),
Example: getExample("purge"),
RunE: config.runPurgeCmd,
}

type purgeCmdConfig struct {
force bool
}

func init() {
rootCmd.AddCommand(purgeCmd)

persistentFlags := purgeCmd.PersistentFlags()
persistentFlags.BoolVarP(&config.purge.force, "force", "f", false, "remove without prompting")
}

func (c *Config) runPurgeCmd(cmd *cobra.Command, args []string) error {
// Build a list of chezmoi-related paths.
var paths []string
for _, dirs := range [][]string{
c.bds.ConfigDirs,
c.bds.DataDirs,
} {
for _, dir := range dirs {
paths = append(paths, filepath.Join(dir, "chezmoi"))
}
}
paths = append(paths, c.configFile, c.getPersistentStateFile())

// Remove all paths that exist.
PATH:
for _, path := range paths {
_, err := c.fs.Stat(path)
switch {
case os.IsNotExist(err):
continue PATH
case err != nil:
return err
}
if !c.purge.force {
choice, err := c.prompt(fmt.Sprintf("Remove %s", path), "ynqa")
if err != nil {
return err
}
switch choice {
case 'a':
c.purge.force = true
case 'n':
continue PATH
case 'q':
return nil
}
}
if err := c.mutator.RemoveAll(path); err != nil {
return err
}
}

return nil
}
41 changes: 41 additions & 0 deletions completions/chezmoi-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,46 @@ _chezmoi_merge()
noun_aliases=()
}

_chezmoi_purge()
{
last_command="chezmoi_purge"

command_aliases=()

commands=()

flags=()
two_word_flags=()
local_nonpersistent_flags=()
flags_with_completion=()
flags_completion=()

flags+=("--force")
flags+=("-f")
flags+=("--color=")
two_word_flags+=("--color")
flags+=("--config=")
two_word_flags+=("--config")
two_word_flags+=("-c")
flags+=("--debug")
flags+=("--destination=")
two_word_flags+=("--destination")
two_word_flags+=("-D")
flags+=("--dry-run")
flags+=("-n")
flags+=("--follow")
flags+=("--remove")
flags+=("--source=")
two_word_flags+=("--source")
two_word_flags+=("-S")
flags+=("--verbose")
flags+=("-v")

must_have_one_flag=()
must_have_one_noun=()
noun_aliases=()
}

_chezmoi_remove()
{
last_command="chezmoi_remove"
Expand Down Expand Up @@ -1896,6 +1936,7 @@ _chezmoi_root_command()
commands+=("import")
commands+=("init")
commands+=("merge")
commands+=("purge")
commands+=("remove")
if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
command_aliases+=("rm")
Expand Down
13 changes: 12 additions & 1 deletion completions/chezmoi.fish
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

function __fish_chezmoi_no_subcommand --description 'Test if chezmoi has yet to be given the subcommand'
for i in (commandline -opc)
if contains -- $i add apply archive cat cd chattr completion data diff docs doctor dump edit edit-config execute-template forget git hg import init merge remove secret source source-path unmanaged update upgrade verify
if contains -- $i add apply archive cat cd chattr completion data diff docs doctor dump edit edit-config execute-template forget git hg import init merge purge remove secret source source-path unmanaged update upgrade verify
return 1
end
end
Expand Down Expand Up @@ -56,6 +56,7 @@ complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a hg -d 'Run mercurial
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a import -d 'Import a tar archive into the source state'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a init -d 'Setup the source directory and update the destination directory to match the target state'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a merge -d 'Perform a three-way merge between the destination state, the source state, and the target state'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a purge -d 'Purge all of chezmoi\'s configuration and data'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a remove -d 'Remove a target from the source state and the destination directory'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a secret -d 'Interact with a secret manager'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a source -d 'Run the source version control system command in the source directory'
Expand Down Expand Up @@ -284,6 +285,16 @@ complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path merge' -l follo
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path merge' -l remove -d 'remove targets'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path merge' -r -s S -l source -d 'source directory'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path merge' -s v -l verbose -d 'verbose'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -s f -l force -d 'remove without prompting'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -r -l color -d 'colorize diffs'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -r -s c -l config -d 'config file'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -l debug -d 'write debug logs'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -r -s D -l destination -d 'destination directory'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -s n -l dry-run -d 'dry run'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -l follow -d 'follow symlinks'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -l remove -d 'remove targets'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -r -s S -l source -d 'source directory'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path purge' -s v -l verbose -d 'verbose'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path remove' -s f -l force -d 'remove without prompting'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path remove' -r -l color -d 'colorize diffs'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path remove' -r -s c -l config -d 'config file'
Expand Down
18 changes: 18 additions & 0 deletions completions/chezmoi.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function _chezmoi {
"import:Import a tar archive into the source state"
"init:Setup the source directory and update the destination directory to match the target state"
"merge:Perform a three-way merge between the destination state, the source state, and the target state"
"purge:Purge all of chezmoi's configuration and data"
"remove:Remove a target from the source state and the destination directory"
"secret:Interact with a secret manager"
"source:Run the source version control system command in the source directory"
Expand Down Expand Up @@ -122,6 +123,9 @@ function _chezmoi {
merge)
_chezmoi_merge
;;
purge)
_chezmoi_purge
;;
remove)
_chezmoi_remove
;;
Expand Down Expand Up @@ -529,6 +533,20 @@ function _chezmoi_merge {
'8: :_files '
}

function _chezmoi_purge {
_arguments \
'(-f --force)'{-f,--force}'[remove without prompting]' \
'--color[colorize diffs]:' \
'(-c --config)'{-c,--config}'[config file]:' \
'--debug[write debug logs]' \
'(-D --destination)'{-D,--destination}'[destination directory]:' \
'(-n --dry-run)'{-n,--dry-run}'[dry run]' \
'--follow[follow symlinks]' \
'--remove[remove targets]' \
'(-S --source)'{-S,--source}'[source directory]:' \
'(-v --verbose)'{-v,--verbose}'[verbose]'
}

function _chezmoi_remove {
_arguments \
'(-f --force)'{-f,--force}'[remove without prompting]' \
Expand Down
15 changes: 15 additions & 0 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Manage your dotfiles securely across multiple machines.
* [`import` *filename*](#import-filename)
* [`manage` *targets*](#manage-targets)
* [`merge` *targets*](#merge-targets)
* [`purge`](#purge)
* [`remove` *targets*](#remove-targets)
* [`rm` *targets*](#rm-targets)
* [`secret`](#secret)
Expand Down Expand Up @@ -652,6 +653,20 @@ cannot be decrypted) a two-way merge is performed instead.

chezmoi merge ~/.bashrc

### `purge`

Remove chezmoi's configuration, state, and source directory, but leave the
target state intact.

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

Remove without prompting.

#### `purge` examples

chezmoi purge
chezmoi purge --force

### `remove` *targets*

Remove *targets* from both the source state and the destination directory.
Expand Down

0 comments on commit 8d538be

Please sign in to comment.