Skip to content

Commit

Permalink
✨ Add --remove flag to hook command
Browse files Browse the repository at this point in the history
  • Loading branch information
5n7-sk committed Aug 2, 2020
1 parent f93459c commit 232caed
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cli/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/fatih/color"
Expand All @@ -28,6 +29,26 @@ gmoji --hook $1
return nil
}

// RemoveHook removes the commit hook.
func (c CLI) RemoveHook() error {
p, err := c.hookPath()
if err != nil {
return err
}

if _, err := os.Stat(p); os.IsNotExist(err) {
return fmt.Errorf("%s does not exist", p)
}

if err := os.Remove(p); err != nil {
return err
}

fmt.Printf("%s\n", color.GreenString("gmoji commit hook removed successfully"))

return nil
}

func (c CLI) hookPath() (string, error) {
gitRoot, err := c.GitRoot()
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions cmd/gmoji/cmd/cmd_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@ import (
"github.com/spf13/cobra"
)

// HookOptions represents the options for hook command.
type HookOptions struct {
Remove bool
}

var (
hookOptions HookOptions
)

func runHook(cmd *cobra.Command, args []string) error {
c, err := cli.NewCLI()
if err != nil {
return err
}

if hookOptions.Remove {
if err := c.RemoveHook(); err != nil {
return err
}
return nil
}

if err := c.Hook(); err != nil {
return err
}
Expand All @@ -26,5 +42,7 @@ var hookCmd = &cobra.Command{
}

func init() {
hookCmd.Flags().BoolVarP(&hookOptions.Remove, "remove", "r", false, "remove the commit hook")

rootCmd.AddCommand(hookCmd)
}

0 comments on commit 232caed

Please sign in to comment.