Skip to content

Commit

Permalink
fix: Set env vars from config file when re-reading config
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 29, 2023
1 parent 6effb0a commit d07689e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
50 changes: 35 additions & 15 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,16 @@ func (c *Config) createAndReloadConfigFile(cmd *cobra.Command) error {
if err := c.decodeConfigBytes(configTemplate.format, configFileContents, &c.ConfigFile); err != nil {
return fmt.Errorf("%s: %w", configTemplate.sourceAbsPath, err)
}
return c.setEncryption()

if err := c.setEncryption(); err != nil {
return err
}

if err := c.setEnvironmentVariables(); err != nil {
return err
}

return nil
}

// createConfigFile creates a config file using a template and returns its
Expand Down Expand Up @@ -2175,20 +2184,9 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
os.Setenv(key, valueStr)
}
}
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)
}
os.Setenv(key, value)

if err := c.setEnvironmentVariables(); err != nil {
return err
}

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

// setEnvironmentVariables sets all environment variables defined in c.
func (c *Config) setEnvironmentVariables() error {
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)
}
if err := os.Setenv(key, value); err != nil {
return err
}
}
return nil
}

// sourceAbsPaths returns the source absolute paths for each target path in
// args.
func (c *Config) sourceAbsPaths(
Expand Down
24 changes: 24 additions & 0 deletions internal/cmd/testdata/scripts/issue3371.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[!exec:git] skip 'git not found in $PATH'

[windows] unix2dos golden/.file

mkgitconfig

# create a dotfile repo with a config file template
exec chezmoi init
exec chezmoi git add .
exec chezmoi git commit -- --message 'Initial commit'

chhome home2/user

# test that chezmoi init --apply sets environment variables during apply from the config file
exec chezmoi init --apply file://$WORK/home/user/.local/share/chezmoi
cmp $HOME/.file golden/.file

-- golden/.file --
TEST_CONFIG_VAR=test_config_value
-- home/user/.local/share/chezmoi/.chezmoi.yaml.tmpl --
env:
TEST_CONFIG_VAR: test_config_value
-- home/user/.local/share/chezmoi/dot_file.tmpl --
TEST_CONFIG_VAR={{ env "TEST_CONFIG_VAR" }}

0 comments on commit d07689e

Please sign in to comment.