Skip to content

Improve installed.json handling in v2/tools #983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use a map to store installed.json information
  • Loading branch information
MatteoPologruto committed Aug 5, 2024
commit b2d1aa0a56b5b1f0deb247a46e19d17659e2dce5
58 changes: 22 additions & 36 deletions v2/pkgs/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,23 @@ type Tools struct {
index *index.Resource
folder string
behaviour string
installed map[string]string
mutex sync.RWMutex
}

// New will return a Tool object, allowing the caller to execute operations on it.
// The New function will accept an index as parameter (used to download the indexes)
// and a folder used to download the indexes
func New(index *index.Resource, folder, behaviour string) *Tools {
return &Tools{
t := &Tools{
index: index,
folder: folder,
behaviour: behaviour,
installed: map[string]string{},
mutex: sync.RWMutex{},
}
t.readInstalled()
return t
}

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

func readInstalled(installedFile string) (map[string]string, error) {
func (t *Tools) readInstalled() error {
t.mutex.Lock()
defer t.mutex.Unlock()
// read installed.json
installed := map[string]string{}
data, err := os.ReadFile(installedFile)
if err == nil {
err = json.Unmarshal(data, &installed)
if err != nil {
return nil, err
}
}
return installed, nil
}

func checkInstalled(folder, key string) (string, bool, error) {
installedFile, err := utilities.SafeJoin(folder, "installed.json")
installedFile, err := utilities.SafeJoin(t.folder, "installed.json")
if err != nil {
return "", false, err
return err
}
installed, err := readInstalled(installedFile)
data, err := os.ReadFile(installedFile)
if err != nil {
return "", false, err
return err
}
location, ok := installed[key]
return location, ok, err
return json.Unmarshal(data, &t.installed)
}

func (t *Tools) writeInstalled(path string) error {
t.mutex.RLock()
defer t.mutex.RUnlock()
// read installed.json
installedFile, err := utilities.SafeJoin(t.folder, "installed.json")
if err != nil {
return err
}
installed, err := readInstalled(installedFile)
if err != nil {
return err
}

parts := strings.Split(path, string(filepath.Separator))
tool := parts[len(parts)-2]
Expand All @@ -334,10 +315,15 @@ func (t *Tools) writeInstalled(path string) error {
if err != nil {
return err
}
installed[tool] = toolFile
installed[toolWithVersion] = toolFile
t.installed[tool] = toolFile
t.installed[toolWithVersion] = toolFile

data, err := json.Marshal(installed)
data, err := json.Marshal(t.installed)
if err != nil {
return err
}

installedFile, err := utilities.SafeJoin(t.folder, "installed.json")
if err != nil {
return err
}
Expand Down