Skip to content

Commit f89acc8

Browse files
committed
Move the function to download tools in a package
Download from http://downloads.arduino.cc/packages/package_index.json Update the Installed Map at startup
1 parent 3cdda5e commit f89acc8

File tree

7 files changed

+462
-173
lines changed

7 files changed

+462
-173
lines changed

docs/tools.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# tools
2+
--
3+
import "github.com/arduino/arduino-create-agent/tools"
4+
5+
6+
## Usage
7+
8+
#### type Tools
9+
10+
```go
11+
type Tools struct {
12+
Directory string
13+
IndexURL string
14+
Logger log.StdLogger
15+
}
16+
```
17+
18+
Tools handle the tools necessary for an upload on a board. It provides a means
19+
to download a tool from the arduino servers.
20+
21+
- *Directory* contains the location where the tools are downloaded.
22+
- *IndexURL* contains the url where the tools description is contained.
23+
- *Logger* is a StdLogger used for reporting debug and info messages
24+
- *installed* contains a map of the tools and their exact location
25+
26+
Usage: You have to instantiate the struct by passing it the required parameters:
27+
28+
_tools := tools.Tools{
29+
Directory: "/home/user/.arduino-create",
30+
IndexURL: "http://downloads.arduino.cc/packages/package_index.json"
31+
Logger: log.Logger
32+
}
33+
34+
#### func (*Tools) Download
35+
36+
```go
37+
func (t *Tools) Download(name, version, behaviour string) error
38+
```
39+
Download will parse the index at the indexURL for the tool to download. It will
40+
extract it in a folder in .arduino-create, and it will update the Installed map.
41+
42+
name contains the name of the tool. version contains the version of the tool.
43+
behaviour contains the strategy to use when there is already a tool installed
44+
45+
If version is "latest" it will always download the latest version (regardless of
46+
the value of behaviour)
47+
48+
If version is not "latest" and behaviour is "replace", it will download the
49+
version again. If instead behaviour is "keep" it will not download the version
50+
if it already exists.
51+
52+
#### func (*Tools) GetLocation
53+
54+
```go
55+
func (t *Tools) GetLocation(command string) (string, error)
56+
```
57+
GetLocation extracts the toolname from a command like
58+
59+
#### func (*Tools) Init
60+
61+
```go
62+
func (t *Tools) Init()
63+
```
64+
Init creates the Installed map and populates it from a file in .arduino-create

download.go

Lines changed: 0 additions & 124 deletions
This file was deleted.

hub.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55

