Skip to content

Commit fd7c422

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 c51d119 commit fd7c422

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"
@@ -188,8 +187,19 @@ func checkCmd(m []byte) {
188187
go spList(true)
189188
} else if strings.HasPrefix(sl, "downloadtool") {
190189
args := strings.Split(s, " ")
191-
if len(args) > 2 {
192-
go tools.Download(args[1], args[2])
190+
if len(args) > 1 {
191+
go func() {
192+
err := Tools.Download(args[1], "latest", "replace")
193+
if err != nil {
194+
mapD := map[string]string{"DownloadStatus": "Error", "Msg": err.Error()}
195+
mapB, _ := json.Marshal(mapD)
196+
h.broadcastSys <- mapB
197+
} else {
198+
mapD := map[string]string{"DownloadStatus": "Success", "Msg": "Map Updated"}
199+
mapB, _ := json.Marshal(mapD)
200+
h.broadcastSys <- mapB
201+
}
202+
}()
193203
}
194204
} else if strings.HasPrefix(sl, "bufferalgorithm") {
195205
go spBufferAlgorithms()

main.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ var (
3636
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)")
3737
logDump = flag.String("log", "off", "off = (default)")
3838
// hostname. allow user to override, otherwise we look it up
39-
hostname = flag.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
40-
updateUrl = flag.String("updateUrl", "", "")
41-
appName = flag.String("appName", "", "")
42-
genCert = flag.Bool("generateCert", false, "")
43-
globalToolsMap = make(map[string]string)
44-
port string
45-
portSSL string
46-
origins = flag.String("origins", "", "Allowed origin list for CORS")
47-
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
39+
hostname = flag.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
40+
updateUrl = flag.String("updateUrl", "", "")
41+
appName = flag.String("appName", "", "")
42+
genCert = flag.Bool("generateCert", false, "")
43+
port string
44+
portSSL string
45+
origins = flag.String("origins", "", "Allowed origin list for CORS")
46+
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
47+
Tools tools.Tools
4848
)
4949

5050
type NullWriter int
@@ -87,7 +87,13 @@ func main() {
8787
src, _ := osext.Executable()
8888
dest := filepath.Dir(src)
8989

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

9298
if embedded_autoextract {
9399
// 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)