-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (87 loc) · 1.77 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"flag"
"log"
"net/http"
"os"
"regexp"
"time"
"golang.org/x/mod/modfile"
)
const envGoRoot = "GOROOT"
const modPath = "./go.mod"
type config struct {
dest string
version string
useModFile bool
}
func main() {
cfg := config{}
flag.StringVar(&cfg.dest, "dest", "/usr/local/golang/", "Path to the Go versions storage in SemaphoreCI.")
flag.BoolVar(&cfg.useModFile, "mod", false, "")
debug := flag.Bool("debug", false, "Debug mode.")
help := flag.Bool("h", false, "Show this help.")
flag.Usage = usage
flag.Parse()
if *help {
usage()
}
nArgs := flag.NArg()
if nArgs != 1 && !cfg.useModFile || cfg.useModFile && nArgs > 1 {
log.Println("Error: missing go version.")
usage()
}
cfg.version = flag.Arg(0)
if cfg.version != "" {
ok, _ := regexp.MatchString(`go(\d\.\d+)(?:.\d+)?`, cfg.version)
if !ok {
log.Fatalf("invalid version (expected gox.x+[.x+]): %s", cfg.version)
}
}
if cfg.useModFile {
mod, err := readGoMod()
if err != nil {
log.Fatal(err)
}
if mod != nil {
cfg.version = "go" + mod.Go.Version
}
}
if cfg.version == "" {
log.Fatal("The version is missing.")
}
if *debug {
log.Printf("Debug mode: %#v", cfg)
}
smg := sem{
client: &http.Client{Timeout: 30 * time.Second},
debug: *debug,
goRoot: envGoRoot,
}
err := smg.getGo(cfg.dest, cfg.version)
if err != nil {
log.Fatal(err)
}
}
func readGoMod() (*modfile.File, error) {
_, err := os.Stat(modPath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
file, err := os.ReadFile(modPath)
if err != nil {
return nil, err
}
return modfile.Parse(modPath, file, nil)
}
func usage() {
_, _ = os.Stderr.WriteString(`SemGo
semgo [flags] <version>
Flags:
`)
flag.PrintDefaults()
os.Exit(2)
}