/
setup.go
194 lines (164 loc) · 4.59 KB
/
setup.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (C) 2021 Toitware ApS. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.
package commands
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/spf13/cobra"
"github.com/toitlang/jaguar/cmd/jag/directory"
)
func getToitSDKURL(version string) (string, error) {
currOS := runtime.GOOS
if currOS == "darwin" {
currOS = "macos"
} else {
currARCH := runtime.GOARCH
if currARCH == "arm" {
currOS = "rpi"
} else if currARCH == "arm64" {
currOS = "aarch64"
}
}
return fmt.Sprintf("https://github.com/toitlang/toit/releases/download/%s/toit-%s.tar.gz", version, currOS), nil
}
func getAssetsURL(version string) string {
return fmt.Sprintf("https://github.com/toitlang/jaguar/releases/download/%s/assets.tar.gz", version)
}
func SetupCmd(info Info) *cobra.Command {
cmd := &cobra.Command{
Use: "setup",
Short: "Setup the Toit SDK",
Long: "Setup the Toit SDK by downloading the necessary bits from https://github.com/toitlang/toit.\n" +
"The downloaded SDK is stored locally in a subdirectory of your home folder.",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
check, err := cmd.Flags().GetBool("check")
if err != nil {
return err
}
if check {
if _, err := GetSDK(ctx); err != nil {
return err
}
if _, err := directory.GetJaguarSnapshotPath(); err != nil {
return err
}
fmt.Println("Jaguar setup is valid.")
return nil
}
sdkPath, err := directory.GetSDKCachePath()
if err != nil {
return err
}
downloaderPath := filepath.Join(sdkPath, "JAGUAR")
os.Remove(downloaderPath)
if err := downloadSdk(ctx, info.SDKVersion); err != nil {
return err
}
if err := downloadAssets(ctx, info.Version); err != nil {
return err
}
downloaderBytes, err := json.Marshal(&info)
if err != nil {
return err
}
if err := os.WriteFile(downloaderPath, downloaderBytes, 0666); err != nil {
return err
}
fmt.Printf("Successfully setup Jaguar %s with Toit SDK %s.\n", info.Version, info.SDKVersion)
return nil
},
}
cmd.AddCommand(SetupSdkCmd(info))
cmd.Flags().BoolP("check", "c", false, "if set, will check the local setup")
return cmd
}
func SetupSdkCmd(info Info) *cobra.Command {
cmd := &cobra.Command{
Use: "sdk",
Short: "Setup just the SDK",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if len(args) != 1 {
return fmt.Errorf("takes exactly one argument")
}
if err := downloadSdkTo(ctx, info.SDKVersion, args[0]); err != nil {
return err
}
return nil
},
}
return cmd
}
func downloadAssets(ctx context.Context, version string) error {
assetsPath, err := directory.GetAssetsCachePath()
if err != nil {
return err
}
assetsURL := getAssetsURL(version)
fmt.Printf("Downloading Jaguar assets from %s ...\n", assetsURL)
bundle, err := download(ctx, assetsURL)
if err != nil {
return err
}
gzipReader, err := newGZipReader(bundle)
if err != nil {
bundle.Close()
return fmt.Errorf("failed to read Jaguar assets as gzip file: %w", err)
}
defer gzipReader.Close()
if err := os.RemoveAll(assetsPath); err != nil {
return err
}
if err := os.MkdirAll(assetsPath, 0755); err != nil {
return err
}
if err := extractTarFile(gzipReader, assetsPath, "assets/"); err != nil {
return fmt.Errorf("failed to extract Jaguar assets, reason: %w", err)
}
fmt.Println("Successfully installed Jaguar assets into", assetsPath)
return nil
}
func downloadSdk(ctx context.Context, version string) error {
sdkPath, err := directory.GetSDKCachePath()
if err != nil {
return err
}
return downloadSdkTo(ctx, version, sdkPath)
}
func downloadSdkTo(ctx context.Context, version string, sdkPath string) error {
sdkURL, err := getToitSDKURL(version)
if err != nil {
return err
}
fmt.Printf("Downloading Toit SDK from %s ...\n", sdkURL)
sdk, err := download(ctx, sdkURL)
if err != nil {
return err
}
gzipReader, err := newGZipReader(sdk)
if err != nil {
sdk.Close()
return fmt.Errorf("failed to read the Toit SDK artifact as gzip file: %w", err)
}
defer gzipReader.Close()
if err := os.RemoveAll(sdkPath); err != nil {
return err
}
if err := os.MkdirAll(sdkPath, 0755); err != nil {
return err
}
if err := extractTarFile(gzipReader, sdkPath, "toit/"); err != nil {
return fmt.Errorf("failed to extract the Toit SDK, reason: %w", err)
}
gzipReader.Close()
fmt.Println("Successfully installed Toit SDK into", sdkPath)
return nil
}