Skip to content

Commit 55dc11b

Browse files
committed
Added initial go
1 parent 1590758 commit 55dc11b

File tree

11 files changed

+804
-2
lines changed

11 files changed

+804
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"id": "go",
3+
"version": "1.0.0",
4+
"name": "Go",
5+
"description": "A package which installs Go.",
6+
"options": {
7+
"version": {
8+
"type": "string",
9+
"proposals": [
10+
"latest",
11+
"1.24",
12+
"1.21.8"
13+
],
14+
"default": "latest",
15+
"description": "The version of Go to install."
16+
},
17+
"downloadRegistryBase": {
18+
"type": "string",
19+
"default": "",
20+
"proposals": [
21+
"https://mycompany.com/artifactory/dl-google-generic-remote"
22+
],
23+
"description": "The download registry to use for Go binaries."
24+
},
25+
"downloadRegistryPath": {
26+
"type": "string",
27+
"default": "",
28+
"description": "The download registry path to use for Go binaries."
29+
}
30+
},
31+
"customizations": {
32+
"vscode": {
33+
"extensions": [
34+
"golang.Go"
35+
]
36+
}
37+
},
38+
"containerEnv": {
39+
"PATH": "/usr/local/go/bin:${PATH}"
40+
}
41+
}

features/src/go/install.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
./installer \
2+
-version="${VERSION:-"latest"}" \
3+
-downloadRegistryBase="${DOWNLOAD_REGISTRY_BASE:-""}" \
4+
-downloadRegistryPath="${DOWNLOAD_REGISTRY_PATH:-""}"

features/src/go/installer.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package main
2+
3+
import (
4+
"builder/installer"
5+
"encoding/json"
6+
"flag"
7+
"fmt"
8+
"net/url"
9+
"os"
10+
"regexp"
11+
"strings"
12+
13+
"github.com/roemer/gover"
14+
)
15+
16+
//////////
17+
// Configuration
18+
//////////
19+
20+
const versionsIndexUrl = "https://go.dev/dl/?mode=json&include=all"
21+
const latestVersionUrl = "https://go.dev/VERSION?m=text"
22+
23+
var versionRegexp *regexp.Regexp = regexp.MustCompile(`(?m:)^go(?P<d1>\d+)(?:\.(?P<d2>\d+))?(?:\.(?P<d3>\d+))?(?:(?P<s4>[a-z]+)(?P<d5>\d+)?)?$`)
24+
25+
//////////
26+
// Main
27+
//////////
28+
29+
func main() {
30+
if err := runMain(); err != nil {
31+
fmt.Printf("Error: %v\n", err)
32+
os.Exit(1)
33+
}
34+
}
35+
36+
func runMain() error {
37+
// Handle the flags
38+
version := flag.String("version", "latest", "The version of Go to install.")
39+
downloadRegistryBase := flag.String("downloadRegistryBase", "", "The download registry to use for Go binaries.")
40+
downloadRegistryPath := flag.String("downloadRegistryPath", "", "The download registry path to use for Go binaries.")
41+
flag.Parse()
42+
43+
fill(downloadRegistryBase, "https://dl.google.com", "")
44+
fill(downloadRegistryPath, "/go", "")
45+
46+
// Create and process the feature
47+
feature := installer.NewFeature("Go", true,
48+
&goComponent{
49+
ComponentBase: installer.NewComponentBase("Go", *version),
50+
DownloadRegistryBase: *downloadRegistryBase,
51+
DownloadRegistryPath: *downloadRegistryPath,
52+
})
53+
return feature.Process()
54+
}
55+
56+
func fill(passedValue *string, defaultValue string, key string) {
57+
if *passedValue == "" {
58+
// TODO: Get value from env?file?
59+
// Otherwise set to default value
60+
*passedValue = defaultValue
61+
}
62+
}
63+
64+
func buildUrl(base string, parts ...string) (string, error) {
65+
return url.JoinPath(base, parts...)
66+
}
67+
68+
//////////
69+
// Implementation
70+
//////////
71+
72+
type goComponent struct {
73+
*installer.ComponentBase
74+
DownloadRegistryBase string
75+
DownloadRegistryPath string
76+
}
77+
78+
func (c *goComponent) GetLatestVersion() (*gover.Version, error) {
79+
versionFileContent, err := installer.Tools.Download.AsString(latestVersionUrl)
80+
if err != nil {
81+
return nil, err
82+
}
83+
// Only use the first line
84+
lines := strings.Split(strings.ReplaceAll(versionFileContent, "\r\n", "\n"), "\n")
85+
if len(lines) == 0 {
86+
return nil, fmt.Errorf("no version found in go latest")
87+
}
88+
version, err := gover.ParseVersionFromRegex(lines[0], versionRegexp)
89+
if err != nil {
90+
return nil, err
91+
}
92+
return version, err
93+
}
94+
95+
func (c *goComponent) GetAllVersions() ([]*gover.Version, error) {
96+
versionFileContent, err := installer.Tools.Download.AsBytes(versionsIndexUrl)
97+
if err != nil {
98+
return nil, err
99+
}
100+
var jsonData []map[string]interface{}
101+
if err := json.Unmarshal(versionFileContent, &jsonData); err != nil {
102+
return nil, err
103+
}
104+
versions := []*gover.Version{}
105+
for _, entry := range jsonData {
106+
versionString := entry["version"].(string)
107+
version, err := gover.ParseVersionFromRegex(versionString, versionRegexp)
108+
if err != nil {
109+
return nil, err
110+
}
111+
versions = append(versions, version)
112+
}
113+
114+
return versions, nil
115+
}
116+
117+
func (c *goComponent) InstallVersion(version *gover.Version) error {
118+
// Download the file
119+
fileName := fmt.Sprintf("%s.linux-amd64.tar.gz", version.Raw)
120+
downloadUrl, err := buildUrl(c.DownloadRegistryBase, c.DownloadRegistryPath, fileName)
121+
if err != nil {
122+
return err
123+
}
124+
if err := installer.Tools.Download.ToFile(downloadUrl, fileName, "Go"); err != nil {
125+
return err
126+
}
127+
// Extract it
128+
if err := installer.Tools.Compression.ExtractTarGz(fileName, "/usr/local", false); err != nil {
129+
return err
130+
}
131+
// Cleanup
132+
if err := os.Remove(fileName); err != nil {
133+
return err
134+
}
135+
return nil
136+
}

