Skip to content

Commit 66ed59a

Browse files
committed
Merge pull request #95 from arduino/md5_fixed
[WIP] Download tools refactoring
2 parents 2630276 + b663ce9 commit 66ed59a

Some content is hidden

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

53 files changed

+9124
-262
lines changed

conn.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net/http"
1515

1616
log "github.com/Sirupsen/logrus"
17+
"github.com/arduino/arduino-create-agent/utilities"
1718
"github.com/gin-gonic/gin"
1819
"github.com/googollee/go-socket.io"
1920
)
@@ -95,7 +96,7 @@ func uploadHandler(c *gin.Context) {
9596

9697
buffer := bytes.NewBuffer(data.Hex)
9798

98-
path, err := saveFileonTempDir(data.Filename, buffer)
99+
path, err := utilities.SaveFileonTempDir(data.Filename, buffer)
99100
if err != nil {
100101
c.String(http.StatusBadRequest, err.Error())
101102
return

discovery.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
package main
3030

3131
import (
32-
log "github.com/Sirupsen/logrus"
33-
"github.com/oleksandr/bonjour"
3432
"net"
3533
"strings"
3634
"time"
35+
36+
log "github.com/Sirupsen/logrus"
37+
"github.com/oleksandr/bonjour"
3738
)
3839

3940
const timeoutConst = 2
@@ -143,3 +144,14 @@ func getPorts() ([]OsSerialPort, error) {
143144
return arrPorts, nil
144145
}
145146
}
147+
148+
// Filter returns a new slice containing all OsSerialPort in the slice that satisfy the predicate f.
149+
func Filter(vs []OsSerialPort, f func(OsSerialPort) bool) []OsSerialPort {
150+
var vsf []OsSerialPort
151+
for _, v := range vs {
152+
if f(v) {
153+
vsf = append(vsf, v)
154+
}
155+
}
156+
return vsf
157+
}

docs/tools.md

+64
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

-124
This file was deleted.

hub.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
56
log "github.com/Sirupsen/logrus"
67
"github.com/kardianos/osext"
78
//"os"
@@ -190,8 +191,19 @@ func checkCmd(m []byte) {
190191
go spList(true)
191192
} else if strings.HasPrefix(sl, "downloadtool") {
192193
args := strings.Split(s, " ")
193-
if len(args) > 2 {
194-
go spDownloadTool(args[1], args[2])
194+
if len(args) > 1 {
195+
go func() {
196+
err := Tools.Download(args[1], "latest", "keep")
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+
}()
195207
}
196208
} else if strings.HasPrefix(sl, "bufferalgorithm") {
197209
go spBufferAlgorithms()

killbrowser_linux.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package main
33
import (
44
"os/exec"
55
"strings"
6+
7+
"github.com/arduino/arduino-create-agent/utilities"
68
)
79

810
func findBrowser(process string) ([]byte, error) {
911
ps := exec.Command("ps", "-A", "-o", "command")
1012
grep := exec.Command("grep", process)
1113
head := exec.Command("head", "-n", "1")
1214

13-
return pipe_commands(ps, grep, head)
15+
return utilities.PipeCommands(ps, grep, head)
1416
}
1517

1618
func killBrowser(process string) ([]byte, error) {

main.go

+23-19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"time"
1515

1616
log "github.com/Sirupsen/logrus"
17+
"github.com/arduino/arduino-create-agent/tools"
18+
"github.com/arduino/arduino-create-agent/utilities"
1719
"github.com/gin-gonic/gin"
1820
"github.com/itsjamie/gin-cors"
1921
"github.com/kardianos/osext"
@@ -34,17 +36,17 @@ var (
3436
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)")
3537
logDump = flag.String("log", "off", "off = (default)")
3638
// hostname. allow user to override, otherwise we look it up
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-
globalToolsMap = make(map[string]string)
42-
tempToolsPath = createToolsDir()
43-
port string
44-
portSSL string
45-
origins = flag.String("origins", "", "Allowed origin list for CORS")
46-
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")
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+
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")
48+
Tools tools.Tools
49+
indexURL = flag.String("indexURL", "https://downloads.arduino.cc/packages/package_staging_index.json", "The address from where to download the index json containing the location of upload tools")
4850
)
4951

5052
type NullWriter int
@@ -60,11 +62,6 @@ func (u *logWriter) Write(p []byte) (n int, err error) {
6062

6163
var logger_ws logWriter
6264

63-
func createToolsDir() string {
64-
usr, _ := user.Current()
65-
return usr.HomeDir + "/.arduino-create"
66-
}
67-
6865
func homeHandler(c *gin.Context) {
6966
homeTemplate.Execute(c.Writer, c.Request.Host)
7067
}
@@ -92,14 +89,21 @@ func main() {
9289
src, _ := osext.Executable()
9390
dest := filepath.Dir(src)
9491

95-
os.Mkdir(tempToolsPath, 0777)
96-
hideFile(tempToolsPath)
92+
// Instantiate Tools
93+
usr, _ := user.Current()
94+
directory := filepath.Join(usr.HomeDir, ".arduino-create")
95+
Tools = tools.Tools{
96+
Directory: directory,
97+
IndexURL: *indexURL,
98+
Logger: log.New(),
99+
}
100+
Tools.Init()
97101

98102
if embedded_autoextract {
99103
// save the config.ini (if it exists)
100104
if _, err := os.Stat(dest + "/" + *configIni); os.IsNotExist(err) {
101105
log.Println("First run, unzipping self")
102-
err := Unzip(src, dest)
106+
err := utilities.Unzip(src, dest)
103107
log.Println("Self extraction, err:", err)
104108
}
105109

0 commit comments

Comments
 (0)