-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (58 loc) · 1.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
var (
version = "v1.0.0-alpha.1"
commitMsgFile = flag.String("f", "", "Path to the commit message file")
shouldInit = flag.Bool("init", false, "Install git hook in the current repository")
shouldDisplayVersion = flag.Bool("version", false, "Display version of the binary")
hookCmd = "ccc -f $@"
)
func DisplayVersion() {
fmt.Printf("Conventional Commit Checker\nCCC - %s\n", version)
os.Exit(0)
}
func InitHook() error {
cwd, err := os.Getwd()
CheckErr(err)
gitFolder, err := FindFolderInParent(cwd, ".git")
CheckErr(err)
hooksFolder := filepath.Join(gitFolder, "hooks")
err = CreateFolderIfNotExists(hooksFolder, 0755)
CheckErr(err)
hookFile := filepath.Join(hooksFolder, "commit-msg")
data := ReadFile(hookFile)
if !strings.Contains(data, hookCmd) {
WriteFile(hookFile, fmt.Sprintf("%s\n%s\n", data, hookCmd))
fmt.Println("Conventional Commit Checker Git Hook Installed")
}
fmt.Println("Conventional Commit Checker Git Hook Already Installed")
return nil
}
func main() {
flag.Parse()
if *shouldDisplayVersion {
DisplayVersion()
}
if *shouldInit {
err := InitHook()
CheckErr(err)
os.Exit(0)
}
if *commitMsgFile != "" {
msg := ReadFile(*commitMsgFile)
err := ParseCommit(msg)
if err != nil {
log.Println(err)
log.Fatal(errors.New("commit message should follow the conventional commit format: <type>[optional scope]: <description>"))
}
os.Exit(0)
}
}