Skip to content

Commit

Permalink
[skip travis] install hooks from config
Browse files Browse the repository at this point in the history
  • Loading branch information
A.A.Abroskin committed Mar 5, 2019
1 parent 4de163b commit ec21d17
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,29 @@ import (

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var availableHooks = [...]string{
"applypatch-msg",
"pre-applypatch",
"post-applypatch",
"pre-commit",
"prepare-commit-msg",
"commit-msg",
"post-commit",
"pre-rebase",
"post-checkout",
"post-merge",
"pre-push",
"pre-receive",
"update",
"post-receive",
"post-update",
"pre-auto-gc",
"post-rewrite",
}

var installCmd = &cobra.Command{
Use: "install",
Short: "Write basic configuration file in your project repository. Or initialize existed config",
Expand Down Expand Up @@ -41,14 +62,32 @@ source_dir_local: ".hookah-local"

// AddGitHooks write existed directories in source_dir as hooks in .git/hooks
func AddGitHooks(fs afero.Fs) {
// add directory hooks
dirs, err := afero.ReadDir(fs, getSourceDir())
if err != nil {
return
if err == nil {
for _, f := range dirs {
if f.IsDir() {
addHook(f.Name(), fs)
}
}
}

// add config hooks
var dirsHooks []string
for _, dir := range dirs {
dirsHooks = append(dirsHooks, dir.Name())
}

var configHooks []string
for _, key := range availableHooks {
if viper.Get(key) != nil {
configHooks = append(configHooks, key)
}
}

for _, f := range dirs {
if f.IsDir() {
addHook(f.Name(), fs)
for _, key := range configHooks {
if !contains(dirsHooks, key) {
addHook(key, fs)
}
}
}
Expand All @@ -60,3 +99,12 @@ func getConfigYamlPath() string {
func getConfigLocalYamlPath() string {
return filepath.Join(getRootPath(), configLocalFileName) + configExtension
}

func contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}

0 comments on commit ec21d17

Please sign in to comment.