go.mod

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ module builder
22

33
go 1.24.5
44

5-
require github.com/roemer/gotaskr v0.6.0
5+
require (
6+
github.com/roemer/gotaskr v0.6.0
7+
github.com/roemer/gover v0.6.1
8+
github.com/schollz/progressbar/v3 v3.18.0
9+
github.com/ulikunitz/xz v0.5.12
10+
)
611

712
require (
813
dario.cat/mergo v1.0.1 // indirect
@@ -35,13 +40,13 @@ require (
3540
github.com/mattn/go-colorable v0.1.13 // indirect
3641
github.com/mattn/go-isatty v0.0.20 // indirect
3742
github.com/minio/sha256-simd v1.0.1 // indirect
43+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
3844
github.com/nwaples/rardecode v1.1.3 // indirect
3945
github.com/pierrec/lz4/v4 v4.1.22 // indirect
4046
github.com/pjbgf/sha1cd v0.3.2 // indirect
4147
github.com/rivo/uniseg v0.4.7 // indirect
4248
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
4349
github.com/skeema/knownhosts v1.3.1 // indirect
44-
github.com/ulikunitz/xz v0.5.12 // indirect
4550
github.com/xanzy/ssh-agent v0.3.3 // indirect
4651
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
4752
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
1515
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
1616
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
1717
github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
18+
github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM=
19+
github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY=
1820
github.com/cloudflare/circl v1.6.0 h1:cr5JKic4HI+LkINy2lg3W2jF8sHCVTBncJr5gIIq7qk=
1921
github.com/cloudflare/circl v1.6.0/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
2022
github.com/cyphar/filepath-securejoin v0.4.1 h1:JyxxyPEaktOD+GAnqIqTf9A8tHyAG22rowi7HkoSU1s=
@@ -86,8 +88,12 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
8688
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8789
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
8890
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
91+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
92+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
8993
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
9094
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
95+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
96+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
9197
github.com/nwaples/rardecode v1.1.3 h1:cWCaZwfM5H7nAD6PyEdcVnczzV8i/JtotnyW/dD9lEc=
9298
github.com/nwaples/rardecode v1.1.3/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
9399
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
@@ -104,8 +110,12 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
104110
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
105111
github.com/roemer/gotaskr v0.6.0 h1:TfQbMfiVGKsfXWOBf33h6gcc3bknhNnEip6YgLhLJ4w=
106112
github.com/roemer/gotaskr v0.6.0/go.mod h1:ELbNvPC6EMI+gySWiAPK0wQgOkXlnGbYSzqHvWvTHh8=
113+
github.com/roemer/gover v0.6.1 h1:7dRCk4rU3SWYNJYFqp5OAF6DOS0RpLuSYxNCFPDGgds=
114+
github.com/roemer/gover v0.6.1/go.mod h1:mpNP8x0jTUfi6PuPHnGGLRf32Gh9HtOLa4b6O4jmT0I=
107115
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
108116
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
117+
github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA=
118+
github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
109119
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
110120
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
111121
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=

0 commit comments

Comments
 (0)