Skip to content

Commit 1f51048

Browse files
committed
implement crude tool install
1 parent 071b582 commit 1f51048

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3790
-29
lines changed

Gopkg.lock

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/download.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
227227
// Decompress
228228
t.Logger("Unpacking tool " + name)
229229

230-
location := path.Join( dir(), pack, correctTool.Name, correctTool.Version)
230+
location := path.Join(dir(), pack, correctTool.Name, correctTool.Version)
231231
err = os.RemoveAll(location)
232232

233233
if err != nil {
@@ -393,7 +393,7 @@ func findBaseDir(dirList []string) string {
393393
return commonBaseDir
394394
}
395395

396-
func extractZip(log func(msg string) , body []byte, location string) (string, error) {
396+
func extractZip(log func(msg string), body []byte, location string) (string, error) {
397397
path, err := utilities.SaveFileonTempDir("tooldownloaded.zip", bytes.NewReader(body))
398398
r, err := zip.OpenReader(path)
399399
if err != nil {
@@ -442,7 +442,7 @@ func extractZip(log func(msg string) , body []byte, location string) (string, e
442442
return location, nil
443443
}
444444

445-
func extractTarGz(log func(msg string),body []byte, location string) (string, error) {
445+
func extractTarGz(log func(msg string), body []byte, location string) (string, error) {
446446
bodyCopy := make([]byte, len(body))
447447
copy(bodyCopy, body)
448448
tarFile, _ := gzip.NewReader(bytes.NewReader(body))
@@ -506,8 +506,7 @@ func extractTarGz(log func(msg string),body []byte, location string) (string, er
506506
return location, nil
507507
}
508508

509-
510-
func extractBz2(log func(msg string),body []byte, location string) (string, error) {
509+
func extractBz2(log func(msg string), body []byte, location string) (string, error) {
511510
bodyCopy := make([]byte, len(body))
512511
copy(bodyCopy, body)
513512
tarFile := bzip2.NewReader(bytes.NewReader(body))
@@ -573,7 +572,6 @@ func extractBz2(log func(msg string),body []byte, location string) (string, err
573572
return location, nil
574573
}
575574

576-
577575
func (t *Tools) installDrivers(location string) error {
578576
OK_PRESSED := 6
579577
extension := ".bat"

v2/pkgs/indexes.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,3 @@ func (c *Indexes) Remove(ctx context.Context, payload *indexes.IndexPayload) err
8585
filename := url.PathEscape(payload.URL)
8686
return os.RemoveAll(filepath.Join(c.Folder, filename))
8787
}
88-
89-
type Index struct {
90-
Packages []struct {
91-
Name string `json:"name"`
92-
Maintainer string `json:"maintainer"`
93-
WebsiteURL string `json:"websiteURL"`
94-
Email string `json:"email,omitempty"`
95-
Help struct {
96-
Online string `json:"online"`
97-
} `json:"help,omitempty"`
98-
Tools []struct {
99-
Name string `json:"name"`
100-
Version string `json:"version"`
101-
Systems []struct {
102-
Host string `json:"host"`
103-
URL string `json:"url"`
104-
ArchiveFileName string `json:"archiveFileName"`
105-
Checksum string `json:"checksum"`
106-
Size string `json:"size"`
107-
} `json:"systems"`
108-
} `json:"tools"`
109-
} `json:"packages"`
110-
}

v2/pkgs/pkgs.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package pkgs
2+
3+
type Index struct {
4+
Packages []struct {
5+
Name string `json:"name"`
6+
Maintainer string `json:"maintainer"`
7+
WebsiteURL string `json:"websiteURL"`
8+
Email string `json:"email,omitempty"`
9+
Help struct {
10+
Online string `json:"online"`
11+
} `json:"help,omitempty"`
12+
Tools []Tool `json:"tools"`
13+
} `json:"packages"`
14+
}
15+
16+
type Tool struct {
17+
Name string `json:"name"`
18+
Version string `json:"version"`
19+
Systems []struct {
20+
Host string `json:"host"`
21+
URL string `json:"url"`
22+
ArchiveFileName string `json:"archiveFileName"`
23+
Checksum string `json:"checksum"`
24+
Size string `json:"size"`
25+
} `json:"systems"`
26+
}

v2/pkgs/tools.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package pkgs
22

33
import (
44
"context"
5+
"fmt"
56
"io/ioutil"
7+
"net/http"
68
"os/user"
79
"path/filepath"
10+
"runtime"
811

912
"github.com/arduino/arduino-create-agent/gen/tools"
13+
"github.com/codeclysm/extract"
1014
"github.com/sirupsen/logrus"
15+
"github.com/xrash/smetrics"
1116
)
1217

1318
type Tools struct {
@@ -86,9 +91,77 @@ func (c *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
8691
}
8792

8893
func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) error {
94+
list, err := c.Indexes.List(ctx)
95+
if err != nil {
96+
return err
97+
}
98+
99+
for _, url := range list {
100+
index, err := c.Indexes.Get(ctx, url)
101+
if err != nil {
102+
return err
103+
}
104+
105+
for _, packager := range index.Packages {
106+
if packager.Name != payload.Packager {
107+
continue
108+
}
109+
110+
for _, tool := range packager.Tools {
111+
if tool.Name == payload.Name &&
112+
tool.Version == payload.Version {
113+
return c.install(ctx, tool)
114+
}
115+
}
116+
}
117+
}
118+
119+
return nil
120+
}
121+
122+
func (c *Tools) install(ctx context.Context, tool Tool) error {
123+
i := findSystem(tool)
124+
125+
// Download
126+
fmt.Println(tool.Systems[i].URL)
127+
res, err := http.Get(tool.Systems[i].URL)
128+
if err != nil {
129+
return err
130+
}
131+
defer res.Body.Close()
132+
133+
err = extract.Archive(ctx, res.Body, c.Folder, nil)
134+
if err != nil {
135+
return err
136+
}
137+
89138
return nil
90139
}
91140

92141
func (c *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) error {
93142
return nil
94143
}
144+
145+
func findSystem(tool Tool) int {
146+
var systems = map[string]string{
147+
"linuxamd64": "x86_64-linux-gnu",
148+
"linux386": "i686-linux-gnu",
149+
"darwinamd64": "apple-darwin",
150+
"windows386": "i686-mingw32",
151+
"windowsamd64": "i686-mingw32",
152+
"linuxarm": "arm-linux-gnueabihf",
153+
}
154+
155+
var correctSystem int
156+
maxSimilarity := 0.7
157+
158+
for i, system := range tool.Systems {
159+
similarity := smetrics.Jaro(system.Host, systems[runtime.GOOS+runtime.GOARCH])
160+
if similarity > maxSimilarity {
161+
correctSystem = i
162+
maxSimilarity = similarity
163+
}
164+
}
165+
166+
return correctSystem
167+
}

vendor/github.com/codeclysm/extract/.travis.yml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/codeclysm/extract/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/codeclysm/extract/README.md

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/codeclysm/extract/cancelable_reader.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)