-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
55 lines (48 loc) · 1.23 KB
/
hooks.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
package main
import (
"fmt"
"os"
"os/exec"
)
// Hook is a command which is run before releasing.
type Hook struct {
Name string
Description string
Command []string
}
var AllHooks = []Hook{
{
Name: "go-mod-download",
Description: "run 'go mod download' to make sure all Go modules are accessible",
Command: []string{"go", "mod", "download"},
},
{
Name: "go-generate",
Description: "run 'go generate ./...' to make sure all generated code is up to date",
Command: []string{"go", "generate", "./..."},
},
{
Name: "gofmt",
Description: "run 'gofmt -w .' to format all source code",
Command: []string{"gofmt", "-w", "."},
},
}
// RunHooks run all hooks.
func RunHooks(cfg CheckConfig) error {
// check for uncommitted changes before running hooks
err := CheckUncommittedChanges(cfg)
if err != nil {
return err
}
for _, hook := range AllHooks {
fmt.Printf("run %v\n", hook.Name)
cmd := exec.Command(hook.Command[0], hook.Command[1:]...)
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("hook %v failed: %w", hook.Name, err)
}
}
// afterwards, check if the repository contains uncommitted changes
return CheckUncommittedChanges(cfg)
}