-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprecommit-vet-lint.go
61 lines (54 loc) · 1.39 KB
/
precommit-vet-lint.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func main() {
lintErrors := make([]string, 0)
vetErrors := make([]string, 0)
stagedFiles := GetStagedFiles()
temp := TempDir{}
temp.Create()
defer temp.Close()
for _, file := range stagedFiles {
fileDir, fileName := filepath.Split(file)
tempFileDir := filepath.Join(temp.Path, fileDir)
if _, err := os.Stat(tempFileDir); os.IsNotExist(err) {
err = os.MkdirAll(tempFileDir, os.ModePerm)
}
tempFilePath := filepath.Join(tempFileDir, fileName)
stagedContent := GetStagedContent(file)
err := ioutil.WriteFile(tempFilePath, stagedContent, 0655)
CheckError(err)
lintErrorMessage, err := GetLintErrors(tempFilePath)
if err != nil {
lintErrors = append(lintErrors, strings.Replace(lintErrorMessage, tempFilePath, file, -1))
}
vetErrorMessage, err := GetVetErrors(tempFilePath)
if err != nil {
vetErrors = append(vetErrors, strings.Replace(vetErrorMessage, tempFilePath, file, -1))
}
}
if len(lintErrors) != 0 {
fmt.Println("Following linting error(s) found:")
fmt.Println()
for _, errMsg := range lintErrors {
fmt.Println(errMsg)
}
fmt.Println()
fmt.Println()
}
if len(vetErrors) != 0 {
fmt.Println("Following vet error(s) found:")
fmt.Println()
for _, errMsg := range vetErrors {
fmt.Println(errMsg)
}
}
if len(lintErrors) != 0 || len(vetErrors) != 0 {
os.Exit(1)
}
}