Skip to content

Commit 15f4908

Browse files
authored
feat: introduce hook management commands for git hooks (#186)
- Remove unused import of `errors` - Add an `init` function to register `hookInstallCmd` and `hookUninstallCmd` commands - Remove argument validation and error handling from `hookCmd` - Introduce `hookInstallCmd` command for installing the prepare-commit-msg hook - Introduce `hookUninstallCmd` command for uninstalling the prepare-commit-msg hook Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 3654952 commit 15f4908

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

cmd/hook.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
package cmd
22

33
import (
4-
"errors"
5-
64
"github.com/appleboy/CodeGPT/git"
75

86
"github.com/fatih/color"
97
"github.com/spf13/cobra"
108
)
119

10+
func init() {
11+
hookCmd.AddCommand(hookInstallCmd)
12+
hookCmd.AddCommand(hookUninstallCmd)
13+
}
14+
15+
// hookCmd represents the command for installing/uninstalling the prepare-commit-msg hook.
1216
var hookCmd = &cobra.Command{
1317
Use: "hook",
1418
Short: "install/uninstall git prepare-commit-msg hook",
15-
Args: cobra.MinimumNArgs(1),
19+
}
20+
21+
// hookInstallCmd installs the prepare-commit-msg hook.
22+
var hookInstallCmd = &cobra.Command{
23+
Use: "install",
24+
Short: "install git prepare-commit-msg hook",
1625
RunE: func(cmd *cobra.Command, args []string) error {
17-
if args[0] != "install" && args[0] != "uninstall" {
18-
return errors.New("only support install or uninstall command")
26+
g := git.New()
27+
28+
if err := g.InstallHook(); err != nil {
29+
return err
1930
}
31+
color.Green("Install git hook: prepare-commit-msg successfully")
32+
color.Green("You can see the hook file: .git/hooks/prepare-commit-msg")
2033

34+
return nil
35+
},
36+
}
37+
38+
// hookUninstallCmd uninstalls the prepare-commit-msg hook.
39+
var hookUninstallCmd = &cobra.Command{
40+
Use: "uninstall",
41+
Short: "uninstall git prepare-commit-msg hook",
42+
RunE: func(cmd *cobra.Command, args []string) error {
2143
g := git.New()
2244

23-
switch args[0] {
24-
case "install":
25-
if err := g.InstallHook(); err != nil {
26-
return err
27-
}
28-
color.Green("Install git hook: prepare-commit-msg successfully")
29-
color.Green("You can see the hook file: .git/hooks/prepare-commit-msg")
30-
case "uninstall":
31-
if err := g.UninstallHook(); err != nil {
32-
return err
33-
}
34-
color.Green("Remove git hook: prepare-commit-msg successfully")
45+
if err := g.UninstallHook(); err != nil {
46+
return err
3547
}
36-
48+
color.Green("Remove git hook: prepare-commit-msg successfully")
3749
return nil
3850
},
3951
}

0 commit comments

Comments
 (0)