Skip to content

Commit

Permalink
Add --config-path option to init command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jun 27, 2021
1 parent 8a5831b commit 7b57397
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
6 changes: 6 additions & 0 deletions completions/chezmoi-completion.bash

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

4 changes: 4 additions & 0 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,10 @@ Run `chezmoi apply` after checking out the repo and creating the config file.

Check out *branch* instead of the default branch.

#### `--config-path` *path*

Write the generated config file to *path* instead of the default location.

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

Include existing template data when creating the config file. This defaults to
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/initcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
type initCmdConfig struct {
apply bool
branch string
configPath chezmoi.AbsPath
data bool
depth int
exclude *chezmoi.EntryTypeSet
Expand Down Expand Up @@ -99,6 +100,7 @@ func (c *Config) newInitCmd() *cobra.Command {

flags := initCmd.Flags()
flags.BoolVarP(&c.init.apply, "apply", "a", c.init.apply, "update destination directory")
flags.VarP(&c.init.configPath, "config-path", "C", "Path to write generated config file")
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")
Expand Down Expand Up @@ -213,11 +215,13 @@ func (c *Config) runInitCmd(cmd *cobra.Command, args []string) error {
}

// Write the config.
configDir := chezmoi.AbsPath(c.bds.ConfigHome).Join("chezmoi")
if err := chezmoi.MkdirAll(c.baseSystem, configDir, 0o777); err != nil {
configPath := c.init.configPath
if c.init.configPath == "" {
configPath = chezmoi.AbsPath(c.bds.ConfigHome).Join("chezmoi").Join(configTemplateRelPath)
}
if err := chezmoi.MkdirAll(c.baseSystem, configPath.Dir(), 0o777); err != nil {
return err
}
configPath := configDir.Join(configTemplateRelPath)
if err := c.baseSystem.WriteFile(configPath, configFileContents, 0o600); err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions internal/cmd/testdata/scripts/init.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ chezmoi init
exists $CHEZMOISOURCEDIR/.git

# create a commit
cp golden/chezmoi.toml $CHEZMOISOURCEDIR/.chezmoi.toml.tmpl
cp golden/.file $CHEZMOISOURCEDIR/dot_file
chezmoi git add dot_file
chezmoi git commit -- --message 'Add dot_file'
chezmoi git add .
chezmoi git commit -- --message 'Initial commit'

# test that chezmoi init fetches git repo but does not apply
chhome home2/user
Expand Down Expand Up @@ -78,6 +79,13 @@ mkgitconfig
chezmoi init --apply --branch=new-branch file://$WORK/home/user/.local/share/chezmoi
grep '# edited' $HOME/.file

# test chezmoi init --config-path
chhome home10/user
mkgitconfig
chezmoi init --config-path=$HOME/.chezmoi.toml --debug file://$WORK/home/user/.local/share/chezmoi
cmp $HOME/.chezmoi.toml golden/chezmoi.toml
! exists $CHEZMOICONFIGDIR/chezmoi.toml

-- golden/chezmoi.toml --
[data]
email = "firstname.lastname@company.com"
Expand Down

0 comments on commit 7b57397

Please sign in to comment.