Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make add command add empty files, remove --empty flag #2161

Merged
merged 2 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions assets/chezmoi.io/docs/reference/commands/add.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ Automatically generate a template by replacing strings that match variable
values from the `data` section of the config file with their respective config
names as a template string. Longer substitutions occur before shorter ones.
This implies the `--template` option.
```

## `-e`, `--empty`

Set the `empty` attribute on added files.

## `--encrypt`

Expand Down
8 changes: 3 additions & 5 deletions pkg/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ type ReplaceFunc func(targetRelPath RelPath, newSourceStateEntry, oldSourceState
type AddOptions struct {
AutoTemplate bool // Automatically create templates, if possible.
Create bool // Add create_ entries instead of normal entries.
Empty bool // Add the empty_ attribute to added files.
Encrypt bool // Encrypt files.
EncryptedSuffix string // Suffix for encrypted files.
Exact bool // Add the exact_ attribute to added directories.
Expand Down Expand Up @@ -1731,7 +1730,6 @@ func (s *SourceState) newSourceStateFileEntryFromFile(
) (SourceStateEntry, error) {
fileAttr := FileAttr{
TargetName: fileInfo.Name(),
Empty: options.Empty,
Encrypted: options.Encrypt,
Executable: isExecutable(fileInfo),
Private: isPrivate(fileInfo),
Expand All @@ -1754,8 +1752,8 @@ func (s *SourceState) newSourceStateFileEntryFromFile(
fileAttr.Template = true
}
}
if len(contents) == 0 && !options.Empty {
return nil, nil
if len(contents) == 0 {
fileAttr.Empty = true
}
if options.Encrypt {
contents, err = s.encryption.Encrypt(contents)
Expand All @@ -1772,7 +1770,7 @@ func (s *SourceState) newSourceStateFileEntryFromFile(
lazyContents: lazyContents,
targetStateEntry: &TargetStateFile{
lazyContents: lazyContents,
empty: options.Empty,
empty: len(contents) == 0,
perm: 0o666 &^ s.umask,
},
}, nil
Expand Down
1 change: 0 additions & 1 deletion pkg/chezmoi/sourcestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ func TestSourceStateAdd(t *testing.T) {
NewAbsPath("/home/user/.empty"),
},
addOptions: AddOptions{
Empty: true,
Include: NewEntryTypeSet(EntryTypesAll),
},
tests: []interface{}{
Expand Down
3 changes: 0 additions & 3 deletions pkg/cmd/addcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type addCmdConfig struct {
TemplateSymlinks bool `mapstructure:"templateSymlinks"`
autoTemplate bool
create bool
empty bool
encrypt bool
exact bool
exclude *chezmoi.EntryTypeSet
Expand Down Expand Up @@ -43,7 +42,6 @@ func (c *Config) newAddCmd() *cobra.Command {
flags := addCmd.Flags()
flags.BoolVarP(&c.Add.autoTemplate, "autotemplate", "a", c.Add.autoTemplate, "Generate the template when adding files as templates") //nolint:lll
flags.BoolVar(&c.Add.create, "create", c.Add.create, "Add files that should exist, irrespective of their contents")
flags.BoolVarP(&c.Add.empty, "empty", "e", c.Add.empty, "Add empty files")
flags.BoolVar(&c.Add.encrypt, "encrypt", c.Add.encrypt, "Encrypt files")
flags.BoolVar(&c.Add.exact, "exact", c.Add.exact, "Add directories exactly")
flags.VarP(c.Add.exclude, "exclude", "x", "Exclude entry types")
Expand Down Expand Up @@ -144,7 +142,6 @@ func (c *Config) runAddCmd(cmd *cobra.Command, args []string, sourceState *chezm
return sourceState.Add(c.sourceSystem, c.persistentState, c.destSystem, destAbsPathInfos, &chezmoi.AddOptions{
AutoTemplate: c.Add.autoTemplate,
Create: c.Add.create,
Empty: c.Add.empty,
Encrypt: c.Add.encrypt,
EncryptedSuffix: c.encryption.EncryptedSuffix(),
Exact: c.Add.exact,
Expand Down
14 changes: 0 additions & 14 deletions pkg/cmd/addcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,6 @@ func TestAddCmd(t *testing.T) {
},
},
args: []string{"~/.empty"},
tests: []interface{}{
vfst.TestPath("/home/user/.local/share/chezmoi/empty_dot_empty",
vfst.TestDoesNotExist,
),
},
},
{
name: "empty_with_--empty",
root: map[string]interface{}{
"/home/user": map[string]interface{}{
".empty": "",
},
},
args: []string{"--empty", "~/.empty"},
tests: []interface{}{
vfst.TestPath("/home/user/.local/share/chezmoi/empty_dot_empty",
vfst.TestModeIsRegular,
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/readdcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func (c *Config) runReAddCmd(cmd *cobra.Command, args []string, sourceState *che
destAbsPath: destAbsPathInfo,
}
if err := sourceState.Add(c.sourceSystem, c.persistentState, c.destSystem, destAbsPathInfos, &chezmoi.AddOptions{
Empty: sourceStateFile.Attr.Empty,
Encrypt: sourceStateFile.Attr.Encrypted,
EncryptedSuffix: c.encryption.EncryptedSuffix(),
Include: c.reAdd.include.Sub(c.reAdd.exclude),
Expand Down
8 changes: 2 additions & 6 deletions pkg/cmd/testdata/scripts/add.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ cmp $CHEZMOISOURCEDIR/dot_dir/file golden/dot_dir/file
chezmoi add --exact $HOME${/}.dir/subdir
cmp $CHEZMOISOURCEDIR/dot_dir/exact_subdir/file golden/dot_dir/exact_subdir/file

# test adding an empty file without --empty
# test adding an empty file
chezmoi add $HOME${/}.empty
! exists $CHEZMOISOURCEDIR/dot_empty

# test adding an empty file with --empty
chezmoi add --empty $HOME${/}.empty
cmp $CHEZMOISOURCEDIR/empty_dot_empty golden/empty_dot_empty
exists $CHEZMOISOURCEDIR/empty_dot_empty

# test adding an executable file
chezmoi add $HOME${/}.executable
Expand Down