Skip to content

Commit eb6fc1f

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 ea9af5a commit eb6fc1f

File tree

7 files changed

+460
-167
lines changed

7 files changed

+460
-167
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: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ var (
3838
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)")
3939
logDump = flag.String("log", "off", "off = (default)")
4040
// 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-
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
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+
port string
46+
portSSL string
47+
origins = flag.String("origins", "", "Allowed origin list for CORS")
48+
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
49+
Tools tools.Tools
5050
)
5151

5252
type NullWriter int
@@ -89,7 +89,13 @@ func main() {
8989
src, _ := osext.Executable()
9090
dest := filepath.Dir(src)
9191

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

94100
if embedded_autoextract {
95101
// save the config.ini (if it exists)

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)