Skip to content

Commit c34ad0f

Browse files
committed
Preserve compatibility with v1 upload
1 parent fd1eef1 commit c34ad0f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

tools/tools.go

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tools
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"os/user"
@@ -60,6 +61,16 @@ func (t *Tools) GetLocation(command string) (string, error) {
6061
var location string
6162
var ok bool
6263

64+
// Load installed
65+
fmt.Println(t.installed)
66+
67+
err := t.readMap()
68+
if err != nil {
69+
return "", err
70+
}
71+
72+
fmt.Println(t.installed)
73+
6374
// use string similarity to resolve a runtime var with a "similar" map element
6475
if location, ok = t.installed[command]; !ok {
6576
maxSimilarity := 0.0

v2/pkgs/tools.go

+32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"crypto/sha256"
77
"encoding/hex"
8+
"encoding/json"
89
"errors"
910
"fmt"
1011
"io"
@@ -181,6 +182,12 @@ func (c *Tools) install(ctx context.Context, path, url, checksum string) error {
181182
return errors.New("checksum doesn't match")
182183
}
183184

185+
// Write installed.json for retrocompatibility with v1
186+
err = writeInstalled(c.Folder, path)
187+
if err != nil {
188+
return err
189+
}
190+
184191
return nil
185192
}
186193

@@ -224,3 +231,28 @@ func findSystem(tool Tool) int {
224231

225232
return correctSystem
226233
}
234+
235+
func writeInstalled(folder, path string) error {
236+
// read installed.json
237+
installed := map[string]string{}
238+
239+
data, err := ioutil.ReadFile(filepath.Join(folder, "installed.json"))
240+
if err == nil {
241+
err = json.Unmarshal(data, &installed)
242+
if err != nil {
243+
return err
244+
}
245+
}
246+
247+
parts := strings.Split(path, string(filepath.Separator))
248+
tool := parts[len(parts)-2]
249+
250+
installed[tool] = filepath.Join(folder, path)
251+
252+
data, err = json.Marshal(installed)
253+
if err != nil {
254+
return err
255+
}
256+
257+
return ioutil.WriteFile(filepath.Join(folder, "installed.json"), data, 0644)
258+
}

0 commit comments

Comments
 (0)