Skip to content

Commit b2d1aa0

Browse files
Use a map to store installed.json information
1 parent 2e0309a commit b2d1aa0

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

Diff for: v2/pkgs/tools.go

+22-36
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,23 @@ type Tools struct {
6161
index *index.Resource
6262
folder string
6363
behaviour string
64+
installed map[string]string
6465
mutex sync.RWMutex
6566
}
6667

6768
// New will return a Tool object, allowing the caller to execute operations on it.
6869
// The New function will accept an index as parameter (used to download the indexes)
6970
// and a folder used to download the indexes
7071
func New(index *index.Resource, folder, behaviour string) *Tools {
71-
return &Tools{
72+
t := &Tools{
7273
index: index,
7374
folder: folder,
7475
behaviour: behaviour,
76+
installed: map[string]string{},
7577
mutex: sync.RWMutex{},
7678
}
79+
t.readInstalled()
80+
return t
7781
}
7882

7983
// Installedhead is here only because it was required by the front-end.
@@ -184,10 +188,7 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
184188
key := correctTool.Name + "-" + correctTool.Version
185189
// Check if it already exists
186190
if t.behaviour == "keep" && pathExists(t.folder) {
187-
location, ok, err := checkInstalled(t.folder, key)
188-
if err != nil {
189-
return nil, err
190-
}
191+
location, ok := t.installed[key]
191192
if ok && pathExists(location) {
192193
// overwrite the default tool with this one
193194
err := t.writeInstalled(path)
@@ -288,44 +289,24 @@ func rename(base string) extract.Renamer {
288289
}
289290
}
290291

291-
func readInstalled(installedFile string) (map[string]string, error) {
292+
func (t *Tools) readInstalled() error {
293+
t.mutex.Lock()
294+
defer t.mutex.Unlock()
292295
// read installed.json
293-
installed := map[string]string{}
294-
data, err := os.ReadFile(installedFile)
295-
if err == nil {
296-
err = json.Unmarshal(data, &installed)
297-
if err != nil {
298-
return nil, err
299-
}
300-
}
301-
return installed, nil
302-
}
303-
304-
func checkInstalled(folder, key string) (string, bool, error) {
305-
installedFile, err := utilities.SafeJoin(folder, "installed.json")
296+
installedFile, err := utilities.SafeJoin(t.folder, "installed.json")
306297
if err != nil {
307-
return "", false, err
298+
return err
308299
}
309-
installed, err := readInstalled(installedFile)
300+
data, err := os.ReadFile(installedFile)
310301
if err != nil {
311-
return "", false, err
302+
return err
312303
}
313-
location, ok := installed[key]
314-
return location, ok, err
304+
return json.Unmarshal(data, &t.installed)
315305
}
316306

317307
func (t *Tools) writeInstalled(path string) error {
318308
t.mutex.RLock()
319309
defer t.mutex.RUnlock()
320-
// read installed.json
321-
installedFile, err := utilities.SafeJoin(t.folder, "installed.json")
322-
if err != nil {
323-
return err
324-
}
325-
installed, err := readInstalled(installedFile)
326-
if err != nil {
327-
return err
328-
}
329310

330311
parts := strings.Split(path, string(filepath.Separator))
331312
tool := parts[len(parts)-2]
@@ -334,10 +315,15 @@ func (t *Tools) writeInstalled(path string) error {
334315
if err != nil {
335316
return err
336317
}
337-
installed[tool] = toolFile
338-
installed[toolWithVersion] = toolFile
318+
t.installed[tool] = toolFile
319+
t.installed[toolWithVersion] = toolFile
339320

340-
data, err := json.Marshal(installed)
321+
data, err := json.Marshal(t.installed)
322+
if err != nil {
323+
return err
324+
}
325+
326+
installedFile, err := utilities.SafeJoin(t.folder, "installed.json")
341327
if err != nil {
342328
return err
343329
}

0 commit comments

Comments
 (0)