From b64f2bc121d1097b91cf608fc38aff9e6b0368c0 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 10 Oct 2023 21:00:30 +0200 Subject: [PATCH] feat: Add env config var as an alternative to scriptEnv Co-authored-by: Austin Ziegler --- .../configuration-file/variables.md.yaml | 3 +++ .../chezmoi.io/docs/reference/target-types.md | 2 +- internal/cmd/config.go | 12 +++++++++- internal/cmd/testdata/scripts/issue3268.txtar | 23 +++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/assets/chezmoi.io/docs/reference/configuration-file/variables.md.yaml b/assets/chezmoi.io/docs/reference/configuration-file/variables.md.yaml index 3ad788ae3f0..fa51bef177c 100644 --- a/assets/chezmoi.io/docs/reference/configuration-file/variables.md.yaml +++ b/assets/chezmoi.io/docs/reference/configuration-file/variables.md.yaml @@ -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` diff --git a/assets/chezmoi.io/docs/reference/target-types.md b/assets/chezmoi.io/docs/reference/target-types.md index 05c515ef392..91c22df2d51 100644 --- a/assets/chezmoi.io/docs/reference/target-types.md +++ b/assets/chezmoi.io/docs/reference/target-types.md @@ -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 diff --git a/internal/cmd/config.go b/internal/cmd/config.go index ec02306baf9..b7db4cd707f 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -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"` @@ -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) } diff --git a/internal/cmd/testdata/scripts/issue3268.txtar b/internal/cmd/testdata/scripts/issue3268.txtar index b33956e7f21..d0f7eed1709 100644 --- a/internal/cmd/testdata/scripts/issue3268.txtar +++ b/internal/cmd/testdata/scripts/issue3268.txtar @@ -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