-
Notifications
You must be signed in to change notification settings - Fork 8
/
gosdk.go
68 lines (51 loc) · 1.64 KB
/
gosdk.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package golang
import (
"embed"
"fmt"
"path/filepath"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/mod/semver"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/sdk/host"
)
var (
//go:embed gosdk.json
lib embed.FS
v = &host.ParsedVersions{Name: "go", FS: lib, Filename: "gosdk.json"}
)
type LocalSDK = host.LocalSDK
func vv(version string) string {
v := v.Get()
for ver, minor := range v.Versions {
// If we store `"1.22": "1.22.2"` then "1.22.1" should also map to "1.22".
if semver.Compare("v"+minor, "v"+version) >= 0 {
version = ver
}
}
return version
}
func MatchLatestVersion(version string) string {
version = vv(version)
v := v.Get()
actualVer, has := v.Versions[version]
if !has {
return version
}
return actualVer
}
func MatchSDK(version string, platform specs.Platform) (compute.Computable[LocalSDK], error) {
return SDK(vv(version), platform)
}
func SDK(version string, platform specs.Platform) (compute.Computable[LocalSDK], error) {
return v.SDK(version, platform, func(ver string, platform specs.Platform) (string, string) {
return fmt.Sprintf("https://go.dev/dl/go%s.%s-%s.tar.gz", ver, platform.OS, platform.Architecture), "go/bin/go"
})
}
func GoRoot(sdk LocalSDK) string { return filepath.Join(sdk.Path, "go") }
func GoBin(sdk LocalSDK) string { return sdk.Binary }
func GoRootEnv(sdk LocalSDK) string {
return fmt.Sprintf("GOROOT=%s", GoRoot(sdk))
}