-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenassets.go
92 lines (84 loc) · 2.18 KB
/
genassets.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
// +build ignore
// This is run during go generate and embeds ssh-agent-pipe into the ssh-agent-inject binary.
// * Compiles the ssh-agent-pipe binary for all target platforms (GOOS=linux, different GOARCHs).
// * Creates a .tar.gz with that binary having executable permissions set.
// * Stores that .tar.gz files in the assets/ folder as Go source files targeting the respective GOARCH.
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"text/template"
)
var assetsTemplate = template.Must(template.New("").Funcs(template.FuncMap{
"quote": strconv.Quote,
}).Parse(`package assets
// The .tar.gz archive containing the ssh-agent-pipe binary
const AgentArchive = {{.agent|quote}}
`))
func main() {
os.RemoveAll("assets")
os.MkdirAll("assets", 0700)
archs := []string{
"amd64",
"arm",
"arm64",
}
for _, arch := range archs {
archive, err := buildAgentArchive(arch)
if err != nil {
log.Fatalln("Failed compiling ssh-agent-pipe", err)
}
genPlatformAssets(arch, archive)
}
}
func buildAgentArchive(arch string) ([]byte, error) {
os.Chdir("ssh-agent-pipe")
defer os.Chdir("..")
os.MkdirAll("dist", 0700)
defer os.RemoveAll("dist")
binaryPath := "dist/ssh-agent-pipe"
cmd := exec.Command("go", "build", "-ldflags", "-s -w", "-o", binaryPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=linux", "GOARCH="+arch)
if arch == "arm" {
cmd.Env = append(cmd.Env, "GOARM=7")
}
if err := cmd.Run(); err != nil {
return nil, err
}
contents, err := ioutil.ReadFile(binaryPath)
if err != nil {
return nil, err
}
buffer := &bytes.Buffer{}
gw := gzip.NewWriter(buffer)
tw := tar.NewWriter(gw)
tw.WriteHeader(&tar.Header{
Name: "ssh-agent-pipe",
Size: int64(len(contents)),
Mode: 0755,
})
tw.Write(contents)
tw.Close()
gw.Close()
return buffer.Bytes(), nil
}
func genPlatformAssets(arch string, archive []byte) {
file, err := os.Create(fmt.Sprintf("assets/assets_%s.go", arch))
defer file.Close()
if err != nil {
log.Fatalln("Failed writing assets", err)
}
config := map[string]string{
"agent": string(archive),
}
assetsTemplate.Execute(file, config)
}