Skip to content

Commit

Permalink
Merge pull request #126 from Evilweed/feat/multiple-extends
Browse files Browse the repository at this point in the history
Feature: Ability to use multiple extends
  • Loading branch information
Abroskin Alexander authored May 19, 2020
2 parents ee46876 + f0963d9 commit 77816df
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
27 changes: 26 additions & 1 deletion cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -33,7 +34,7 @@ func TestInstallCmdExecutor(t *testing.T) {
"prepare-commit-msg",
}

files, err := afero.ReadDir(fs,getGitHooksDir())
files, err := afero.ReadDir(fs, getGitHooksDir())
assert.NoError(t, err)

actualFiles := []string{}
Expand Down Expand Up @@ -89,6 +90,30 @@ func TestAddCmdExecutor(t *testing.T) {
assert.Equal(t, expectedDirs, actualDirs, "Haven`t renamed file with .old extension")
}

func TestExtendsProperty(t *testing.T) {
var yamlExample = "extends:"
var yamlExampleArray = []byte(yamlExample + "\n- c1.yml\n- c2.yml")
var yamlExampleString = []byte(yamlExample + " 'c3.yml'")

var expectedPathsArray = []string{"c1.yml", "c2.yml"}
var expectedPathsString = []string{"c3.yml"}
viper.SetConfigType("yaml")

viper.ReadConfig(bytes.NewBuffer([]byte("")))
assert.False(t, isConfigExtends(), "Should not detect extends property")

viper.ReadConfig(bytes.NewBuffer(yamlExampleString))
paths := getExtendsPath()

assert.True(t, isConfigExtends(), "Should detect extends property")
assert.Equal(t, paths, expectedPathsString, "Extends path does not match for string value")

viper.ReadConfig(bytes.NewBuffer(yamlExampleArray))
paths = getExtendsPath()
assert.Equal(t, paths, expectedPathsArray, "Extends path does not match for array value")

}

func presetConfig(fs afero.Fs) {
viper.SetDefault(configSourceDirKey, ".lefthook")

Expand Down
22 changes: 12 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ func initConfig() {
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)
for _, path := range getExtendsPath() {
filename := filepath.Base(path)
extension := filepath.Ext(path)
name := filename[0 : len(filename)-len(extension)]
viper.SetConfigName(name)
viper.AddConfigPath(filepath.Dir(path))
err := viper.MergeInConfig()
check(err)
}
}

viper.AutomaticEnv()
Expand Down Expand Up @@ -133,11 +135,11 @@ func check(e error) {
}

func isConfigExtends() bool {
return viper.GetString(configExtendsOption) != ""
return len(viper.GetStringSlice(configExtendsOption)) > 0
}

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

// EnableColors shows is colors supported for current output or not.
Expand Down
6 changes: 4 additions & 2 deletions docs/full_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,11 @@ If you need to extend config from some another place, just add top level:
```yml
# lefthook.yml
extends: $HOME/work/lefthook-extend.yml
extends:
- $HOME/work/lefthook-extend.yml
- $HOME/work/lefthook-extend-2.yml
```
NOTE: File for extend should have name NOT a "lefthook.yml"
NOTE: Files for extend should have name NOT a "lefthook.yml" and should be unique.

## Referencing commands from lefthook.yml

Expand Down

0 comments on commit 77816df

Please sign in to comment.