@@ -29,8 +29,10 @@ import (
2929 "path/filepath"
3030 "runtime"
3131 "strings"
32+ "time"
3233
3334 "github.com/arduino/arduino-create-agent/gen/tools"
35+ "github.com/arduino/arduino-create-agent/index"
3436 "github.com/arduino/arduino-create-agent/utilities"
3537 "github.com/codeclysm/extract/v3"
3638)
@@ -55,25 +57,29 @@ type Tools struct {
5557
5658// Available crawles the downloaded package index files and returns a list of tools that can be installed.
5759func (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 ())
5969 if err != nil {
6070 return nil , err
6171 }
6272
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 )
6875
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+ })
7783 }
7884 }
7985
@@ -142,31 +148,35 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
142148 return c .install (ctx , path , * payload .URL , * payload .Checksum )
143149 }
144150
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 ())
147161 if err != nil {
148162 return nil , err
149163 }
150164
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 )
156167
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+ }
161172
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 {
165176
166- sys := tool .GetFlavourCompatibleWith (runtime .GOOS , runtime .GOARCH )
177+ sys := tool .GetFlavourCompatibleWith (runtime .GOOS , runtime .GOARCH )
167178
168- return c .install (ctx , path , sys .URL , sys .Checksum )
169- }
179+ return c .install (ctx , path , sys .URL , sys .Checksum )
170180 }
171181 }
172182 }
0 commit comments