-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <appleboy.tw@gmail.com>
- Loading branch information
Showing
1 changed file
with
30 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,51 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/appleboy/CodeGPT/git" | ||
|
||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
hookCmd.AddCommand(hookInstallCmd) | ||
hookCmd.AddCommand(hookUninstallCmd) | ||
} | ||
|
||
// hookCmd represents the command for installing/uninstalling the prepare-commit-msg hook. | ||
var hookCmd = &cobra.Command{ | ||
Use: "hook", | ||
Short: "install/uninstall git prepare-commit-msg hook", | ||
Args: cobra.MinimumNArgs(1), | ||
} | ||
|
||
// hookInstallCmd installs the prepare-commit-msg hook. | ||
var hookInstallCmd = &cobra.Command{ | ||
Use: "install", | ||
Short: "install git prepare-commit-msg hook", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if args[0] != "install" && args[0] != "uninstall" { | ||
return errors.New("only support install or uninstall command") | ||
g := git.New() | ||
|
||
if err := g.InstallHook(); err != nil { | ||
return err | ||
} | ||
color.Green("Install git hook: prepare-commit-msg successfully") | ||
color.Green("You can see the hook file: .git/hooks/prepare-commit-msg") | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
// hookUninstallCmd uninstalls the prepare-commit-msg hook. | ||
var hookUninstallCmd = &cobra.Command{ | ||
Use: "uninstall", | ||
Short: "uninstall git prepare-commit-msg hook", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
g := git.New() | ||
|
||
switch args[0] { | ||
case "install": | ||
if err := g.InstallHook(); err != nil { | ||
return err | ||
} | ||
color.Green("Install git hook: prepare-commit-msg successfully") | ||
color.Green("You can see the hook file: .git/hooks/prepare-commit-msg") | ||
case "uninstall": | ||
if err := g.UninstallHook(); err != nil { | ||
return err | ||
} | ||
color.Green("Remove git hook: prepare-commit-msg successfully") | ||
if err := g.UninstallHook(); err != nil { | ||
return err | ||
} | ||
|
||
color.Green("Remove git hook: prepare-commit-msg successfully") | ||
return nil | ||
}, | ||
} |