Skip to content

Commit b64f2bc

Browse files
twpaynehalostatue
andcommittedOct 10, 2023
feat: Add env config var as an alternative to scriptEnv
Co-authored-by: Austin Ziegler <austin@zieglers.ca>
1 parent f47c268 commit b64f2bc

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed
 

‎assets/chezmoi.io/docs/reference/configuration-file/variables.md.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ sections:
1919
description: Destination directory
2020
encryption:
2121
description: Encryption type, either `age` or `gpg`
22+
env:
23+
type: object
24+
description: Extra environment variables for scripts and commands
2225
format:
2326
default: '`json`'
2427
description: Format for data output, either `json` or `yaml`

‎assets/chezmoi.io/docs/reference/target-types.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ directory that exists and is a directory.
9494

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

9999
### Scripts on Windows
100100

‎internal/cmd/config.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ type ConfigFile struct {
100100
CacheDirAbsPath chezmoi.AbsPath `json:"cacheDir" mapstructure:"cacheDir" yaml:"cacheDir"`
101101
Color autoBool `json:"color" mapstructure:"color" yaml:"color"`
102102
Data map[string]any `json:"data" mapstructure:"data" yaml:"data"`
103+
Env map[string]string `json:"env" mapstructure:"env" yaml:"env"`
103104
Format writeDataFormat `json:"format" mapstructure:"format" yaml:"format"`
104105
DestDirAbsPath chezmoi.AbsPath `json:"destDir" mapstructure:"destDir" yaml:"destDir"`
105106
GitHub gitHubConfig `json:"gitHub" mapstructure:"gitHub" yaml:"gitHub"`
@@ -2124,7 +2125,16 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
21242125
os.Setenv(key, valueStr)
21252126
}
21262127
}
2127-
for key, value := range c.ScriptEnv {
2128+
var env map[string]string
2129+
switch {
2130+
case len(c.Env) != 0 && len(c.ScriptEnv) != 0:
2131+
return errors.New("only one of env or scriptEnv may be set")
2132+
case len(c.Env) != 0:
2133+
env = c.Env
2134+
case len(c.ScriptEnv) != 0:
2135+
env = c.ScriptEnv
2136+
}
2137+
for key, value := range env {
21282138
if strings.HasPrefix(key, "CHEZMOI_") {
21292139
c.errorf("warning: %s: overriding reserved environment variable", key)
21302140
}

‎internal/cmd/testdata/scripts/issue3268.txtar

+23
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,32 @@
44
exec chezmoi execute-template '{{ output "chezmoi-test-command" }}'
55
stdout 'CHEZMOI_SOURCE_DIR=.*/\.local/share/chezmoi\s?$'
66

7+
chhome home2/user
8+
9+
# test that chezmoi sets environment variables from env
10+
exec chezmoi execute-template '{{ env "VAR" }}'
11+
stdout VALUE
12+
13+
chhome home3/user
14+
15+
# test that env and scriptEnv cannot both be set
16+
! exec chezmoi execute-template ''
17+
stderr 'only one of env or scriptEnv may be set'
18+
719
-- bin/chezmoi-test-command --
820
#!/bin/sh
921

1022
echo CHEZMOI_SOURCE_DIR=${CHEZMOI_SOURCE_DIR}
1123
-- bin/chezmoi-test-command.cmd --
1224
@echo CHEZMOI_SOURCE_DIR=%CHEZMOI_SOURCE_DIR%
25+
-- home2/user/.config/chezmoi/chezmoi.json --
26+
{
27+
"env": {
28+
"VAR": "VALUE"
29+
}
30+
}
31+
-- home3/user/.config/chezmoi/chezmoi.yaml --
32+
env:
33+
VAR: VALUE
34+
scriptEnv:
35+
VAR: VALUE

0 commit comments

Comments
 (0)
Please sign in to comment.