Skip to content

Commit 814377f

Browse files
factor out index download logic
Co-authored-by: Alessio Perugini <[email protected]>
1 parent 53afd39 commit 814377f

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

index/index.go

+12
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,15 @@ func checkGPGSig(signed, signature io.Reader) error {
135135
_, err = openpgp.CheckDetachedSignature(keyring, signed, signature)
136136
return err
137137
}
138+
139+
// Read will read the index file. In case it doesn't exists or the latest downloaded
140+
// version is older than 1 hour, it will be downloaded again.
141+
func (ir *Resource) Read() ([]byte, error) {
142+
if !ir.IndexFile.Exist() || time.Since(ir.LastRefresh) > 1*time.Hour {
143+
// Download the file again and save it
144+
if err := ir.DownloadAndVerify(); err != nil {
145+
return nil, err
146+
}
147+
}
148+
return ir.IndexFile.ReadFile()
149+
}

tools/download.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"path/filepath"
3535
"runtime"
3636
"strings"
37-
"time"
3837

3938
"github.com/arduino/arduino-create-agent/utilities"
4039
"github.com/arduino/arduino-create-agent/v2/pkgs"
@@ -79,15 +78,7 @@ func pathExists(path string) bool {
7978
// if it already exists.
8079
func (t *Tools) Download(pack, name, version, behaviour string) error {
8180

82-
if !t.Index.IndexFile.Exist() || time.Since(t.Index.LastRefresh) > 1*time.Hour {
83-
// Download the file again and save it
84-
err := t.Index.DownloadAndVerify()
85-
if err != nil {
86-
return err
87-
}
88-
}
89-
90-
body, err := os.ReadFile(t.Index.IndexFile.String())
81+
body, err := t.Index.Read()
9182
if err != nil {
9283
return err
9384
}

v2/pkgs/tools.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"path/filepath"
3030
"runtime"
3131
"strings"
32-
"time"
3332

3433
"github.com/arduino/arduino-create-agent/gen/tools"
3534
"github.com/arduino/arduino-create-agent/index"
@@ -57,15 +56,7 @@ type Tools struct {
5756

5857
// Available crawles the downloaded package index files and returns a list of tools that can be installed.
5958
func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err error) {
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+
body, err := c.Index.Read()
6960
if err != nil {
7061
return nil, err
7162
}
@@ -149,15 +140,7 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
149140
}
150141

151142
// 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())
143+
body, err := c.Index.Read()
161144
if err != nil {
162145
return nil, err
163146
}

0 commit comments

Comments
 (0)