Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Set CHEZMOI_SOURCE_FILE env var for scripts #3518

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/chezmoi/realsystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func (s *RealSystem) RunScript(scriptname RelPath, dir AbsPath, data []byte, opt
if err != nil {
return err
}
cmd.Env = append(os.Environ(),
"CHEZMOI_SOURCE_FILE="+options.SourceRelPath.String(),
)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
4 changes: 4 additions & 0 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,9 @@ func (s *SourceState) newModifyTargetStateEntryFunc(

// Run the modifier on the current contents.
cmd := interpreter.ExecCommand(tempFile.Name())
cmd.Env = append(os.Environ(),
"CHEZMOI_SOURCE_FILE="+sourceRelPath.String(),
)
cmd.Stdin = bytes.NewReader(currentContents)
cmd.Stderr = os.Stderr
contents, err = chezmoilog.LogCmdOutput(cmd)
Expand Down Expand Up @@ -1911,6 +1914,7 @@ func (s *SourceState) newScriptTargetStateEntryFunc(
sourceAttr: SourceAttr{
Condition: fileAttr.Condition,
},
sourceRelPath: sourceRelPath,
}, nil
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/chezmoi/sourcestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ func TestSourceStateRead(t *testing.T) {
sourceAttr: SourceAttr{
Condition: ScriptConditionAlways,
},
sourceRelPath: NewSourceRelPath("run_script"),
},
},
}),
Expand Down Expand Up @@ -1046,6 +1047,7 @@ func TestSourceStateRead(t *testing.T) {
sourceAttr: SourceAttr{
Condition: ScriptConditionAlways,
},
sourceRelPath: NewSourceRelPath("run_script"),
},
},
}),
Expand Down
5 changes: 3 additions & 2 deletions internal/chezmoi/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

type RunScriptOptions struct {
Interpreter *Interpreter
Condition ScriptCondition
Interpreter *Interpreter
Condition ScriptCondition
SourceRelPath SourceRelPath
}

// A System reads from and writes to a filesystem, runs scripts, and persists
Expand Down
14 changes: 8 additions & 6 deletions internal/chezmoi/targetstateentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ type TargetStateRemove struct{}
// A TargetStateScript represents the state of a script.
type TargetStateScript struct {
*lazyContents
name RelPath
interpreter *Interpreter
condition ScriptCondition
sourceAttr SourceAttr
name RelPath
interpreter *Interpreter
condition ScriptCondition
sourceAttr SourceAttr
sourceRelPath SourceRelPath
}

// A TargetStateSymlink represents the state of a symlink in the target state.
Expand Down Expand Up @@ -333,8 +334,9 @@ func (t *TargetStateScript) Apply(
runAt := time.Now().UTC()
if !isEmpty(contents) {
if err := system.RunScript(t.name, actualStateEntry.Path().Dir(), contents, RunScriptOptions{
Condition: t.condition,
Interpreter: t.interpreter,
Condition: t.condition,
Interpreter: t.interpreter,
SourceRelPath: t.sourceRelPath,
}); err != nil {
return false, err
}
Expand Down
22 changes: 18 additions & 4 deletions internal/cmd/testdata/scripts/issue2934.txtar
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
[windows] skip
[windows] skip 'UNIX only'

# test that chezmoi sets environment variables for modify_ scripts
exec chezmoi apply
grep ^${CHEZMOISOURCEDIR@R}$ $HOME/.modify
exec chezmoi apply $HOME${/}.modify
grep ^CHEZMOI_SOURCE_DIR=${CHEZMOISOURCEDIR@R}$ $HOME/.modify
grep ^CHEZMOI_SOURCE_FILE=modify_dot_modify$ $HOME/.modify

chhome home2/user

# test that CHEZMOI_SOURCE_FILE environment variable is set when running scripts
exec chezmoi apply $HOME${/}script.sh
stdout ^CHEZMOI_SOURCE_DIR=${CHEZMOISOURCEDIR@R}$
stdout ^CHEZMOI_SOURCE_FILE=run_script.sh$

-- home/user/.local/share/chezmoi/modify_dot_modify --
#!/bin/sh

echo ${CHEZMOI_SOURCE_DIR}
echo CHEZMOI_SOURCE_DIR=${CHEZMOI_SOURCE_DIR}
echo CHEZMOI_SOURCE_FILE=${CHEZMOI_SOURCE_FILE}
-- home2/user/.local/share/chezmoi/run_script.sh --
#!/bin/sh

echo CHEZMOI_SOURCE_DIR=${CHEZMOI_SOURCE_DIR}
echo CHEZMOI_SOURCE_FILE=${CHEZMOI_SOURCE_FILE}
Loading