Skip to content

Commit

Permalink
feat: Extend --include and --exclude flags to include templates
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Oct 15, 2022
1 parent 504ca18 commit fdfbc84
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
8 changes: 4 additions & 4 deletions assets/chezmoi.io/docs/reference/command-line-flags/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Set the output format.

Only operate on target state entries of type *types*. *types* is a
comma-separated list of target states (`all`, `dirs`, `files`, `remove`,
`scripts`, `symlinks`) and/or source attributes (`encrypted`, `externals`) and
can be excluded by preceding them with a `no`.
`scripts`, `symlinks`) and/or source attributes (`encrypted`, `externals`,
`templates`) and can be excluded by preceding them with a `no`.

!!! example

Expand All @@ -34,8 +34,8 @@ Recurse into subdirectories, `true` by default.
## `-x`, `--exclude` *types*

Exclude target state entries of type *types*. *types* is a comma-separated list
of target states (`all`, `dirs`, `files`, `remove`, `scripts`, `symlinks`,
`encrypted`, and `externals`).
of target states (`all`, `dirs`, `files`, `remove`, `scripts`, `symlinks`)
and/or source attributes (`encrypted`, `externals`, `templates`).

!!! example

Expand Down
15 changes: 14 additions & 1 deletion pkg/chezmoi/entrytypeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
EntryTypeSymlinks
EntryTypeEncrypted
EntryTypeExternals
EntryTypeTemplates

// EntryTypesAll is all entry types.
EntryTypesAll EntryTypeBits = EntryTypeDirs |
Expand All @@ -37,7 +38,8 @@ const (
EntryTypeScripts |
EntryTypeSymlinks |
EntryTypeEncrypted |
EntryTypeExternals
EntryTypeExternals |
EntryTypeTemplates

// EntryTypesNone is no entry types.
EntryTypesNone EntryTypeBits = 0
Expand All @@ -54,6 +56,7 @@ var (
"symlinks": EntryTypeSymlinks,
"encrypted": EntryTypeEncrypted,
"externals": EntryTypeExternals,
"templates": EntryTypeTemplates,
}

entryTypeCompletions = []string{
Expand All @@ -70,9 +73,11 @@ var (
"noremove",
"noscripts",
"nosymlinks",
"notemplates",
"remove",
"scripts",
"symlinks",
"templates",
}
)

Expand All @@ -98,6 +103,11 @@ func (s *EntryTypeSet) IncludeExternals() bool {
return s.bits&EntryTypeExternals != 0
}

// IncludeTemplates returns true if s includes templates.
func (s *EntryTypeSet) IncludeTemplates() bool {
return s.bits&EntryTypeTemplates != 0
}

// IncludeFileInfo returns true if the type of fileInfo is a member.
func (s *EntryTypeSet) IncludeFileInfo(fileInfo fs.FileInfo) bool {
switch {
Expand All @@ -119,6 +129,8 @@ func (s *EntryTypeSet) IncludeTargetStateEntry(targetStateEntry TargetStateEntry
return true
case s.IncludeExternals() && sourceAttr.External:
return true
case s.IncludeTemplates() && sourceAttr.Template:
return true
}

switch targetStateEntry.(type) {
Expand Down Expand Up @@ -197,6 +209,7 @@ func (s *EntryTypeSet) String() string {
"symlinks",
"encrypted",
"externals",
"templates",
} {
if s.bits&(1<<i) != 0 {
elements = append(elements, element)
Expand Down
2 changes: 1 addition & 1 deletion pkg/chezmoi/entrytypeset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestIncludeMaskSet(t *testing.T) {
},
{
s: "all,noscripts",
expected: NewEntryTypeSet(EntryTypeDirs | EntryTypeFiles | EntryTypeRemove | EntryTypeSymlinks | EntryTypeEncrypted | EntryTypeExternals),
expected: NewEntryTypeSet(EntryTypeDirs | EntryTypeFiles | EntryTypeRemove | EntryTypeSymlinks | EntryTypeEncrypted | EntryTypeExternals | EntryTypeTemplates),
},
{
s: "noscripts",
Expand Down
5 changes: 5 additions & 0 deletions pkg/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,7 @@ func (s *SourceState) newCreateTargetStateEntryFunc(
perm: fileAttr.perm() &^ s.umask,
sourceAttr: SourceAttr{
Encrypted: fileAttr.Encrypted,
Template: fileAttr.Template,
},
}, nil
}
Expand All @@ -1443,6 +1444,9 @@ func (s *SourceState) newFileTargetStateEntryFunc(
linkname := normalizeLinkname(s.sourceDirAbsPath.Join(sourceRelPath.RelPath()).String())
return &TargetStateSymlink{
lazyLinkname: newLazyLinkname(linkname),
sourceAttr: SourceAttr{
Template: fileAttr.Template,
},
}, nil
}
}
Expand All @@ -1465,6 +1469,7 @@ func (s *SourceState) newFileTargetStateEntryFunc(
perm: fileAttr.perm() &^ s.umask,
sourceAttr: SourceAttr{
Encrypted: fileAttr.Encrypted,
Template: fileAttr.Template,
},
}, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/chezmoi/sourcestateentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type SourceAttr struct {
Encrypted bool
External bool
Template bool
}

// A SourceStateOrigin represents the origin of a source state.
Expand Down
28 changes: 28 additions & 0 deletions pkg/cmd/testdata/scripts/managed.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ cmp stdout golden/managed-files
exec chezmoi managed --include=symlinks
cmp stdout golden/managed-symlinks

# test chezmoi managed --include=templates
exec chezmoi managed --include=templates
cmp stdout golden/managed-templates

# test chezmoi managed --exclude=files
exec chezmoi managed --exclude=files
cmp stdout golden/managed-except-files

# test chezmoi managed --exclude=files,templates
exec chezmoi managed --exclude=files,templates
cmp stdout golden/managed-except-files-and-templates

# test chezmoi managed with arguments
exec chezmoi managed $HOME${/}.dir $HOME${/}.create
cmp stdout golden/managed-with-args
Expand Down Expand Up @@ -81,6 +89,24 @@ cmp stdout golden/managed2
.dir
.dir/subdir
.symlink
.template
-- golden/managed-except-files-and-templates --
.dir
.dir/subdir
.symlink
-- golden/managed-except-templates --
.create
.dir
.dir/file
.dir/subdir
.dir/subdir/file
.empty
.executable
.file
.private
.readonly
.remove
.symlink
-- golden/managed-files --
.create
.dir/file
Expand All @@ -97,6 +123,8 @@ cmp stdout golden/managed2
.dir/subdir/file
-- golden/managed-symlinks --
.symlink
-- golden/managed-templates --
.template
-- golden/managed-with-absent-args --
.dir
.dir/file
Expand Down

0 comments on commit fdfbc84

Please sign in to comment.