66
log "github.com/Sirupsen/logrus"
7-
"github.com/arduino/arduino-create-agent/tools"
87
"github.com/kardianos/osext"
98
//"os"
109
"os/exec"
@@ -192,8 +191,19 @@ func checkCmd(m []byte) {
192191
go spList(true)
193192
} else if strings.HasPrefix(sl, "downloadtool") {
194193
args := strings.Split(s, " ")
195-
if len(args) > 2 {
196-
go tools.Download(args[1], args[2])
194+
if len(args) > 1 {
195+
go func() {
196+
err := Tools.Download(args[1], "latest", "replace")
197+
if err != nil {
198+
mapD := map[string]string{"DownloadStatus": "Error", "Msg": err.Error()}
199+
mapB, _ := json.Marshal(mapD)
200+
h.broadcastSys <- mapB
201+
} else {
202+
mapD := map[string]string{"DownloadStatus": "Success", "Msg": "Map Updated"}
203+
mapB, _ := json.Marshal(mapD)
204+
h.broadcastSys <- mapB
205+
}
206+
}()
197207
}
198208
} else if strings.HasPrefix(sl, "bufferalgorithm") {
199209
go spBufferAlgorithms()

main.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ import (
1313
"time"
1414

1515
log "github.com/Sirupsen/logrus"
16-
<<<<<<< e73846650fde9b0955aa35e237100ec552af47fb
17-
=======
1816
"github.com/arduino/arduino-create-agent/tools"
19-
"github.com/carlescere/scheduler"
20-
>>>>>>> Move initialization of tools in package tools
2117
"github.com/gin-gonic/gin"
2218
"github.com/itsjamie/gin-cors"
2319
"github.com/kardianos/osext"
@@ -38,16 +34,16 @@ var (
3834
gcType = flag.String("gc", "std", "Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)")
3935
logDump = flag.String("log", "off", "off = (default)")
4036
// hostname. allow user to override, otherwise we look it up
41-
hostname = flag.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
42-
updateUrl = flag.String("updateUrl", "", "")
43-
appName = flag.String("appName", "", "")
44-
genCert = flag.Bool("generateCert", false, "")
45-
globalToolsMap = make(map[string]string)
46-
port string
47-
portSSL string
48-
origins = flag.String("origins", "", "Allowed origin list for CORS")
49-
signatureKey = flag.String("signatureKey", "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvc0yZr1yUSen7qmE3cxF\nIE12rCksDnqR+Hp7o0nGi9123eCSFcJ7CkIRC8F+8JMhgI3zNqn4cUEn47I3RKD1\nZChPUCMiJCvbLbloxfdJrUi7gcSgUXrlKQStOKF5Iz7xv1M4XOP3JtjXLGo3EnJ1\npFgdWTOyoSrA8/w1rck4c/ISXZSinVAggPxmLwVEAAln6Itj6giIZHKvA2fL2o8z\nCeK057Lu8X6u2CG8tRWSQzVoKIQw/PKK6CNXCAy8vo4EkXudRutnEYHEJlPkVgPn\n2qP06GI+I+9zKE37iqj0k1/wFaCVXHXIvn06YrmjQw6I0dDj/60Wvi500FuRVpn9\ntwIDAQAB\n-----END PUBLIC KEY-----", "Pem-encoded public key to verify signed commandlines")
50-
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
37+
hostname = flag.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
38+
updateUrl = flag.String("updateUrl", "", "")
39+
appName = flag.String("appName", "", "")
40+
genCert = flag.Bool("generateCert", false, "")
41+
port string
42+
portSSL string
43+
origins = flag.String("origins", "", "Allowed origin list for CORS")
44+
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
45+
signatureKey = flag.String("signatureKey", "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvc0yZr1yUSen7qmE3cxF\nIE12rCksDnqR+Hp7o0nGi9123eCSFcJ7CkIRC8F+8JMhgI3zNqn4cUEn47I3RKD1\nZChPUCMiJCvbLbloxfdJrUi7gcSgUXrlKQStOKF5Iz7xv1M4XOP3JtjXLGo3EnJ1\npFgdWTOyoSrA8/w1rck4c/ISXZSinVAggPxmLwVEAAln6Itj6giIZHKvA2fL2o8z\nCeK057Lu8X6u2CG8tRWSQzVoKIQw/PKK6CNXCAy8vo4EkXudRutnEYHEJlPkVgPn\n2qP06GI+I+9zKE37iqj0k1/wFaCVXHXIvn06YrmjQw6I0dDj/60Wvi500FuRVpn9\ntwIDAQAB\n-----END PUBLIC KEY-----", "Pem-encoded public key to verify signed commandlines")
46+
Tools tools.Tools
5147
)
5248

5349
type NullWriter int
@@ -90,13 +86,19 @@ func main() {
9086
src, _ := osext.Executable()
9187
dest := filepath.Dir(src)
9288

93-
tools.CreateDir()
89+
// Instantiate Tools
90+
Tools = tools.Tools{
91+
Directory: "/home/user/.arduino-create",
92+
IndexURL: "http://downloads.arduino.cc/packages/package_index.json",
93+
Logger: log.New(),
94+
}
95+
Tools.Init()
9496

9597
if embedded_autoextract {
9698
// save the config.ini (if it exists)
9799
if _, err := os.Stat(dest + "/" + *configIni); os.IsNotExist(err) {
98100
log.Println("First run, unzipping self")
99-
err := Unzip(src, dest)
101+
err := utilities.Unzip(src, dest)
100102
log.Println("Self extraction, err:", err)
101103
}
102104

programmer.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import (
55
"bytes"
66
"encoding/json"
77
"fmt"
8-
log "github.com/Sirupsen/logrus"
9-
"github.com/facchinm/go-serial"
10-
"github.com/mattn/go-shellwords"
11-
"github.com/sfreiberg/simplessh"
12-
"github.com/xrash/smetrics"
138
"io"
149
"mime/multipart"
1510
"net/http"
@@ -21,6 +16,11 @@ import (
2116
"strconv"
2217
"strings"
2318
"time"
19+
20+
log "github.com/Sirupsen/logrus"
21+
"github.com/facchinm/go-serial"
22+
"github.com/mattn/go-shellwords"
23+
"github.com/sfreiberg/simplessh"
2424
)
2525

2626
var compiling = false
@@ -231,23 +231,16 @@ func spProgramLocal(portname string, boardname string, filePath string, commandl
231231
var runtimeRe = regexp.MustCompile("\\{(.*?)\\}")
232232
runtimeVars := runtimeRe.FindAllString(commandline, -1)
233233

234-
fmt.Println(runtimeVars)
235-
236234
for _, element := range runtimeVars {
237235

238-
// use string similarity to resolve a runtime var with a "similar" map element
239-
if globalToolsMap[element] == "" {
240-
max_similarity := 0.0
241-
for i, candidate := range globalToolsMap {
242-
similarity := smetrics.Jaro(element, i)
243-
if similarity > 0.8 && similarity > max_similarity {
244-
max_similarity = similarity
245-
globalToolsMap[element] = candidate
246-
}
247-
}
236+
location, err := Tools.GetLocation(element)
237+
if err != nil {
238+
log.Printf("Command finished with error: %v", err)
239+
mapD := map[string]string{"ProgrammerStatus": "Error", "Msg": "Could not find the upload tool"}
240+
mapB, _ := json.Marshal(mapD)
241+
h.broadcastSys <- mapB
248242
}
249-
250-
commandline = strings.Replace(commandline, element, globalToolsMap[element], 1)
243+
commandline = strings.Replace(commandline, element, location, 1)
251244
}
252245

253246
z, _ := shellwords.Parse(commandline)
@@ -291,7 +284,6 @@ func spProgramRW(portname string, boardname string, filePath string, commandline
291284
var oscmd *exec.Cmd
292285

293286
func spHandlerProgram(flasher string, cmdString []string) error {
294-
295287
// if runtime.GOOS == "darwin" {
296288
// sh, _ := exec.LookPath("sh")
297289
// // prepend the flasher to run it via sh
@@ -391,7 +383,6 @@ func formatCmdline(cmdline string, boardOptions map[string]string) (string, bool
391383
}
392384
}
393385
}
394-
log.Println(cmdline)
395386
return cmdline, true
396387
}
397388

0 commit comments

Comments
 (0)