Skip to content

Commit d07689e

Browse files
committed
fix: Set env vars from config file when re-reading config
1 parent 6effb0a commit d07689e

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

internal/cmd/config.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,16 @@ func (c *Config) createAndReloadConfigFile(cmd *cobra.Command) error {
766766
if err := c.decodeConfigBytes(configTemplate.format, configFileContents, &c.ConfigFile); err != nil {
767767
return fmt.Errorf("%s: %w", configTemplate.sourceAbsPath, err)
768768
}
769-
return c.setEncryption()
769+
770+
if err := c.setEncryption(); err != nil {
771+
return err
772+
}
773+
774+
if err := c.setEnvironmentVariables(); err != nil {
775+
return err
776+
}
777+
778+
return nil
770779
}
771780

772781
// createConfigFile creates a config file using a template and returns its
@@ -2175,20 +2184,9 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
21752184
os.Setenv(key, valueStr)
21762185
}
21772186
}
2178-
var env map[string]string
2179-
switch {
2180-
case len(c.Env) != 0 && len(c.ScriptEnv) != 0:
2181-
return errors.New("only one of env or scriptEnv may be set")
2182-
case len(c.Env) != 0:
2183-
env = c.Env
2184-
case len(c.ScriptEnv) != 0:
2185-
env = c.ScriptEnv
2186-
}
2187-
for key, value := range env {
2188-
if strings.HasPrefix(key, "CHEZMOI_") {
2189-
c.errorf("warning: %s: overriding reserved environment variable", key)
2190-
}
2191-
os.Setenv(key, value)
2187+
2188+
if err := c.setEnvironmentVariables(); err != nil {
2189+
return err
21922190
}
21932191

21942192
if err := c.runHookPre(cmd.Name()); err != nil {
@@ -2465,6 +2463,28 @@ func (c *Config) setEncryption() error {
24652463
return nil
24662464
}
24672465

2466+
// setEnvironmentVariables sets all environment variables defined in c.
2467+
func (c *Config) setEnvironmentVariables() error {
2468+
var env map[string]string
2469+
switch {
2470+
case len(c.Env) != 0 && len(c.ScriptEnv) != 0:
2471+
return errors.New("only one of env or scriptEnv may be set")
2472+
case len(c.Env) != 0:
2473+
env = c.Env
2474+
case len(c.ScriptEnv) != 0:
2475+
env = c.ScriptEnv
2476+
}
2477+
for key, value := range env {
2478+
if strings.HasPrefix(key, "CHEZMOI_") {
2479+
c.errorf("warning: %s: overriding reserved environment variable", key)
2480+
}
2481+
if err := os.Setenv(key, value); err != nil {
2482+
return err
2483+
}
2484+
}
2485+
return nil
2486+
}
2487+
24682488
// sourceAbsPaths returns the source absolute paths for each target path in
24692489
// args.
24702490
func (c *Config) sourceAbsPaths(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[!exec:git] skip 'git not found in $PATH'
2+
3+
[windows] unix2dos golden/.file
4+
5+
mkgitconfig
6+
7+
# create a dotfile repo with a config file template
8+
exec chezmoi init
9+
exec chezmoi git add .
10+
exec chezmoi git commit -- --message 'Initial commit'
11+
12+
chhome home2/user
13+
14+
# test that chezmoi init --apply sets environment variables during apply from the config file
15+
exec chezmoi init --apply file://$WORK/home/user/.local/share/chezmoi
16+
cmp $HOME/.file golden/.file
17+
18+
-- golden/.file --
19+
TEST_CONFIG_VAR=test_config_value
20+
-- home/user/.local/share/chezmoi/.chezmoi.yaml.tmpl --
21+
env:
22+
TEST_CONFIG_VAR: test_config_value
23+
-- home/user/.local/share/chezmoi/dot_file.tmpl --
24+
TEST_CONFIG_VAR={{ env "TEST_CONFIG_VAR" }}

0 commit comments

Comments
 (0)