- Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpluginhook.go
More file actions
Latest commit
93 lines (86 loc) · 1.98 KB
/
Copy pathpluginhook.go
File metadata and controls
93 lines (86 loc) · 1.98 KB
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
package main
import (
"os"
"fmt"
"os/exec"
"syscall"
"path/filepath"
"log"
"flag"
"bytes"
"strings"
"golang.org/x/crypto/ssh/terminal"
)
funcmain() {
varparallel=flag.Bool("p", false, "Run hooks in parallel")
vartrace=flag.Bool("x", false, "Trace mode")
flag.Parse()
iflen(os.Getenv("PLUGINHOOK_TRACE")) >0 {
*trace=true
}
pluginPath:=os.Getenv("PLUGIN_PATH")
ifpluginPath=="" {
log.Fatal("[ERROR] Unable to locate plugins: set $PLUGIN_PATH\n")
os.Exit(1)
}
ifflag.NArg() <1 {
log.Fatal("[ERROR] Hook name argument is required\n")
os.Exit(1)
}
cmds:=make([]exec.Cmd, 0)
varmatches, _=filepath.Glob(fmt.Sprintf("%s/*/%s", pluginPath, flag.Arg(0)))
for_, hook:=rangematches {
cmd:=exec.Command(hook, flag.Args()[1:]...)
cmds=append(cmds, *cmd)
}
fori:=len(cmds)-1; i>=0; i-- {
cmds[i].Stderr=os.Stderr
ifi==len(cmds)-1 {
cmds[i].Stdout=os.Stdout
}
ifi>0 {
if*parallel {
stdout, err:=cmds[i-1].StdoutPipe()
iferr!=nil {
log.Fatal(err)
}
cmds[i].Stdin=stdout
} else {
varbuf bytes.Buffer
cmds[i-1].Stdout=&buf
cmds[i].Stdin=&buf
}
}
ifi==0&&!terminal.IsTerminal(syscall.Stdin) {
cmds[i].Stdin=os.Stdin
}
}
if*parallel {
done:=make(chanbool, len(cmds))
fori:=0; i<len(cmds); i++ {
gofunc(cmd exec.Cmd, iint) {
if*trace {
fmt.Fprintln(os.Stderr, "+", strings.Join(cmds[i].Args, " "))
}
err:=cmd.Run()
ifmsg, ok:=err.(*exec.ExitError); ok { // there is error code
os.Exit(msg.Sys().(syscall.WaitStatus).ExitStatus())
}
done<-true
}(cmds[i], i)
}
fori:=0; i<len(cmds); i++ {
<-done
}
} else {
fori:=0; i<len(cmds); i++ {
if*trace {
fmt.Fprintln(os.Stderr, "+", strings.Join(cmds[i].Args, " "))
}
err:=cmds[i].Run()
ifmsg, ok:=err.(*exec.ExitError); ok { // there is error code
os.Exit(msg.Sys().(syscall.WaitStatus).ExitStatus())
}
}
}
}