Skip to content

Commit 7203d6b

Browse files
committedOct 10, 2023
feat: Set CHEZMOI_ and scriptEnv env vars for all invoked commands
1 parent 559b5e7 commit 7203d6b

19 files changed

+22
-94
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ sections:
3939
description: Display progress bars
4040
scriptEnv:
4141
type: object
42-
description: Extra environment variables for scripts
42+
description: Extra environment variables for scripts and commands
4343
scriptTempDir:
4444
description: Temporary directory for scripts
4545
sourceDir:

‎internal/chezmoi/archivereadersystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,3 @@ func (s *ArchiveReaderSystem) Readlink(name AbsPath) (string, error) {
109109
}
110110
return "", fs.ErrNotExist
111111
}
112-
113-
// UnderlyingSystem implements System.UnderlyingSystem.
114-
func (s *ArchiveReaderSystem) UnderlyingSystem() System {
115-
return s
116-
}

‎internal/chezmoi/debugsystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,6 @@ func (s *DebugSystem) UnderlyingFS() vfs.FS {
191191
return s.system.UnderlyingFS()
192192
}
193193

194-
// UnderlyingSystem implements System.UnderlyingSystem.
195-
func (s *DebugSystem) UnderlyingSystem() System {
196-
return s.system
197-
}
198-
199194
// WriteFile implements System.WriteFile.
200195
func (s *DebugSystem) WriteFile(name AbsPath, data []byte, perm fs.FileMode) error {
201196
err := s.system.WriteFile(name, data, perm)

‎internal/chezmoi/dryrunsystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ func (s *DryRunSystem) UnderlyingFS() vfs.FS {
127127
return s.system.UnderlyingFS()
128128
}
129129

130-
// UnderlyingSystem implements System.UnderlyingSystem.
131-
func (s *DryRunSystem) UnderlyingSystem() System {
132-
return s.system
133-
}
134-
135130
// WriteFile implements System.WriteFile.
136131
func (s *DryRunSystem) WriteFile(AbsPath, []byte, fs.FileMode) error {
137132
s.setModified()

‎internal/chezmoi/dumpsystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ func (s *DumpSystem) UnderlyingFS() vfs.FS {
124124
return nil
125125
}
126126

127-
// UnderlyingSystem implements System.UnderlyingSystem.
128-
func (s *DumpSystem) UnderlyingSystem() System {
129-
return s
130-
}
131-
132127
// WriteFile implements System.WriteFile.
133128
func (s *DumpSystem) WriteFile(filename AbsPath, data []byte, perm fs.FileMode) error {
134129
return s.setData(filename.String(), &fileData{

‎internal/chezmoi/erroronwritesystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ func (s *ErrorOnWriteSystem) UnderlyingFS() vfs.FS {
114114
return s.system.UnderlyingFS()
115115
}
116116

117-
// UnderlyingSystem implements System.UnderlyingSystem.
118-
func (s *ErrorOnWriteSystem) UnderlyingSystem() System {
119-
return s.system
120-
}
121-
122117
// WriteFile implements System.WriteFile.
123118
func (s *ErrorOnWriteSystem) WriteFile(AbsPath, []byte, fs.FileMode) error {
124119
return s.err

‎internal/chezmoi/externaldiffsystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,6 @@ func (s *ExternalDiffSystem) UnderlyingFS() vfs.FS {
224224
return s.system.UnderlyingFS()
225225
}
226226

227-
// UnderlyingSystem implements System.UnderlyingSystem.
228-
func (s *ExternalDiffSystem) UnderlyingSystem() System {
229-
return s.system
230-
}
231-
232227
// WriteFile implements System.WriteFile.
233228
func (s *ExternalDiffSystem) WriteFile(filename AbsPath, data []byte, perm fs.FileMode) error {
234229
if s.filter.IncludeEntryTypeBits(EntryTypeFiles) {

‎internal/chezmoi/gitdiffsystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,6 @@ func (s *GitDiffSystem) UnderlyingFS() vfs.FS {
247247
return s.system.UnderlyingFS()
248248
}
249249

250-
// UnderlyingSystem implements System.UnderlyingSystem.
251-
func (s *GitDiffSystem) UnderlyingSystem() System {
252-
return s.system
253-
}
254-
255250
// WriteFile implements System.WriteFile.
256251
func (s *GitDiffSystem) WriteFile(filename AbsPath, data []byte, perm fs.FileMode) error {
257252
if s.filter.IncludeEntryTypeBits(EntryTypeFiles) {

‎internal/chezmoi/nullsystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,3 @@ type NullSystem struct {
44
emptySystemMixin
55
noUpdateSystemMixin
66
}
7-
8-
// UnderlyingSystem implements System.UnderlyingSystem.
9-
func (s *NullSystem) UnderlyingSystem() System {
10-
return s
11-
}

‎internal/chezmoi/readonlysystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,3 @@ func (s *ReadOnlySystem) Stat(name AbsPath) (fs.FileInfo, error) {
5858
func (s *ReadOnlySystem) UnderlyingFS() vfs.FS {
5959
return s.system.UnderlyingFS()
6060
}
61-
62-
// UnderlyingSystem implements System.UnderlyingSystem.
63-
func (s *ReadOnlySystem) UnderlyingSystem() System {
64-
return s.system
65-
}

‎internal/chezmoi/realsystem.go

-11
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,13 @@ func (s *RealSystem) RunScript(
128128
if err != nil {
129129
return err
130130
}
131-
cmd.Env = s.scriptEnv
132131
cmd.Stdin = os.Stdin
133132
cmd.Stdout = os.Stdout
134133
cmd.Stderr = os.Stderr
135134

136135
return s.RunCmd(cmd)
137136
}
138137

139-
// SetScriptEnv sets the environment variables for scripts.
140-
func (s *RealSystem) SetScriptEnv(scriptEnv []string) {
141-
s.scriptEnv = scriptEnv
142-
}
143-
144138
// Stat implements System.Stat.
145139
func (s *RealSystem) Stat(name AbsPath) (fs.FileInfo, error) {
146140
return s.fileSystem.Stat(name.String())
@@ -151,11 +145,6 @@ func (s *RealSystem) UnderlyingFS() vfs.FS {
151145
return s.fileSystem
152146
}
153147

154-
// UnderlyingSystem implements System.UnderlyingSystem.
155-
func (s *RealSystem) UnderlyingSystem() System {
156-
return s
157-
}
158-
159148
// getScriptWorkingDir returns the script's working directory.
160149
//
161150
// If this is a before_ script then the requested working directory may not

‎internal/chezmoi/realsystem_unix.go

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type RealSystem struct {
2121
safe bool
2222
createScriptTempDirOnce sync.Once
2323
scriptTempDir AbsPath
24-
scriptEnv []string
2524
devCache map[AbsPath]uint // devCache maps directories to device numbers.
2625
tempDirCache map[uint]string // tempDirCache maps device numbers to renameio temporary directories.
2726
}

‎internal/chezmoi/sourcestate.go

-9
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ type SourceState struct {
124124
defaultTemplateData map[string]any
125125
userTemplateData map[string]any
126126
priorityTemplateData map[string]any
127-
scriptEnv []string
128127
templateData map[string]any
129128
templateFuncs template.FuncMap
130129
templateOptions []string
@@ -213,13 +212,6 @@ func WithReadTemplateData(readTemplateData bool) SourceStateOption {
213212
}
214213
}
215214

216-
// WithScriptEnv sets the script environment variables.
217-
func WithScriptEnv(scriptEnv []string) SourceStateOption {
218-
return func(s *SourceState) {
219-
s.scriptEnv = scriptEnv
220-
}
221-
}
222-
223215
// WithSourceDir sets the source directory.
224216
func WithSourceDir(sourceDirAbsPath AbsPath) SourceStateOption {
225217
return func(s *SourceState) {
@@ -1911,7 +1903,6 @@ func (s *SourceState) newModifyTargetStateEntryFunc(
19111903

19121904
// Run the modifier on the current contents.
19131905
cmd := interpreter.ExecCommand(tempFile.Name())
1914-
cmd.Env = s.scriptEnv
19151906
cmd.Stdin = bytes.NewReader(currentContents)
19161907
cmd.Stderr = os.Stderr
19171908
contents, err = chezmoilog.LogCmdOutput(cmd)

‎internal/chezmoi/system.go

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ type System interface { //nolint:interfacebloat
3838
RunScript(scriptname RelPath, dir AbsPath, data []byte, options RunScriptOptions) error
3939
Stat(name AbsPath) (fs.FileInfo, error)
4040
UnderlyingFS() vfs.FS
41-
UnderlyingSystem() System
4241
WriteFile(filename AbsPath, data []byte, perm fs.FileMode) error
4342
WriteSymlink(oldname string, newname AbsPath) error
4443
}

‎internal/chezmoi/tarwritersystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ func (s *TarWriterSystem) RunScript(
5252
return s.WriteFile(NewAbsPath(scriptname.String()), data, 0o700)
5353
}
5454

55-
// UnderlyingSystem implements System.UnderlyingSystem.
56-
func (s *TarWriterSystem) UnderlyingSystem() System {
57-
return s
58-
}
59-
6055
// WriteFile implements System.WriteFile.
6156
func (s *TarWriterSystem) WriteFile(filename AbsPath, data []byte, perm fs.FileMode) error {
6257
header := s.headerTemplate

‎internal/chezmoi/zipwritersystem.go

-5
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ func (s *ZIPWriterSystem) RunScript(
5757
return s.WriteFile(NewAbsPath(scriptname.String()), data, 0o700)
5858
}
5959

60-
// UnderlyingSystem implements System.UnderlyingSystem.
61-
func (s *ZIPWriterSystem) UnderlyingSystem() System {
62-
return s
63-
}
64-
6560
// WriteFile implements System.WriteFile.
6661
func (s *ZIPWriterSystem) WriteFile(filename AbsPath, data []byte, perm fs.FileMode) error {
6762
fileHeader := zip.FileHeader{

‎internal/cmd/cdcmd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ func (c *Config) newCDCmd() *cobra.Command {
3636
}
3737

3838
func (c *Config) runCDCmd(cmd *cobra.Command, args []string) error {
39-
if _, ok := os.LookupEnv("CHEZMOI"); ok {
39+
if _, ok := os.LookupEnv("CHEZMOI_SUBSHELL"); ok {
4040
return errors.New("already in a chezmoi subshell")
4141
}
42+
os.Setenv("CHEZMOI_SUBSHELL", "1")
4243

4344
cdCommand, cdArgs, err := c.cdCommand()
4445
if err != nil {

‎internal/cmd/config.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ type Config struct {
232232
sourceState *chezmoi.SourceState
233233
sourceStateErr error
234234
templateData *templateData
235-
runEnv []string
236235

237236
stdin io.Reader
238237
stdout io.Writer
@@ -710,9 +709,7 @@ func (c *Config) createAndReloadConfigFile(cmd *cobra.Command) error {
710709
return err
711710
}
712711
c.templateData.sourceDir = sourceDirAbsPath
713-
c.runEnv = append(c.runEnv, "CHEZMOI_SOURCE_DIR="+sourceDirAbsPath.String())
714-
realSystem := c.baseSystem.UnderlyingSystem().(*chezmoi.RealSystem) //nolint:forcetypeassert
715-
realSystem.SetScriptEnv(c.runEnv)
712+
os.Setenv("CHEZMOI_SOURCE_DIR", sourceDirAbsPath.String())
716713

717714
// Find config template, execute it, and create config file.
718715
configTemplate, err := c.findConfigTemplate()
@@ -1705,7 +1702,6 @@ func (c *Config) newSourceState(
17051702
chezmoi.WithLogger(&sourceStateLogger),
17061703
chezmoi.WithMode(c.Mode),
17071704
chezmoi.WithPriorityTemplateData(c.Data),
1708-
chezmoi.WithScriptEnv(c.runEnv),
17091705
chezmoi.WithSourceDir(c.SourceDirAbsPath),
17101706
chezmoi.WithSystem(c.sourceSystem),
17111707
chezmoi.WithTemplateFuncs(c.templateFuncs),
@@ -2091,9 +2087,8 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
20912087
}
20922088
}
20932089

2094-
scriptEnv := os.Environ()
20952090
templateData := c.getTemplateData(cmd)
2096-
scriptEnv = append(scriptEnv, "CHEZMOI=1")
2091+
os.Setenv("CHEZMOI", "1")
20972092
for key, value := range map[string]string{
20982093
"ARCH": templateData.arch,
20992094
"ARGS": strings.Join(templateData.args, " "),
@@ -2112,10 +2107,10 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
21122107
"USERNAME": templateData.username,
21132108
"WORKING_TREE": templateData.workingTree.String(),
21142109
} {
2115-
scriptEnv = append(scriptEnv, "CHEZMOI_"+key+"="+value)
2110+
os.Setenv("CHEZMOI_"+key, value)
21162111
}
21172112
if c.Verbose {
2118-
scriptEnv = append(scriptEnv, "CHEZMOI_VERBOSE=1")
2113+
os.Setenv("CHEZMOI_VERBOSE", "1")
21192114
}
21202115
for groupKey, group := range map[string]map[string]any{
21212116
"KERNEL": templateData.kernel,
@@ -2124,16 +2119,14 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
21242119
"WINDOWS_VERSION": templateData.windowsVersion,
21252120
} {
21262121
for key, value := range group {
2127-
upperSnakeCaseKey := camelCaseToUpperSnakeCase(key)
2122+
key := "CHEZMOI_" + groupKey + "_" + camelCaseToUpperSnakeCase(key)
21282123
valueStr := fmt.Sprintf("%s", value)
2129-
scriptEnv = append(scriptEnv, "CHEZMOI_"+groupKey+"_"+upperSnakeCaseKey+"="+valueStr)
2124+
os.Setenv(key, valueStr)
21302125
}
21312126
}
21322127
for key, value := range c.ScriptEnv {
2133-
scriptEnv = append(scriptEnv, key+"="+value)
2128+
os.Setenv(key, value)
21342129
}
2135-
c.runEnv = scriptEnv
2136-
realSystem.SetScriptEnv(scriptEnv)
21372130

21382131
if command := c.Hooks[cmd.Name()].Pre; command.Command != "" {
21392132
if err := c.run(c.homeDirAbsPath, command.Command, command.Args); err != nil {
@@ -2326,7 +2319,6 @@ func (c *Config) run(dir chezmoi.AbsPath, name string, args []string) error {
23262319
}
23272320
cmd.Dir = dirRawAbsPath.String()
23282321
}
2329-
cmd.Env = c.runEnv
23302322
cmd.Stdin = c.stdin
23312323
cmd.Stdout = c.stdout
23322324
cmd.Stderr = c.stderr
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[unix] chmod 755 bin/chezmoi-test-command
2+
3+
# test that chezmoi sets CHEZMOI_ environment variables
4+
exec chezmoi execute-template '{{ output "chezmoi-test-command" }}'
5+
stdout 'CHEZMOI_SOURCE_DIR=.*/\.local/share/chezmoi\s?$'
6+
7+
-- bin/chezmoi-test-command --
8+
#!/bin/sh
9+
10+
echo CHEZMOI_SOURCE_DIR=${CHEZMOI_SOURCE_DIR}
11+
-- bin/chezmoi-test-command.cmd --
12+
@echo CHEZMOI_SOURCE_DIR=%CHEZMOI_SOURCE_DIR%

0 commit comments

Comments
 (0)
Please sign in to comment.