Skip to content

Commit

Permalink
feat: Add --cache option and .chezmoi.cacheDir template variable
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 11, 2021
1 parent 949056d commit 126cb0c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/REFERENCE.md
Expand Up @@ -5,6 +5,7 @@ Manage your dotfiles across multiple machines, securely.
<!--- toc --->
* [Concepts](#concepts)
* [Global command line flags](#global-command-line-flags)
* [`--cache` *directory*](#--cache-directory)
* [`--color` *value*](#--color-value)
* [`-c`, `--config` *filename*](#-c---config-filename)
* [`--config-format` `json`|`toml`|`yaml`](#--config-format-jsontomlyaml)
Expand Down Expand Up @@ -174,6 +175,10 @@ destination directory, where:

Command line flags override any values set in the configuration file.

### `--cache` *directory*

Use *directory* as the cache directory.

### `--color` *value*

Colorize diffs, *value* can be `on`, `off`, `auto`, or any boolean-like value
Expand Down Expand Up @@ -1891,6 +1896,7 @@ chezmoi provides the following automatically-populated variables:
| -------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `.chezmoi.arch` | `string` | Architecture, e.g. `amd64`, `arm`, etc. as returned by [runtime.GOARCH](https://pkg.go.dev/runtime?tab=doc#pkg-constants) |
| `.chezmoi.args` | `[]string` | The arguments passed to the `chezmoi` command, starting with the program command |
| `.chezmoi.cacheDir` | `string` | The cache directory |
| `.chezmoi.configFile` | `string` | The path to the configuration file used by chezmoi, if any |
| `.chezmoi.executable` | `string` | The path to the `chezmoi` executable, if available |
| `.chezmoi.fqdnHostname` | `string` | The fully-qualified domain name hostname of the machine chezmoi is running on |
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/config.go
Expand Up @@ -930,6 +930,7 @@ func (c *Config) defaultTemplateData() map[string]interface{} {
"chezmoi": map[string]interface{}{
"arch": runtime.GOARCH,
"args": os.Args,
"cacheDir": c.CacheDirAbsPath.String(),
"configFile": c.configFileAbsPath.String(),
"executable": executable,
"fqdnHostname": fqdnHostname,
Expand Down Expand Up @@ -1245,6 +1246,7 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {

persistentFlags := rootCmd.PersistentFlags()

persistentFlags.Var(&c.CacheDirAbsPath, "cache", "Set cache directory")
persistentFlags.Var(&c.Color, "color", "Colorize output")
persistentFlags.VarP(&c.DestDirAbsPath, "destination", "D", "Set destination directory")
persistentFlags.Var(&c.Mode, "mode", "Mode")
Expand All @@ -1256,6 +1258,7 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {
persistentFlags.BoolVarP(&c.Verbose, "verbose", "v", c.Verbose, "Make output more verbose")
persistentFlags.VarP(&c.WorkingTreeAbsPath, "working-tree", "W", "Set working tree directory")
for viperKey, key := range map[string]string{
"cacheDir": "cache",
"color": "color",
"destDir": "destination",
"persistentState": "persistent-state",
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/testdata/scripts/config.txt
Expand Up @@ -20,6 +20,10 @@ stdout 'sourceDir: .*/config/source'
chezmoi data --config=$CHEZMOICONFIGDIR/chezmoi.yaml --format=yaml
stdout 'sourceDir: .*/config2/source'

# test that the cache directory can be set
chezmoi data --cache=/flag/cache --format=yaml
stdout 'cacheDir: .*/flag/cache'

[windows] stop 'remaining tests require /dev/stdin'

# test that chezmoi can read the config from stdin
Expand Down

0 comments on commit 126cb0c

Please sign in to comment.