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

add-extends-option #80

Merged
merged 1 commit into from
Oct 1, 2019
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
add-extends-option
  • Loading branch information
Alexander Abroskin committed Oct 1, 2019
commit 1a4b0ee155eb66ae6f3c365164012bee9332605a
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## master (unreleased)

- [Commit](https://github.com/Arkweid/lefthook/commit/0940b725729788c334bdd1e983f23e3340b1eef0)
Option `extends` for top level config added. Now you can merge some settings from different places:
```yml
extends: $HOME/work/lefthook-extend.yml
```

- [Commit](https://github.com/Arkweid/lefthook/commit/83cf818106dbf222ea33ba86aafce8f30d7cb5a9)
Add examples to generated lefthook.yml

Expand Down
19 changes: 19 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
rootExecutionRelPath string = "."
configFileName string = "lefthook"
configLocalFileName string = "lefthook-local"
configExtendsOption string = "extends"
configExtension string = ".yml"
defaultFilePermission os.FileMode = 0755
defaultDirPermission os.FileMode = 0666
Expand Down Expand Up @@ -87,6 +88,16 @@ func initConfig() {
viper.SetConfigName(configLocalFileName)
viper.MergeInConfig()

if isConfigExtends() {
filename := filepath.Base(getExtendsPath())
extension := filepath.Ext(getExtendsPath())
name := filename[0 : len(filename)-len(extension)]
viper.SetConfigName(name)
viper.AddConfigPath(filepath.Dir(getExtendsPath()))
err := viper.MergeInConfig()
check(err)
}

viper.AutomaticEnv()
}

Expand All @@ -112,6 +123,14 @@ func check(e error) {
}
}

func isConfigExtends() bool {
return viper.GetString(configExtendsOption) != ""
}

func getExtendsPath() string {
return viper.GetString(configExtendsOption)
}

// EnableColors shows is colors supported for current output or not.
// If `colors` explicitly specified in config, will return this value.
// Otherwise enabled for TTY and disabled for non-terminal output.
Expand Down
15 changes: 15 additions & 0 deletions docs/full_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ If we have a lot of commands and scripts we can tag them and run skip commands w
For example, if we have `lefthook.yml` like this:

```yml
# lefthook.yml

pre-push:
commands:
packages-audit:
Expand All @@ -241,6 +243,8 @@ pre-push:
## Piped option
If any command in the sequence fails, the other will not be executed.
```yml
# lefthook.yml

database:
piped: true
commands:
Expand All @@ -252,6 +256,15 @@ database:
run: rake db:seed
```

## Extends option
If you need to extend config from some another place, just add top level:
```yml
# lefthook.yml

extends: $HOME/work/lefthook-extend.yml
```
NOTE: File for extend should have name NOT a "lefthook.yml"

## Referencing commands from lefthook.yml

If you have the following config
Expand Down Expand Up @@ -333,6 +346,8 @@ lefthook run lint

```yml
# lefthook.yml
color: false
extends: $HOME/work/lefthook-extend.yml

pre-commit:
commands:
Expand Down