@@ -29,8 +29,10 @@ import (
29
29
"path/filepath"
30
30
"runtime"
31
31
"strings"
32
+ "time"
32
33
33
34
"github.com/arduino/arduino-create-agent/gen/tools"
35
+ "github.com/arduino/arduino-create-agent/index"
34
36
"github.com/arduino/arduino-create-agent/utilities"
35
37
"github.com/codeclysm/extract/v3"
36
38
)
@@ -55,25 +57,29 @@ type Tools struct {
55
57
56
58
// Available crawles the downloaded package index files and returns a list of tools that can be installed.
57
59
func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err error) {
58
- list, err := c.Indexes.List(ctx)
60
+ if !c.Index.IndexFile.Exist() || time.Since(c.Index.LastRefresh) > 1*time.Hour {
61
+ // Download the file again and save it
62
+ err := c.Index.DownloadAndVerify()
63
+ if err != nil {
64
+ return nil, err
65
+ }
66
+ }
67
+
68
+ body, err := os.ReadFile(c.Index.IndexFile.String())
59
69
if err != nil {
60
70
return nil, err
61
71
}
62
72
63
- for _, url := range list {
64
- index, err := c.Indexes.Get(ctx, url)
65
- if err != nil {
66
- return nil, err
67
- }
73
+ var index Index
74
+ json.Unmarshal(body, &index)
68
75
69
- for _, packager := range index.Packages {
70
- for _, tool := range packager.Tools {
71
- res = append(res, &tools.Tool{
72
- Packager: packager.Name,
73
- Name: tool.Name,
74
- Version: tool.Version,
75
- })
76
- }
76
+ for _, packager := range index.Packages {
77
+ for _, tool := range packager.Tools {
78
+ res = append(res, &tools.Tool{
79
+ Packager: packager.Name,
80
+ Name: tool.Name,
81
+ Version: tool.Version,
82
+ })
77
83
}
78
84
}
79
85
@@ -142,31 +148,35 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
142
148
return c.install(ctx, path, *payload.URL, *payload.Checksum)
143
149
}
144
150
145
- // otherwise we install from the loaded indexes
146
- list, err := c.Indexes.List(ctx)
151
+ // otherwise we install from the default index
152
+ if !c.Index.IndexFile.Exist() || time.Since(c.Index.LastRefresh) > 1*time.Hour {
153
+ // Download the file again and save it
154
+ err := c.Index.DownloadAndVerify()
155
+ if err != nil {
156
+ return nil, err
157
+ }
158
+ }
159
+
160
+ body, err := os.ReadFile(c.Index.IndexFile.String())
147
161
if err != nil {
148
162
return nil, err
149
163
}
150
164
151
- for _, url := range list {
152
- index, err := c.Indexes.Get(ctx, url)
153
- if err != nil {
154
- return nil, err
155
- }
165
+ var index Index
166
+ json.Unmarshal(body, &index)
156
167
157
- for _, packager := range index.Packages {
158
- if packager.Name != payload.Packager {
159
- continue
160
- }
168
+ for _, packager := range index.Packages {
169
+ if packager.Name != payload.Packager {
170
+ continue
171
+ }
161
172
162
- for _, tool := range packager.Tools {
163
- if tool.Name == payload.Name &&
164
- tool.Version == payload.Version {
173
+ for _, tool := range packager.Tools {
174
+ if tool.Name == payload.Name &&
175
+ tool.Version == payload.Version {
165
176
166
- sys := tool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)
177
+ sys := tool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)
167
178
168
- return c.install(ctx, path, sys.URL, sys.Checksum)
169
- }
179
+ return c.install(ctx, path, sys.URL, sys.Checksum)
170
180
}
171
181
}
172
182
}
0 commit comments