Skip to content

Commit

Permalink
feature: Expand env variables (#391)
Browse files Browse the repository at this point in the history
Closes #178
  • Loading branch information
mrexox authored Nov 30, 2022
1 parent 26309e3 commit daee7bb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
23 changes: 23 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,29 @@ pre-commit:
run: bundle exec rspec
```

#### Extending PATH

If your hook is run by GUI program, and you use some PATH tweaks in your ~/.<shell>rc, you might see an error saying *executable not found*. In that case You can extend the **$PATH** variable with `lefthook-local.yml` configuration the following way.

```yml
# lefthook.yml
pre-commit:
commands:
test:
run: yarn test
```

```yml
# lefthook-local.yml
pre-commit:
commands:
test:
env:
PATH: $PATH:/home/me/path/to/yarn
```

**Notes**

This option is useful when using lefthook on different OSes or shells where ENV variables are set in different ways.
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/runner/execute_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (e CommandExecutor) Execute(opts ExecuteOptions) (*bytes.Buffer, error) {
for name, value := range opts.env {
envList = append(
envList,
fmt.Sprintf("%s=%s", strings.ToUpper(name), value),
fmt.Sprintf("%s=%s", strings.ToUpper(name), os.ExpandEnv(value)),
)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/runner/execute_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (e CommandExecutor) Execute(opts ExecuteOptions) (*bytes.Buffer, error) {
for name, value := range opts.env {
envList = append(
envList,
fmt.Sprintf("%s=%s", strings.ToUpper(name), value),
fmt.Sprintf("%s=%s", strings.ToUpper(name), os.ExpandEnv(value)),
)
}

Expand Down
2 changes: 0 additions & 2 deletions internal/templates/hook.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,4 @@ call_lefthook()
fi
}

{{.AutoInstall}}

call_lefthook "run {{.HookName}} $@"
5 changes: 2 additions & 3 deletions internal/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ const checksumFormat = "%s %d\n"
var templatesFS embed.FS

type hookTmplData struct {
AutoInstall string
HookName string
Extension string
HookName string
Extension string
}

func Hook(hookName string) []byte {
Expand Down

0 comments on commit daee7bb

Please sign in to comment.