Skip to content

Commit

Permalink
refactor(cmd): remove config flag in init cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
muthukrishnan24 committed Jan 29, 2022
1 parent a81a020 commit aa23278
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func lintMsg(confPath, msgPath string) error {
}

// hookCreate is the callback function for create hook command
func hookCreate(confPath string, isReplace bool) error {
return createHooks(confPath, isReplace)
func hookCreate(isReplace bool) error {
return createHooks(isReplace)
}

// configCreate is the callback function for create config command
Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,15 @@ func configCmd() *cli.Command {
}

func hookCmd() *cli.Command {
confFlag := formConfFlag()
replaceFlag := formReplaceFlag()

createCmd := &cli.Command{
Name: "create",
Usage: "Creates git hook files in current directory",
Flags: []cli.Flag{confFlag, replaceFlag},
Flags: []cli.Flag{replaceFlag},
Action: func(ctx *cli.Context) error {
confPath := ctx.String("config")
isReplace := ctx.Bool("replace")
err := hookCreate(confPath, isReplace)
err := hookCreate(isReplace)
if err != nil {
if isHookExists(err) {
fmt.Println("create failed. hook files already exists")
Expand Down
10 changes: 5 additions & 5 deletions internal/cmd/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ func initHooks(confPath string, isGlobal, isReplace bool) (string, error) {
return "", err
}

err = writeHooks(hookDir, confPath, isReplace)
err = writeHooks(hookDir, isReplace)
if err != nil {
return "", err
}
return hookDir, nil
}

func createHooks(confPath string, isReplace bool) error {
return writeHooks(hookBaseDir, confPath, isReplace)
func createHooks(isReplace bool) error {
return writeHooks(hookBaseDir, isReplace)
}

func writeHooks(hookDir, confPath string, isReplace bool) error {
func writeHooks(hookDir string, isReplace bool) error {
// if commit-msg already exists skip creating or overwriting it
if _, err := os.Stat(hookDir); !os.IsNotExist(err) {
if !isReplace {
Expand All @@ -47,7 +47,7 @@ func writeHooks(hookDir, confPath string, isReplace bool) error {
}

// create hook file
return hook.WriteHooks(hookDir, confPath)
return hook.WriteHooks(hookDir)
}

func getHookDir(baseDir string, isGlobal bool) (string, error) {
Expand Down
42 changes: 20 additions & 22 deletions internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,46 @@
package hook

import (
"bufio"
"io"
"os"
"path/filepath"
"strings"
)

// commitMsgHook represent commit-msg hook file name
const commitMsgHook = "commit-msg"

const hookMessage = `#!/bin/sh
if ! type commitlint >/dev/null 2>/dev/null; then
echo ""
echo "commitlint could not be found"
echo "try again after installing commitlint or add commitlint to PATH"
echo ""
exit 2;
fi
commitlint lint --message $1
`

// WriteHooks write git hooks to the given outDir
func WriteHooks(outDir, confPath string) (retErr error) {
func WriteHooks(outDir string) (retErr error) {
hookFilePath := filepath.Join(outDir, commitMsgHook)
// commit-msg needs to be executable
file, err := os.OpenFile(hookFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
if err != nil {
return err
}

defer func() {
err1 := file.Close()
if retErr == nil && err1 != nil {
retErr = err1
}
}()
return writeTo(file, confPath)
}

// writeTo util func to write commit-msg hook to given io.Writer
func writeTo(wr io.Writer, confPath string) error {
w := bufio.NewWriter(wr)

w.WriteString("#!/bin/sh")
w.WriteString("\n\ncommitlint lint")

confPath = strings.TrimSpace(confPath)
if confPath != "" {
confPath = filepath.Clean(confPath)
w.WriteString(` --config "` + confPath + `"`)
_, err = file.WriteString(hookMessage)
if err != nil {
return err
}

w.WriteString(" --message $1")
w.WriteString("\n")

return w.Flush()
return nil
}

0 comments on commit aa23278

Please sign in to comment.