Skip to content

Commit

Permalink
feat: Set CHEZMOI* env vars when running cd command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Feb 5, 2023
1 parent 90c1daf commit a0fbd37
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions assets/chezmoi.io/docs/reference/commands/cd.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ attempt to detect your shell and finally fall back to an OS-specific default.
If the optional argument *path* is present, the shell will be launched in the
source directory corresponding to *path*.

The shell will have various `CHEZMOI*` environment variables set, as for
scripts.

!!! hint

This does not change the current directory of the current shell. To do
Expand All @@ -21,4 +24,6 @@ source directory corresponding to *path*.

```console
$ chezmoi cd
$ chezmoi cd ~
$ chezmoi cd ~/.config
```
3 changes: 3 additions & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ type Config struct {
sourceState *chezmoi.SourceState
sourceStateErr error
templateData *templateData
runEnv []string

stdin io.Reader
stdout io.Writer
Expand Down Expand Up @@ -1903,6 +1904,7 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
for key, value := range c.ScriptEnv {
scriptEnv = append(scriptEnv, key+"="+value)
}
c.runEnv = scriptEnv
realSystem.SetScriptEnv(scriptEnv)

return nil
Expand Down Expand Up @@ -2078,6 +2080,7 @@ func (c *Config) run(dir chezmoi.AbsPath, name string, args []string) error {
}
cmd.Dir = dirRawAbsPath.String()
}
cmd.Env = c.runEnv
cmd.Stdin = c.stdin
cmd.Stdout = c.stdout
cmd.Stderr = c.stderr
Expand Down
23 changes: 13 additions & 10 deletions pkg/cmd/testdata/scripts/cd_unix.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ chmod 755 bin/shell
# test that chezmoi cd creates source directory if needed
exec chezmoi cd
exists $CHEZMOISOURCEDIR
grep ${CHEZMOISOURCEDIR@R} shell.log
rm shell.log
grep ${CHEZMOISOURCEDIR@R} pwd.log
rm pwd.log

# test that chezmoi cd changes into an existing directory
# test that chezmoi cd changes into an existing directory and sets the CHEZMOI environment variable
exec chezmoi cd
grep ${CHEZMOISOURCEDIR@R} shell.log
rm shell.log
grep CHEZMOI=1 env.log
rm env.log
grep ${CHEZMOISOURCEDIR@R} pwd.log
rm pwd.log

# test chat chezmoi cd with an argument changes into the corresponding source directory
exec chezmoi cd $HOME${/}.dir
grep ${CHEZMOISOURCEDIR@R}/dot_dir shell.log
rm shell.log
grep ${CHEZMOISOURCEDIR@R}/dot_dir pwd.log
rm pwd.log

# test that chezmoi cd works when $SHELL environment variable contains spaces
env SHELL='shell arg1'
Expand All @@ -35,13 +37,14 @@ chhome home3/user

# test that chezmoi cd $HOME with .chezmoiroot changes into .chezmoiroot
exec chezmoi cd $HOME
grep ${CHEZMOISOURCEDIR@R}/home shell.log
rm shell.log
grep ${CHEZMOISOURCEDIR@R}/home pwd.log
rm pwd.log

-- bin/shell --
#!/bin/sh

pwd > $WORK/shell.log
echo CHEZMOI=$CHEZMOI > $WORK/env.log
pwd > $WORK/pwd.log
echo shell $*
-- home/user/.dir/.keep --
-- home/user/.local/share/chezmoi/dot_dir/.keep --
Expand Down

0 comments on commit a0fbd37

Please sign in to comment.