Skip to content

Commit

Permalink
Add init --data option
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 14, 2021
1 parent d41951f commit 1d42c99
Show file tree
Hide file tree
Showing 5 changed files with 41 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 @@ -275,6 +275,7 @@ func newConfig(options ...configOption) (*Config, error) {
include: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesAll),
},
init: initCmdConfig{
data: true,
exclude: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesNone),
},
managed: managedCmdConfig{
Expand Down
6 changes: 5 additions & 1 deletion cmd/initcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

type initCmdConfig struct {
apply bool
data bool
depth int
exclude *chezmoi.EntryTypeSet
oneShot bool
Expand Down Expand Up @@ -84,6 +85,7 @@ func (c *Config) newInitCmd() *cobra.Command {

flags := initCmd.Flags()
flags.BoolVarP(&c.init.apply, "apply", "a", c.init.apply, "update destination directory")
flags.BoolVar(&c.init.data, "data", c.init.data, "include existing template data")
flags.IntVarP(&c.init.depth, "depth", "d", c.init.depth, "create a shallow clone")
flags.VarP(c.init.exclude, "exclude", "x", "exclude entry types")
flags.BoolVar(&c.init.oneShot, "one-shot", c.init.oneShot, "one shot")
Expand Down Expand Up @@ -239,7 +241,9 @@ func (c *Config) createConfigFile(filename chezmoi.RelPath, data []byte) ([]byte

sb := strings.Builder{}
templateData := c.defaultTemplateData()
chezmoi.RecursiveMerge(templateData, c.Data)
if c.init.data {
chezmoi.RecursiveMerge(templateData, c.Data)
}
if err = t.Execute(&sb, templateData); err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions completions/chezmoi-completion.bash

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

6 changes: 6 additions & 0 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,12 @@ own binary.

Run `chezmoi apply` after checking out the repo and creating the config file.

#### `--data` *bool*

Include existing template data when creating the config file. This defaults to
`true`. Set this to `false` to simulate creating the config file with no
existing template data.

#### `--depth` *depth*

Clone the repo with depth *depth*.
Expand Down
27 changes: 27 additions & 0 deletions testdata/scripts/init.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,35 @@ cmp $HOME/.file golden/.file
! exists $CHEZMOICONFIGDIR
! exists $CHEZMOISOURCEDIR

# test chezmoi init --data=true
chhome home7/user
mkgitconfig
chezmoi init --data=true file://$WORK/home/user/.local/share/chezmoi
cmp $CHEZMOICONFIGDIR/chezmoi.toml golden/chezmoi.toml

# test chezmoi init --data=false
chezmoi init --data=false file://$WORK/home/user/.local/share/chezmoi
cmp $CHEZMOICONFIGDIR/chezmoi.toml golden/chezmoi.toml-no-data

-- golden/chezmoi.toml --
[data]
email = "user@company.com"
-- golden/chezmoi.toml-no-data --
[data]
email = "user@home.org"
-- home4/user/.config/chezmoi/chezmoi.toml --
-- home5/user/dotfiles/.git/.keep --
-- home5/user/dotfiles/.chezmoi.toml.tmpl --
[data]
email = "user@home.org"
-- home7/user/.config/chezmoi/chezmoi.toml --
[data]
email = "user@company.com"
-- home7/user/.local/share/chezmoi/.git/.keep --
-- home7/user/.local/share/chezmoi/.chezmoi.toml.tmpl --
{{- $email := get . "email" -}}
{{- if not $email -}}
{{- $email = "user@home.org" -}}
{{- end -}}
[data]
email = {{ $email | quote }}

0 comments on commit 1d42c99

Please sign in to comment.