|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "errors" |
5 |
| - |
6 | 4 | "github.com/appleboy/CodeGPT/git"
|
7 | 5 |
|
8 | 6 | "github.com/fatih/color"
|
9 | 7 | "github.com/spf13/cobra"
|
10 | 8 | )
|
11 | 9 |
|
| 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. |
12 | 16 | var hookCmd = &cobra.Command{
|
13 | 17 | Use: "hook",
|
14 | 18 | 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", |
16 | 25 | 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 |
19 | 30 | }
|
| 31 | + color.Green("Install git hook: prepare-commit-msg successfully") |
| 32 | + color.Green("You can see the hook file: .git/hooks/prepare-commit-msg") |
20 | 33 |
|
| 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 { |
21 | 43 | g := git.New()
|
22 | 44 |
|
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 |
35 | 47 | }
|
36 |
| - |
| 48 | + color.Green("Remove git hook: prepare-commit-msg successfully") |
37 | 49 | return nil
|
38 | 50 | },
|
39 | 51 | }
|
0 commit comments