-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.go
129 lines (117 loc) · 2.85 KB
/
install.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"sort"
"strings"
"github.com/gocolly/colly"
"github.com/manifoldco/promptui"
"github.com/urfave/cli/v2"
)
func install(ctx *cli.Context) error {
versions, err := fetchVersions()
if err != nil {
return err
}
home, err := os.UserHomeDir()
if err != nil {
return err
}
sdkDir := getSDKDirPath(home)
installedVers, err := listInstalledVersions(sdkDir)
if err != nil {
return err
}
// すでにインストール済みのバージョンは除外する
versions = removeDuplicates(versions, installedVers)
// バージョンを選択させる
promptSelect := promptui.Select{
Label: "Choose a version",
Items: versions,
Templates: &promptui.SelectTemplates{
Help: "Install NEW Golang Version",
},
}
_, result, err := promptSelect.Run()
if err != nil {
if err == promptui.ErrAbort || err == promptui.ErrInterrupt {
fmt.Println("Aborted")
return nil
}
return err
}
// install確認
promptConfirm := promptui.Prompt{
Label: fmt.Sprintf("Install %s", result),
IsConfirm: true,
}
_, err = promptConfirm.Run()
if err != nil {
if err == promptui.ErrAbort || err == promptui.ErrInterrupt {
fmt.Println("Aborted")
return nil
}
return err
}
// install実行
installver := fmt.Sprintf("golang.org/dl/go%s@latest", result)
cmd := exec.Command("go", "install", installver)
fmt.Println(fmt.Sprintf("go install %s", installver))
output, err := cmd.CombinedOutput()
if err != nil {
return err
}
if len(output) > 0 {
fmt.Print(string(output))
}
gocmd := fmt.Sprintf("go%s", result)
cmd = exec.Command(gocmd, "download")
fmt.Println(fmt.Sprintf("%s download", gocmd))
output, err = cmd.CombinedOutput()
if err != nil {
return err
}
return nil
}
// https://go.dev/dl/をスクレイピングして現在のOS/ARCに一致するバージョン一覧を取得する
func fetchVersions() ([]string, error) {
currentOS := runtime.GOOS
currentArch := runtime.GOARCH
ext := "tar.gz"
if currentOS == "windows" {
ext = "zip"
}
suffix := fmt.Sprintf(".%s-%s.%s", currentOS, currentArch, ext)
c := colly.NewCollector()
var versions []string
c.OnHTML("table tr", func(e *colly.HTMLElement) {
filename := e.ChildText("td:first-child")
if !strings.HasSuffix(filename, suffix) {
return
}
filename = strings.TrimPrefix(strings.TrimSuffix(filename, suffix), "go")
versions = append(versions, filename)
})
err := c.Visit("https://go.dev/dl/")
if err != nil {
return nil, err
}
sort.Sort(sort.Reverse(sort.StringSlice(versions)))
return versions, nil
}
// s2に含まれる要素をs1から除外する
func removeDuplicates(s1, s2 []string) []string {
m := make(map[string]bool)
for _, v := range s2 {
m[v] = true
}
result := []string{}
for _, v := range s1 {
if _, exists := m[v]; !exists {
result = append(result, v)
}
}
return result
}