-
Notifications
You must be signed in to change notification settings - Fork 8
/
make.go
107 lines (88 loc) · 2.35 KB
/
make.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
_ "net/http/pprof"
"os"
"os/signal"
"strings"
"dagger.io/dagger"
"github.com/Azure/moby-packaging/pkg/archive"
"github.com/Azure/moby-packaging/targets"
"golang.org/x/sys/unix"
)
func main() {
outDir := flag.String("output", "bundles", "Output directory for built packages (note the distro name will be appended to this path)")
buildSpec := flag.String("build-spec", "", "Location of the build spec json file")
flag.Parse()
spec, err := readBuildSpec(*buildSpec)
if err != nil {
fmt.Fprintln(os.Stderr, "could not read or parse build spec file")
os.Exit(1)
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, unix.SIGTERM)
defer cancel()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
defer client.Close()
go func() {
<-ctx.Done()
client.Close()
}()
out, err := do(ctx, client, spec)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(3)
}
if _, err := out.Export(ctx, spec.Dir(*outDir)); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(4)
}
}
func readBuildSpec(filename string) (*archive.Spec, error) {
if filename == "" {
return nil, fmt.Errorf("no build spec file specified")
}
b, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var spec archive.Spec
if err := json.Unmarshal(b, &spec); err != nil {
return nil, err
}
return &spec, nil
}
func do(ctx context.Context, client *dagger.Client, cfg *archive.Spec) (*dagger.Directory, error) {
if cfg.Arch == "" {
p, err := client.DefaultPlatform(ctx)
if err != nil {
return nil, fmt.Errorf("could not determine default platform: %w", err)
}
_, a, ok := strings.Cut(string(p), "/")
if !ok {
return nil, fmt.Errorf("unexpected platform format: %q", p)
}
cfg.Arch = a
}
targetOs := "linux"
if cfg.Distro == "windows" {
targetOs = "windows"
}
platform := dagger.Platform(fmt.Sprintf("%s/%s", targetOs, cfg.Arch))
getGoVersion, ok := targets.GetGoVersionForPackage[cfg.Pkg]
if !ok {
return nil, fmt.Errorf("unknown package: %q", cfg.Pkg)
}
goVersion := getGoVersion(cfg)
target, err := targets.GetTarget(ctx, cfg.Distro, client, platform, goVersion)
if err != nil {
return nil, err
}
return target.Make(cfg, packageDir(client, cfg.Pkg), hackCrossDir(client))
}