-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
58 lines (49 loc) · 1.24 KB
/
hook.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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"regexp"
)
const dependsOnUs = `"nativeBuildInputs","[^"]*nix-gocacheprog-hook`
func hookMain() {
if flag.NArg() < 1 {
return // not called as a hook properly?
} else if drv, err := os.ReadFile(flag.Arg(0)); err != nil {
log.Println("can't open", flag.Arg(0))
return // just ignore errors
} else if ok, err := regexp.Match(dependsOnUs, drv); err != nil || !ok {
return // does not depend on this hook
}
socketPath := filepath.Join(SocketDir, SocketFile)
c, err := net.Dial("unix", socketPath)
if err != nil {
log.Fatalln(err)
}
br := bufio.NewReader(c)
jd := json.NewDecoder(br)
bw := bufio.NewWriter(c)
je := json.NewEncoder(bw)
id := genBuildID()
je.Encode(&Hello{BuildID: id, Phase: PhaseHook})
bw.Flush()
var res HookResponse
if err := jd.Decode(&res); err != nil {
log.Fatalln(err)
}
selfBin, err := os.Readlink("/proc/self/exe")
if err != nil {
log.Fatalln(err)
}
bw = bufio.NewWriter(os.Stdout)
fmt.Fprintf(bw, "extra-sandbox-paths\n")
fmt.Fprintf(bw, "%s\n", SocketDir)
fmt.Fprintf(bw, "%s/%s=%s\n", SandboxCacheDir, id, res.BuildDir)
fmt.Fprintf(bw, "%s/client=%s\n", SandboxCacheDir, selfBin)
bw.Flush()
}