Skip to content

Commit

Permalink
feat: support globs in extends (#853)
Browse files Browse the repository at this point in the history
* feat: support globs in extends

* chore: remove redundant dependency

* docs: add info about the glob in extends
  • Loading branch information
mrexox authored Oct 22, 2024
1 parent 67626f7 commit f3b1a81
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 209 deletions.
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ no_tty: true

You can extend your config with another one YAML file. Its content will be merged. Extends for `lefthook.yml`, `lefthook-local.yml`, and [`remote`](#remote) configs are handled separately, so you can have different extends in these files.

You can use asterisk to make a glob.

**Example**

```yml
Expand All @@ -148,6 +150,7 @@ extends:
- /home/user/work/lefthook-extend-2.yml
- lefthook-extends/file.yml
- ../extend.yml
- projects/*/specific-lefthook-config.yml
```

> [!IMPORTANT]
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/charmbracelet/lipgloss v0.13.0
github.com/creack/pty v1.1.23
github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.6.0
github.com/mattn/go-tty v0.0.7
github.com/mitchellh/mapstructure v1.5.0
github.com/rogpeppe/go-internal v1.13.1
Expand Down
39 changes: 23 additions & 16 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,28 +219,35 @@ func extend(fs afero.Fs, v *viper.Viper, root string) error {
// extendRecursive merges extends.
// If extends contain other extends they get merged too.
func extendRecursive(fs afero.Fs, v *viper.Viper, root string, extends map[string]struct{}) error {
for _, path := range v.GetStringSlice("extends") {
if _, contains := extends[path]; contains {
return fmt.Errorf("possible recursion in extends: path %s is specified multiple times", path)
for _, pathOrGlob := range v.GetStringSlice("extends") {
if !filepath.IsAbs(pathOrGlob) {
pathOrGlob = filepath.Join(root, pathOrGlob)
}
extends[path] = struct{}{}

if !filepath.IsAbs(path) {
path = filepath.Join(root, path)
paths, err := afero.Glob(fs, pathOrGlob)
if err != nil {
return fmt.Errorf("bad glob syntax for '%s': %w", pathOrGlob, err)
}

extendV := newViper(fs, root)
extendV.SetConfigFile(path)
if err := extendV.ReadInConfig(); err != nil {
return err
}
for _, path := range paths {
if _, contains := extends[path]; contains {
return fmt.Errorf("possible recursion in extends: path %s is specified multiple times", path)
}
extends[path] = struct{}{}

if err := extendRecursive(fs, extendV, root, extends); err != nil {
return err
}
extendV := newViper(fs, root)
extendV.SetConfigFile(path)
if err := extendV.ReadInConfig(); err != nil {
return err
}

if err := v.MergeConfigMap(extendV.AllSettings()); err != nil {
return err
if err := extendRecursive(fs, extendV, root, extends); err != nil {
return err
}

if err := v.MergeConfigMap(extendV.AllSettings()); err != nil {
return err
}
}
}

Expand Down
Loading

0 comments on commit f3b1a81

Please sign in to comment.