Skip to content

Commit 48930a2

Browse files
Use a map to store installed.json information
1 parent 7728ff1 commit 48930a2

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

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)
@@ -284,44 +285,24 @@ func rename(base string) extract.Renamer {
284285
}
285286
}
286287

287-
func readInstalled(installedFile string) (map[string]string, error) {
288+
func (t *Tools) readInstalled() error {
289+
t.mutex.Lock()
290+
defer t.mutex.Unlock()
288291
// read installed.json
289-
installed := map[string]string{}
290-
data, err := os.ReadFile(installedFile)
291-
if err == nil {
292-
err = json.Unmarshal(data, &installed)
293-
if err != nil {
294-
return nil, err
295-
}
296-
}
297-
return installed, nil
298-
}
299-
300-
func checkInstalled(folder, key string) (string, bool, error) {
301-
installedFile, err := utilities.SafeJoin(folder, "installed.json")
292+
installedFile, err := utilities.SafeJoin(t.folder, "installed.json")
302293
if err != nil {
303-
return "", false, err
294+
return err
304295
}
305-
installed, err := readInstalled(installedFile)
296+
data, err := os.ReadFile(installedFile)
306297
if err != nil {
307-
return "", false, err
298+
return err
308299
}
309-
location, ok := installed[key]
310-
return location, ok, err
300+
return json.Unmarshal(data, &t.installed)
311301
}
312302

313303
func (t *Tools) writeInstalled(path string) error {
314304
t.mutex.RLock()
315305
defer t.mutex.RUnlock()
316-
// read installed.json
317-
installedFile, err := utilities.SafeJoin(t.folder, "installed.json")
318-
if err != nil {
319-
return err
320-
}
321-
installed, err := readInstalled(installedFile)
322-
if err != nil {
323-
return err
324-
}
325306

326307
parts := strings.Split(path, string(filepath.Separator))
327308
tool := parts[len(parts)-2]
@@ -330,10 +311,15 @@ func (t *Tools) writeInstalled(path string) error {
330311
if err != nil {
331312
return err
332313
}
333-
installed[tool] = toolFile
334-
installed[toolWithVersion] = toolFile
314+
t.installed[tool] = toolFile
315+
t.installed[toolWithVersion] = toolFile
335316

336-
data, err := json.Marshal(installed)
317+
data, err := json.Marshal(t.installed)
318+
if err != nil {
319+
return err
320+
}
321+
322+
installedFile, err := utilities.SafeJoin(t.folder, "installed.json")
337323
if err != nil {
338324
return err
339325
}

0 commit comments

Comments
 (0)