-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.go
More file actions
71 lines (60 loc) · 1.6 KB
/
detect.go
File metadata and controls
71 lines (60 loc) · 1.6 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
package tinygo
import (
"fmt"
"os"
"os/exec"
"strings"
)
func IsInstalled(opts ...Option) bool {
_, err := GetPath(opts...)
return err == nil
}
func GetPath(opts ...Option) (string, error) {
return getPath(newConfig(opts...))
}
func getPath(c *config) (string, error) {
// 1. PATH
if p, err := c.lookPath("tinygo"); err == nil {
return p, nil
}
// 2. Scoop shims (Windows: scoop install tinygo places tinygo.exe here,
// but shims dir may not be in PATH within the current process)
if home, err := os.UserHomeDir(); err == nil {
scoopBin := home + `\scoop\shims\tinygo.exe`
if _, err := os.Stat(scoopBin); err == nil {
return scoopBin, nil
}
}
// 3. local tarball install
bin := c.binPath()
if _, err := os.Stat(bin); err == nil {
return bin, nil
}
return "", fmt.Errorf("tinygo not found in PATH or in local installation")
}
func GetVersion(opts ...Option) (string, error) {
p, err := GetPath(opts...)
if err != nil {
return "", err
}
cmd := exec.Command(p, "version")
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to run tinygo version: %w", err)
}
return strings.TrimSpace(string(out)), nil
}
// installedVersion returns just the semver number from `tinygo version` output.
// e.g. "tinygo version 0.39.0 linux/amd64 ..." → "0.39.0"
func installedVersion(opts ...Option) (string, error) {
full, err := GetVersion(opts...)
if err != nil {
return "", err
}
// output format: "tinygo version X.Y.Z ..."
fields := strings.Fields(full)
if len(fields) < 3 {
return "", fmt.Errorf("unexpected tinygo version output: %q", full)
}
return fields[2], nil
}