diff --git a/internal/commands/root.go b/internal/commands/root.go index 662cae3..9c330b2 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -93,29 +93,19 @@ func setupHooks(allowedHooks map[string]struct{}) error { pterm.DefaultSection.Println("Generating .git/hooks") pterm.Debug.Printfln("Allowed hooks: %v", allowedHooks) - pterm.Info.Println("Writing Commit Hooks") gitHooks := githooks.NewGitHooks() - for _, v := range gitHooks.Commit { - if _, ok := allowedHooks[v.Name]; ok { - if err := writeConfig(&v); err != nil { - pterm.Error.Println("✖ Hook write failed for: ", v.Name, err.Error()) - return err + for hookName, hookGroup := range gitHooks.Hooks { + pterm.Info.Println("Writing", hookName, "Hooks") + for _, v := range hookGroup { + if _, ok := allowedHooks[v.Name]; ok { + if err := writeConfig(&v); err != nil { + pterm.Error.Println("✖ Hook write failed for: ", v.Name, err.Error()) + return err + } + pterm.Success.Println("✔ Hook written:", v.Name) + } else { + pterm.Warning.Println("Skipping hook:", v.Name, "not allowed") } - pterm.Success.Println("✔ Hook written:", v.Name) - } else { - pterm.Warning.Println("Skipping hook:", v.Name, "not allowed") - } - } - pterm.Info.Println("Writing Checkout Hooks") - for _, v := range gitHooks.Checkout { - if _, ok := allowedHooks[v.Name]; ok { - if err := writeConfig(&v); err != nil { - pterm.Error.Println("✖ Hook write failed for: ", v.Name, err.Error()) - return err - } - pterm.Success.Println("✔ Hook written:", v.Name) - } else { - pterm.Warning.Println("Skipping hook:", v.Name, "not allowed") } } diff --git a/internal/githooks/githooks.go b/internal/githooks/githooks.go index 3309919..7e3284d 100644 --- a/internal/githooks/githooks.go +++ b/internal/githooks/githooks.go @@ -7,10 +7,10 @@ import ( // hooksBaseDir is the base directory for Git hooks const hooksBaseDir = ".git/hooks" -// GitHooks contains an array of GitHook +// GitHooks represents a collection of Git hooks, each defined by a name, path, and script template. type GitHooks struct { - Commit []GitHook - Checkout []GitHook + // Hooks is a slice of GitHook representing configured Git hooks with their names, paths, and templates. + Hooks map[string][]GitHook } // GitHook represents a Git hook with its name, path, and template @@ -25,9 +25,13 @@ type GitHook struct { // NewGitHooks returns a pointer to a GitHooks instance with commit hooks initialized func NewGitHooks() *GitHooks { + hooks := map[string][]GitHook{ + "Commit": generateHooks(getCommitHook()), + "Checkout": generateHooks(getCheckoutHooks()), + } + return &GitHooks{ - Commit: generateHooks(getCommitHook()), - Checkout: generateHooks(getCheckoutHooks()), + Hooks: hooks, } } diff --git a/internal/githooks/githooks_test.go b/internal/githooks/githooks_test.go index 7e4d5d7..f746109 100644 --- a/internal/githooks/githooks_test.go +++ b/internal/githooks/githooks_test.go @@ -42,14 +42,14 @@ func TestNewGitHooks(t *testing.T) { assert.NotNil(t, hooksInstance, "NewGitHooks should not return nil") // Test commit hooks - for _, hook := range hooksInstance.Commit { + for _, hook := range hooksInstance.Hooks["Commit"] { expectedPath := filepath.Join(hooksBaseDir, hook.Name) assert.Equal(t, expectedPath, hook.Path, "commit hook Path should match expected") assert.Equal(t, gitHookTemplate, hook.Template, "commit hook Template should match gitHookTemplate") } // Test checkout hooks - for _, hook := range hooksInstance.Checkout { + for _, hook := range hooksInstance.Hooks["Checkout"] { expectedPath := filepath.Join(hooksBaseDir, hook.Name) assert.Equal(t, expectedPath, hook.Path, "checkout hook Path should match expected") assert.Equal(t, gitHookTemplate, hook.Template, "checkout hook Template should match gitHookTemplate")