2
2
package hook
3
3
4
4
import (
5
+ "bufio"
6
+ "io"
5
7
"os"
6
8
"path/filepath"
9
+ "strings"
7
10
)
8
11
9
- // CommitMsgHook represent commit-msg hook file name
10
- const CommitMsgHook = "commit-msg"
12
+ // commitMsgHook represent commit-msg hook file name
13
+ const commitMsgHook = "commit-msg"
11
14
12
- const hookFile = `#!/bin/sh
13
-
14
- commitlint lint --message $1
15
- `
16
-
17
- // WriteToFile util func to write commit-msg hook to given file
18
- func WriteToFile (hookDir string ) (retErr error ) {
19
- hookFilePath := filepath .Join (hookDir , filepath .Clean (CommitMsgHook ))
20
- // if commit-msg already exists skip creating or overwriting it
21
- if _ , err := os .Stat (hookFilePath ); ! os .IsNotExist (err ) {
22
- return nil
23
- }
15
+ func WriteHooks (outDir string , confPath string ) (retErr error ) {
16
+ hookFilePath := filepath .Join (outDir , commitMsgHook )
24
17
// commit-msg needs to be executable
25
18
file , err := os .OpenFile (hookFilePath , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0700 )
26
19
if err != nil {
@@ -32,7 +25,24 @@ func WriteToFile(hookDir string) (retErr error) {
32
25
retErr = err1
33
26
}
34
27
}()
28
+ return writeTo (file , confPath )
29
+ }
30
+
31
+ // writeTo util func to write commit-msg hook to given io.Writer
32
+ func writeTo (wr io.Writer , confPath string ) error {
33
+ w := bufio .NewWriter (wr )
34
+
35
+ w .WriteString ("#!/bin/sh" )
36
+ w .WriteString ("\n \n commitlint lint" )
37
+
38
+ confPath = strings .TrimSpace (confPath )
39
+ if confPath != "" {
40
+ confPath = filepath .Clean (confPath )
41
+ w .WriteString (` --config "` + confPath + `"` )
42
+ }
43
+
44
+ w .WriteString (" --message $1" )
45
+ w .WriteString ("\n " )
35
46
36
- _ , err = file .WriteString (hookFile )
37
- return err
47
+ return w .Flush ()
38
48
}
0 commit comments