-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
129 lines (102 loc) · 2.62 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/docopt/docopt-go"
"github.com/kovetskiy/bithooks"
"github.com/reconquest/hierr-go"
)
var (
version = "1.0"
usage = `bithooker ` + version + `
bithooker is summoned for using multiple hooks in Atlassian Bitbucket
pre-receive git hook.
bithooker just reads pre-receive hook contents, runs specified program with
specified args.
You should pass configuration to bithooker as stdin using following syntax:
<hook-name>@<unique-hook-id>
<args>
<args>
<another-hook-name>@<another-unique-hook-id>
<args>
<args>
* <hook-name> - name of executing hook which will be summoned for accomplishment
the task. bithooker will call <hook-name> with <args> (one per line),
pass self stdin to hook, and pass hook stdout/stderr to bitbucket.
* <unique-hook-id> - it's just unique string for your debugging purposes.
* <args> - hook args, should be indented with one space.
If there is syntax error or any hook exited with non-zero exit code or any
another error occurred, then bithooker will print notice to stderr and exit.
Usage:
bithooker -h | --help
bithooker --version
bithooker <hook>...
Options:
-h --help Show this screen.
--version Show version.
`
)
type output struct {
newline bool
}
func main() {
_, err := docopt.Parse(usage, nil, true, version, true, true)
if err != nil {
panic(err)
}
var (
hooksDirectory = filepath.Dir(os.Args[0])
hooksData = strings.Join(os.Args[1:], "\n")
)
stdin, err := ioutil.ReadFile("/dev/stdin")
if err != nil {
fatal(err, "can't read stdin")
}
hooks, err := bithooks.Decode(hooksData)
if err != nil {
fatal(err, "can't decode hooks")
}
for _, hook := range hooks {
stderr := bytes.NewBuffer(nil)
output := new(output)
hookExecutable := filepath.Join(hooksDirectory, hook.Name)
program := exec.Command(hookExecutable, hook.Args...)
program.Stdin = bytes.NewBuffer(stdin)
program.Stdout = output
program.Stderr = stderr
program.Env = append(
os.Environ(),
"HOOK_NAME="+hook.Name,
"HOOK_ID="+hook.ID,
)
err := program.Run()
if err != nil {
if !output.newline {
fmt.Println()
}
fatal(
hierr.Errorf(
stderr.String(),
err.Error(),
),
"[hook] %s %s", hook.Name, hook.ID,
)
}
}
fmt.Println()
}
func fatal(err error, msg string, args ...interface{}) {
fmt.Fprintln(os.Stderr, hierr.Errorf(err, msg, args...).Error())
os.Exit(1)
}
func (output *output) Write(data []byte) (int, error) {
if !output.newline {
fmt.Println()
}
return fmt.Fprint(os.Stderr, string(data))
}