Skip to content

Commit aa23278

Browse files
refactor(cmd): remove config flag in init cmd
1 parent a81a020 commit aa23278

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

internal/cmd/callback.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func lintMsg(confPath, msgPath string) error {
4242
}
4343

4444
// hookCreate is the callback function for create hook command
45-
func hookCreate(confPath string, isReplace bool) error {
46-
return createHooks(confPath, isReplace)
45+
func hookCreate(isReplace bool) error {
46+
return createHooks(isReplace)
4747
}
4848

4949
// configCreate is the callback function for create config command

internal/cmd/cmd.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,15 @@ func configCmd() *cli.Command {
162162
}
163163

164164
func hookCmd() *cli.Command {
165-
confFlag := formConfFlag()
166165
replaceFlag := formReplaceFlag()
167166

168167
createCmd := &cli.Command{
169168
Name: "create",
170169
Usage: "Creates git hook files in current directory",
171-
Flags: []cli.Flag{confFlag, replaceFlag},
170+
Flags: []cli.Flag{replaceFlag},
172171
Action: func(ctx *cli.Context) error {
173-
confPath := ctx.String("config")
174172
isReplace := ctx.Bool("replace")
175-
err := hookCreate(confPath, isReplace)
173+
err := hookCreate(isReplace)
176174
if err != nil {
177175
if isHookExists(err) {
178176
fmt.Println("create failed. hook files already exists")

internal/cmd/hook.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ func initHooks(confPath string, isGlobal, isReplace bool) (string, error) {
2222
return "", err
2323
}
2424

25-
err = writeHooks(hookDir, confPath, isReplace)
25+
err = writeHooks(hookDir, isReplace)
2626
if err != nil {
2727
return "", err
2828
}
2929
return hookDir, nil
3030
}
3131

32-
func createHooks(confPath string, isReplace bool) error {
33-
return writeHooks(hookBaseDir, confPath, isReplace)
32+
func createHooks(isReplace bool) error {
33+
return writeHooks(hookBaseDir, isReplace)
3434
}
3535

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

4949
// create hook file
50-
return hook.WriteHooks(hookDir, confPath)
50+
return hook.WriteHooks(hookDir)
5151
}
5252

5353
func getHookDir(baseDir string, isGlobal bool) (string, error) {

internal/hook/hook.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,46 @@
22
package hook
33

44
import (
5-
"bufio"
6-
"io"
75
"os"
86
"path/filepath"
9-
"strings"
107
)
118

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

12+
const hookMessage = `#!/bin/sh
13+
14+
if ! type commitlint >/dev/null 2>/dev/null; then
15+
echo ""
16+
echo "commitlint could not be found"
17+
echo "try again after installing commitlint or add commitlint to PATH"
18+
echo ""
19+
exit 2;
20+
fi
21+
22+
commitlint lint --message $1
23+
24+
`
25+
1526
// WriteHooks write git hooks to the given outDir
16-
func WriteHooks(outDir, confPath string) (retErr error) {
27+
func WriteHooks(outDir string) (retErr error) {
1728
hookFilePath := filepath.Join(outDir, commitMsgHook)
1829
// commit-msg needs to be executable
1930
file, err := os.OpenFile(hookFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
2031
if err != nil {
2132
return err
2233
}
34+
2335
defer func() {
2436
err1 := file.Close()
2537
if retErr == nil && err1 != nil {
2638
retErr = err1
2739
}
2840
}()
29-
return writeTo(file, confPath)
30-
}
3141

32-
// writeTo util func to write commit-msg hook to given io.Writer
33-
func writeTo(wr io.Writer, confPath string) error {
34-
w := bufio.NewWriter(wr)
35-
36-
w.WriteString("#!/bin/sh")
37-
w.WriteString("\n\ncommitlint lint")
38-
39-
confPath = strings.TrimSpace(confPath)
40-
if confPath != "" {
41-
confPath = filepath.Clean(confPath)
42-
w.WriteString(` --config "` + confPath + `"`)
42+
_, err = file.WriteString(hookMessage)
43+
if err != nil {
44+
return err
4345
}
44-
45-
w.WriteString(" --message $1")
46-
w.WriteString("\n")
47-
48-
return w.Flush()
46+
return nil
4947
}

0 commit comments

Comments
 (0)