-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinygo.go
More file actions
123 lines (107 loc) · 2.41 KB
/
tinygo.go
File metadata and controls
123 lines (107 loc) · 2.41 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package tinygo
import (
"net/http"
"os/exec"
"path/filepath"
"runtime"
)
const DefaultVersion = "0.40.1"
// defaultInstallDir returns the platform-specific install directory,
// matching the official TinyGo installation instructions at
// https://tinygo.org/getting-started/install/
//
// - Linux/macOS: /usr/local (tar -xf tinygo...tar.gz -C /usr/local)
// - Windows: "" (Scoop manages its own directories; no fixed path)
func defaultInstallDir(goos string) string {
if goos == "windows" {
return ""
}
return "/usr/local"
}
type config struct {
version string
installDir string
logger func(string)
lookPath func(string) (string, error)
httpClient *http.Client
goos string
goarch string
// for testing
downloadURLFunc func() string
}
type Option func(*config)
func WithVersion(v string) Option {
return func(c *config) {
c.version = v
}
}
func WithInstallDir(dir string) Option {
return func(c *config) {
c.installDir = dir
}
}
func WithLogger(f func(string)) Option {
return func(c *config) {
c.logger = f
}
}
func newConfig(opts ...Option) *config {
c := &config{
version: DefaultVersion,
installDir: defaultInstallDir(runtime.GOOS),
logger: func(string) {},
lookPath: exec.LookPath,
httpClient: http.DefaultClient,
goos: runtime.GOOS,
goarch: runtime.GOARCH,
}
for _, opt := range opts {
opt(c)
}
return c
}
// internal options for testing (not exported)
func withLookPath(f func(string) (string, error)) Option {
return func(c *config) {
c.lookPath = f
}
}
func withHTTPClient(cl *http.Client) Option {
return func(c *config) {
c.httpClient = cl
}
}
func withGOOS(goos string) Option {
return func(c *config) {
c.goos = goos
}
}
func withGOARCH(goarch string) Option {
return func(c *config) {
c.goarch = goarch
}
}
func withDownloadURLFunc(f func() string) Option {
return func(c *config) {
c.downloadURLFunc = f
}
}
func (c *config) binPath() string {
bin := filepath.Join(c.installDir, "tinygo", "bin", "tinygo")
if c.goos == "windows" {
bin += ".exe"
}
return bin
}
func (c *config) downloadURL() string {
if c.downloadURLFunc != nil {
return c.downloadURLFunc()
}
ext := "tar.gz"
if c.goos == "windows" {
ext = "zip"
}
// eg: tinygo0.40.1.linux-amd64.tar.gz
return "https://github.com/tinygo-org/tinygo/releases/download/v" +
c.version + "/tinygo" + c.version + "." + c.goos + "-" + c.goarch + "." + ext
}