-
Notifications
You must be signed in to change notification settings - Fork 3
/
quickhook.go
114 lines (97 loc) · 2.31 KB
/
quickhook.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"os"
"strings"
"github.com/alecthomas/kong"
"github.com/fatih/color"
"github.com/dirk/quickhook/hooks"
"github.com/dirk/quickhook/repo"
"github.com/dirk/quickhook/tracing"
)
const VERSION = "main"
var cli struct {
Install struct {
Yes bool `short:"y" help:"Assume yes for all prompts"`
Bin string `help:"Path to Quickhook executable to use in the shim (if it's not on $PATH)"`
} `cmd:"" help:"Install Quickhook shims into .git/hooks"`
Hook struct {
PreCommit struct {
Files []string `help:"For testing, supply list of files as changed files"`
} `cmd:"" help:"Run pre-commit hooks"`
CommitMsg struct {
MessageFile string `arg:"" help:"Temp file containing the commit message"`
} `cmd:"" help:"Run commit-msg hooks"`
} `cmd:""`
NoColor bool `env:"NO_COLOR" help:"Don't colorize output"`
Trace bool `env:"QUICKHOOK_TRACE" help:"Enable tracing, writes to trace.out"`
Version kong.VersionFlag `help:"Show version information"`
}
func main() {
parser, err := kong.New(&cli,
kong.Vars{
"version": VERSION,
})
if err != nil {
panic(err)
}
args := os.Args[1:]
// Print the help if there are no args.
if len(args) == 0 {
parsed := kong.Context{
Kong: parser,
}
parsed.PrintUsage(false)
parsed.Exit(1)
}
parsed, err := parser.Parse(args)
parser.FatalIfErrorf(err)
if cli.Trace {
finish := tracing.Start()
defer finish()
}
if cli.NoColor {
color.NoColor = true
}
switch parsed.Command() {
case "install":
repo, err := repo.NewRepo()
if err != nil {
panic(err)
}
// TODO: Dry run option.
prompt := !cli.Install.Yes
quickhook := strings.TrimSpace(cli.Install.Bin)
if quickhook == "" {
quickhook = "quickhook"
}
err = install(repo, quickhook, prompt)
if err != nil {
panic(err)
}
case "hook commit-msg <message-file>":
repo, err := repo.NewRepo()
if err != nil {
panic(err)
}
hook := hooks.CommitMsg{
Repo: repo,
}
err = hook.Run(cli.Hook.CommitMsg.MessageFile)
if err != nil {
panic(err)
}
case "hook pre-commit":
repo, err := repo.NewRepo()
if err != nil {
panic(err)
}
hook := hooks.PreCommit{Repo: repo}
err = hook.Run(cli.Hook.PreCommit.Files)
if err != nil {
panic(err)
}
default:
panic(fmt.Sprintf("Unrecognized command: %v", parsed.Command()))
}
}