/
run.go
60 lines (48 loc) · 1.53 KB
/
run.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
package gitreceive
import (
"bufio"
"fmt"
"os"
"strings"
storagedriver "github.com/docker/distribution/registry/storage/driver"
builderconf "github.com/teamhephy/builder/pkg/conf"
"github.com/teamhephy/builder/pkg/sys"
"github.com/teamhephy/pkg/log"
client "k8s.io/kubernetes/pkg/client/unversioned"
)
func readLine(line string) (string, string, string, error) {
spl := strings.Split(line, " ")
if len(spl) != 3 {
return "", "", "", fmt.Errorf("malformed line [%s]", line)
}
return spl[0], spl[1], spl[2], nil
}
// Run runs the git-receive hook. This func is effectively the main for the git-receive hook,
// although it is called from the main in boot.go.
func Run(conf *Config, fs sys.FS, env sys.Env, storageDriver storagedriver.StorageDriver) error {
log.Debug("Running git hook")
builderKey, err := builderconf.GetBuilderKey()
if err != nil {
return err
}
kubeClient, err := client.NewInCluster()
if err != nil {
return fmt.Errorf("couldn't reach the api server (%s)", err)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
oldRev, newRev, refName, err := readLine(line)
if err != nil {
return fmt.Errorf("reading STDIN (%s)", err)
}
log.Debug("read [%s,%s,%s]", oldRev, newRev, refName)
// if we're processing a receive-pack on an existing repo, run a build
if strings.HasPrefix(conf.SSHOriginalCommand, "git-receive-pack") {
if err := build(conf, storageDriver, kubeClient, fs, env, builderKey, newRev); err != nil {
return err
}
}
}
return scanner.Err()
}