-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
97 lines (81 loc) · 2.48 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/go-homedir"
)
const (
DEFAULT_GOLANG = "1.10.3"
DEFAULT_GOOS = runtime.GOOS
DEFAULT_GOARCH = runtime.GOARCH
DEFAULT_DOWNLOAD_BASE = "https://storage.googleapis.com/golang/"
EXTRACTED_CANARY = "go-extracted"
SHA_EXTENSION = ".sha256"
RUNGO_VERSION = "0.0.8"
)
func main() {
verbose := os.Getenv("RUNGO_VERBOSE")
if verbose != "" {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.SetFormatter(&log.TextFormatter{DisableColors: true})
log.Debugf("Starting rungo version %s", RUNGO_VERSION)
// Find the version requested
version := findVersion()
// Find the user's home directory
homeDir, err := homedir.Dir()
if err != nil {
log.Fatalf("Failed to determine home directory: %v", err)
}
// baseDir of all file operations for this go version
baseDir := filepath.Join(homeDir, DEFAULT_HOME_INSTALL_LOCATION, version)
// Form URL to download golangArchive
downloadBase := os.Getenv("RUNGO_DOWNLOAD_BASE")
if downloadBase == "" {
downloadBase = DEFAULT_DOWNLOAD_BASE
}
fileUrl := downloadBase + fmt.Sprintf(DEFAULT_ARCHIVE_NAME, version, DEFAULT_GOOS, DEFAULT_GOARCH)
// Location on the filesystem to store the golang archive
golangArchive := filepath.Join(baseDir, path.Base(fileUrl))
sha256sum, err := fetchSha256(fileUrl+SHA_EXTENSION, golangArchive+SHA_EXTENSION)
if err != nil {
log.Fatalf("Failed to fetch sha256: %v", err)
}
err = downloadFile(fileUrl, sha256sum, golangArchive)
if err != nil {
log.Fatalf("Failed to download: %v", err)
}
// Extract golang archive
canaryFile := filepath.Join(baseDir, EXTRACTED_CANARY) // File that signals extraction has already occurred
if fileExists(canaryFile) {
log.Debugf("Skipping extraction due to presence of canary at %q", canaryFile)
} else {
// Remove extracted canary, if exists
_ = os.Remove(filepath.Join(baseDir, EXTRACTED_CANARY))
err = extractFile(golangArchive, baseDir)
if err != nil {
log.Fatalf("Failed to extract: %v", err)
}
ioutil.WriteFile(canaryFile, []byte(""), 0755)
log.Debugf("Successfully extracted %q", golangArchive)
}
// Run go command
setGoRoot(baseDir)
binary := filepath.Base(os.Args[0])
if binary == "rungo" {
binary = "go"
} else if binary == "rungo.exe" {
binary = "go.exe"
}
err = runGo(binary, baseDir, os.Args[1:])
if err != nil {
log.Fatalf("command failed: %v", err)
}
}