Skip to content

Commit

Permalink
feat: Add env config var as an alternative to scriptEnv
Browse files Browse the repository at this point in the history
Co-authored-by: Austin Ziegler <austin@zieglers.ca>
  • Loading branch information
twpayne and halostatue committed Oct 10, 2023
1 parent f47c268 commit b64f2bc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ sections:
description: Destination directory
encryption:
description: Encryption type, either `age` or `gpg`
env:
type: object
description: Extra environment variables for scripts and commands
format:
default: '`json`'
description: Format for data output, either `json` or `yaml`
Expand Down
2 changes: 1 addition & 1 deletion assets/chezmoi.io/docs/reference/target-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ directory that exists and is a directory.

chezmoi sets a number of `CHEZMOI*` environment variables when running scripts,
corresponding to commonly-used template data variables. Extra environment
variables can be set in the `scriptEnv` configuration variable.
variables can be set in the `env` or `scriptEnv` configuration variables.

### Scripts on Windows

Expand Down
12 changes: 11 additions & 1 deletion internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type ConfigFile struct {
CacheDirAbsPath chezmoi.AbsPath `json:"cacheDir" mapstructure:"cacheDir" yaml:"cacheDir"`
Color autoBool `json:"color" mapstructure:"color" yaml:"color"`
Data map[string]any `json:"data" mapstructure:"data" yaml:"data"`
Env map[string]string `json:"env" mapstructure:"env" yaml:"env"`
Format writeDataFormat `json:"format" mapstructure:"format" yaml:"format"`
DestDirAbsPath chezmoi.AbsPath `json:"destDir" mapstructure:"destDir" yaml:"destDir"`
GitHub gitHubConfig `json:"gitHub" mapstructure:"gitHub" yaml:"gitHub"`
Expand Down Expand Up @@ -2124,7 +2125,16 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
os.Setenv(key, valueStr)
}
}
for key, value := range c.ScriptEnv {
var env map[string]string
switch {
case len(c.Env) != 0 && len(c.ScriptEnv) != 0:
return errors.New("only one of env or scriptEnv may be set")
case len(c.Env) != 0:
env = c.Env
case len(c.ScriptEnv) != 0:
env = c.ScriptEnv
}
for key, value := range env {
if strings.HasPrefix(key, "CHEZMOI_") {
c.errorf("warning: %s: overriding reserved environment variable", key)
}
Expand Down
23 changes: 23 additions & 0 deletions internal/cmd/testdata/scripts/issue3268.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,32 @@
exec chezmoi execute-template '{{ output "chezmoi-test-command" }}'
stdout 'CHEZMOI_SOURCE_DIR=.*/\.local/share/chezmoi\s?$'

chhome home2/user

# test that chezmoi sets environment variables from env
exec chezmoi execute-template '{{ env "VAR" }}'
stdout VALUE

chhome home3/user

# test that env and scriptEnv cannot both be set
! exec chezmoi execute-template ''
stderr 'only one of env or scriptEnv may be set'

-- bin/chezmoi-test-command --
#!/bin/sh

echo CHEZMOI_SOURCE_DIR=${CHEZMOI_SOURCE_DIR}
-- bin/chezmoi-test-command.cmd --
@echo CHEZMOI_SOURCE_DIR=%CHEZMOI_SOURCE_DIR%
-- home2/user/.config/chezmoi/chezmoi.json --
{
"env": {
"VAR": "VALUE"
}
}
-- home3/user/.config/chezmoi/chezmoi.yaml --
env:
VAR: VALUE
scriptEnv:
VAR: VALUE

0 comments on commit b64f2bc

Please sign in to comment.