Skip to content

Commit

Permalink
feat: Add CHEZMOI_COMMAND_DIR env var for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 1, 2023
1 parent e68328e commit a9d389e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
3 changes: 2 additions & 1 deletion assets/chezmoi.io/docs/reference/configuration-file/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ after *event* has occurred.
```

When running hooks, the `CHEZMOI=1` and `CHEZMOI_*` environment variables will
be set. `CHEZMOI_COMMAND` is set to the chezmoi command being run and
be set. `CHEZMOI_COMMAND` is set to the chezmoi command being run,
`CHEZMOI_COMMAND_DIR` is set to the directory where chezmoi was run from, and
`CHEZMOI_ARGS` contains the full arguments to chezmoi, starting with the path to
chezmoi's executable.
20 changes: 14 additions & 6 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ type Config struct {
logger *zerolog.Logger

// Computed configuration.
commandDirAbsPath chezmoi.AbsPath
homeDirAbsPath chezmoi.AbsPath
encryption chezmoi.Encryption
sourceDirAbsPath chezmoi.AbsPath
Expand Down Expand Up @@ -253,6 +254,7 @@ type templateData struct {
args []string
cacheDir chezmoi.AbsPath
command string
commandDir chezmoi.AbsPath
config map[string]any
configFile chezmoi.AbsPath
executable chezmoi.AbsPath
Expand Down Expand Up @@ -482,6 +484,14 @@ func newConfig(options ...configOption) (*Config, error) {
}
}

wd, err := os.Getwd()
if err != nil {
return nil, err
}
c.commandDirAbsPath, err = chezmoi.NormalizePath(wd)
if err != nil {
return nil, err
}
c.homeDirAbsPath, err = chezmoi.NormalizePath(c.homeDir)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1418,6 +1428,7 @@ func (c *Config) getTemplateDataMap(cmd *cobra.Command) map[string]any {
"args": templateData.args,
"cacheDir": templateData.cacheDir.String(),
"command": templateData.command,
"commandDir": templateData.commandDir.String(),
"config": templateData.config,
"configFile": templateData.configFile.String(),
"executable": templateData.executable.String(),
Expand Down Expand Up @@ -2154,6 +2165,7 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
"ARGS": strings.Join(templateData.args, " "),
"CACHE_DIR": templateData.cacheDir.String(),
"COMMAND": templateData.command,
"COMMAND_DIR": templateData.commandDir.String(),
"CONFIG_FILE": templateData.configFile.String(),
"EXECUTABLE": templateData.executable.String(),
"FQDN_HOSTNAME": templateData.fqdnHostname,
Expand Down Expand Up @@ -2325,6 +2337,7 @@ func (c *Config) newTemplateData(cmd *cobra.Command) *templateData {
args: os.Args,
cacheDir: c.CacheDirAbsPath,
command: cmd.Name(),
commandDir: c.commandDirAbsPath,
config: c.ConfigFile.toMap(),
configFile: c.configFileAbsPath,
executable: chezmoi.NewAbsPath(executable),
Expand Down Expand Up @@ -2648,13 +2661,8 @@ func (c *Config) targetValidArgs(
}

if !filepath.IsAbs(toComplete) {
wd, err := os.Getwd()
if err != nil {
cobra.CompErrorln(err.Error())
return nil, cobra.ShellCompDirectiveError
}
for i, completion := range completions {
completions[i] = strings.TrimPrefix(completion, wd+"/")
completions[i] = strings.TrimPrefix(completion, c.commandDirAbsPath.String()+"/")
}
}

Expand Down
6 changes: 1 addition & 5 deletions internal/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,9 @@ func (c *Config) fromYamlTemplateFunc(s string) any {
}

func (c *Config) globTemplateFunc(pattern string) []string {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
defer func() {
value := recover()
err := os.Chdir(wd)
err := os.Chdir(c.commandDirAbsPath.String())
if value != nil {
panic(value)
}
Expand Down

0 comments on commit a9d389e

Please sign in to comment.