Skip to content

Commit

Permalink
feat: Make managed command accept destination directory args
Browse files Browse the repository at this point in the history
  • Loading branch information
RuijieYu authored and twpayne committed Jul 27, 2022
1 parent f8eabef commit 84e7a93
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
7 changes: 5 additions & 2 deletions assets/chezmoi.io/docs/reference/commands/managed.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# `managed`
# `managed` [*target*...]

List all managed entries in the destination directory in alphabetical order.
List all managed entries in the destination directory under all *target*s in
alphabetical order. When no *target*s are supplied, list all managed entries in
the destination directory in alphabetical order.

## `-i`, `--include` *types*

Expand All @@ -14,4 +16,5 @@ Only include entries of type *types*.
$ chezmoi managed --include=files,symlinks
$ chezmoi managed -i dirs
$ chezmoi managed -i dirs,files
$ chezmoi managed -i files ~/.config
```
35 changes: 33 additions & 2 deletions pkg/cmd/managedcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ type managedCmdConfig struct {

func (c *Config) newManagedCmd() *cobra.Command {
managedCmd := &cobra.Command{
Use: "managed",
Use: "managed [paths]...",
Aliases: []string{"list"},
Short: "List the managed entries in the destination directory",
Long: mustLongHelp("managed"),
Example: example("managed"),
Args: cobra.NoArgs,
Args: cobra.ArbitraryArgs,
RunE: c.makeRunEWithSourceState(c.runManagedCmd),
}

Expand All @@ -35,6 +35,22 @@ func (c *Config) newManagedCmd() *cobra.Command {

func (c *Config) runManagedCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
include := c.managed.include.Sub(c.managed.exclude)

// Build queued paths. When no arguments, start from root; otherwise start
// from arguments.
paths := []chezmoi.RelPath{}
if len(args) != 0 {
for _, arg := range args {
if p, err := chezmoi.NormalizePath(arg); err != nil {
return err
} else if p, err := p.TrimDirPrefix(c.DestDirAbsPath); err != nil {
return err
} else {
paths = append(paths, p)
}
}
}

var targetRelPaths chezmoi.RelPaths
_ = sourceState.ForEach(func(targetRelPath chezmoi.RelPath, sourceStateEntry chezmoi.SourceStateEntry) error {
targetStateEntry, err := sourceStateEntry.TargetStateEntry(c.destSystem, c.DestDirAbsPath.Join(targetRelPath))
Expand All @@ -44,6 +60,21 @@ func (c *Config) runManagedCmd(cmd *cobra.Command, args []string, sourceState *c
if !include.IncludeTargetStateEntry(targetStateEntry) {
return nil
}

// when specified arguments, only include paths under these arguments
if len(paths) != 0 {
included := false
for _, path := range paths {
if targetRelPath.HasDirPrefix(path) || targetRelPath.String() == path.String() {
included = true
break
}
}
if !included {
return nil
}
}

targetRelPaths = append(targetRelPaths, targetRelPath)
return nil
})
Expand Down
34 changes: 34 additions & 0 deletions pkg/cmd/testdata/scripts/managed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ cmp stdout golden/managed-symlinks
chezmoi managed --exclude=files
cmp stdout golden/managed-except-files

# test chezmoi managed with arguments
chezmoi managed $HOME${/}.dir $HOME${/}.create
cmp stdout golden/managed-with-args

# test chezmoi managed with child of managed dir as argument
chezmoi managed $HOME${/}.dir/subdir
cmp stdout golden/managed-in-managed

# test chezmoi managed --exclude=dir with arguments
chezmoi managed --exclude=dirs $HOME${/}.dir $HOME${/}.create
cmp stdout golden/managed-with-nodir-args

# test chezmoi managed with absent arguments
chezmoi managed $HOME${/}.dir $HOME${/}.non-exist
cmp stdout golden/managed-with-absent-args

chhome home2/user

# test that chezmoi managed does not evaluate templates
Expand Down Expand Up @@ -84,6 +100,24 @@ cmp stdout golden/managed2
.symlink
.template
script
-- golden/managed-with-args --
.create
.dir
.dir/file
.dir/subdir
.dir/subdir/file
-- golden/managed-in-managed --
.dir/subdir
.dir/subdir/file
-- golden/managed-with-nodir-args --
.create
.dir/file
.dir/subdir/file
-- golden/managed-with-absent-args --
.dir
.dir/file
.dir/subdir
.dir/subdir/file
-- home/user/.local/share/chezmoi/.chezmoiremove --
.remove
-- home2/user/.local/share/chezmoi/create_dot_create.tmpl --
Expand Down

0 comments on commit 84e7a93

Please sign in to comment.