Skip to content

Commit 18e9250

Browse files
Reintroduce caching option when downloading tools
1 parent a882ed7 commit 18e9250

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

tools/download.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ import (
4343
// If version is not "latest" and behaviour is "replace", it will download the
4444
// version again. If instead behaviour is "keep" it will not download the version
4545
// if it already exists.
46-
//
47-
// At the moment the value of behaviour is ignored.
4846
func (t *Tools) Download(pack, name, version, behaviour string) error {
4947

50-
tool := pkgs.New(t.index, t.directory.String())
48+
tool := pkgs.New(t.index, t.directory.String(), behaviour)
5149
_, err := tool.Install(context.Background(), &tools.ToolPayload{Name: name, Version: version, Packager: pack})
5250
if err != nil {
5351
return err

v2/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Server(directory string, index *index.Resource) http.Handler {
4040
logAdapter := LogAdapter{Logger: logger}
4141

4242
// Mount tools
43-
toolsSvc := pkgs.New(index, directory)
43+
toolsSvc := pkgs.New(index, directory, "replace")
4444
toolsEndpoints := toolssvc.NewEndpoints(toolsSvc)
4545
toolsServer := toolssvr.New(toolsEndpoints, mux, CustomRequestDecoder, goahttp.ResponseEncoder, errorHandler(logger), nil)
4646
toolssvr.Mount(mux, toolsServer)

v2/pkgs/tools.go

+64-13
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,19 @@ var (
5757
//
5858
// It requires an Index Resource to search for tools
5959
type Tools struct {
60-
index *index.Resource
61-
folder string
60+
index *index.Resource
61+
folder string
62+
behaviour string
6263
}
6364

6465
// New will return a Tool object, allowing the caller to execute operations on it.
6566
// The New function will accept an index as parameter (used to download the indexes)
6667
// and a folder used to download the indexes
67-
func New(index *index.Resource, folder string) *Tools {
68+
func New(index *index.Resource, folder, behaviour string) *Tools {
6869
return &Tools{
69-
index: index,
70-
folder: folder,
70+
index: index,
71+
folder: folder,
72+
behaviour: behaviour,
7173
}
7274
}
7375

@@ -175,6 +177,23 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
175177

176178
correctTool, correctSystem, found := FindTool(payload.Packager, payload.Name, payload.Version, index)
177179
path = filepath.Join(payload.Packager, correctTool.Name, correctTool.Version)
180+
181+
key := correctTool.Name + "-" + correctTool.Version
182+
// Check if it already exists
183+
if t.behaviour == "keep" && pathExists(t.folder) {
184+
location, ok, err := checkInstalled(t.folder, key)
185+
if err != nil {
186+
return nil, err
187+
}
188+
if ok && pathExists(location) {
189+
// overwrite the default tool with this one
190+
err := writeInstalled(t.folder, path)
191+
if err != nil {
192+
return nil, err
193+
}
194+
return &tools.Operation{Status: "ok"}, nil
195+
}
196+
}
178197
if found {
179198
return t.install(ctx, path, correctSystem.URL, correctSystem.Checksum)
180199
}
@@ -262,21 +281,42 @@ func rename(base string) extract.Renamer {
262281
}
263282
}
264283

265-
func writeInstalled(folder, path string) error {
284+
func readInstalled(installedFile string) (map[string]string, error) {
266285
// read installed.json
267286
installed := map[string]string{}
268-
269-
installedFile, err := utilities.SafeJoin(folder, "installed.json")
270-
if err != nil {
271-
return err
272-
}
273287
data, err := os.ReadFile(installedFile)
274288
if err == nil {
275289
err = json.Unmarshal(data, &installed)
276290
if err != nil {
277-
return err
291+
return nil, err
278292
}
279293
}
294+
return installed, nil
295+
}
296+
297+
func checkInstalled(folder, key string) (string, bool, error) {
298+
installedFile, err := utilities.SafeJoin(folder, "installed.json")
299+
if err != nil {
300+
return "", false, err
301+
}
302+
installed, err := readInstalled(installedFile)
303+
if err != nil {
304+
return "", false, err
305+
}
306+
location, ok := installed[key]
307+
return location, ok, err
308+
}
309+
310+
func writeInstalled(folder, path string) error {
311+
// read installed.json
312+
installedFile, err := utilities.SafeJoin(folder, "installed.json")
313+
if err != nil {
314+
return err
315+
}
316+
installed, err := readInstalled(installedFile)
317+
if err != nil {
318+
return err
319+
}
280320

281321
parts := strings.Split(path, string(filepath.Separator))
282322
tool := parts[len(parts)-2]
@@ -288,14 +328,25 @@ func writeInstalled(folder, path string) error {
288328
installed[tool] = toolFile
289329
installed[toolWithVersion] = toolFile
290330

291-
data, err = json.Marshal(installed)
331+
data, err := json.Marshal(installed)
292332
if err != nil {
293333
return err
294334
}
295335

296336
return os.WriteFile(installedFile, data, 0644)
297337
}
298338

339+
func pathExists(path string) bool {
340+
_, err := os.Stat(path)
341+
if err == nil {
342+
return true
343+
}
344+
if os.IsNotExist(err) {
345+
return false
346+
}
347+
return true
348+
}
349+
299350
// FindTool searches the index for the correct tool and system that match the specified tool name and version
300351
func FindTool(pack, name, version string, data Index) (Tool, System, bool) {
301352
var correctTool Tool

v2/pkgs/tools_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestTools(t *testing.T) {
4545
// Instantiate Index
4646
Index := index.Init(indexURL, config.GetDataDir())
4747

48-
service := pkgs.New(Index, tmp)
48+
service := pkgs.New(Index, tmp, "replace")
4949

5050
ctx := context.Background()
5151

@@ -126,7 +126,7 @@ func TestEvilFilename(t *testing.T) {
126126
// Instantiate Index
127127
Index := index.Init(indexURL, config.GetDataDir())
128128

129-
service := pkgs.New(Index, tmp)
129+
service := pkgs.New(Index, tmp, "replace")
130130

131131
ctx := context.Background()
132132

@@ -195,7 +195,7 @@ func TestInstalledHead(t *testing.T) {
195195
// Instantiate Index
196196
Index := index.Init(indexURL, config.GetDataDir())
197197

198-
service := pkgs.New(Index, tmp)
198+
service := pkgs.New(Index, tmp, "replace")
199199

200200
ctx := context.Background()
201201

@@ -216,7 +216,7 @@ func TestInstall(t *testing.T) {
216216
LastRefresh: time.Now(),
217217
}
218218

219-
tool := pkgs.New(testIndex, tmp)
219+
tool := pkgs.New(testIndex, tmp, "replace")
220220

221221
ctx := context.Background()
222222

0 commit comments

Comments
 (0)