Skip to content

Commit 36af1ed

Browse files
committed
remove globas tools and index
1 parent 96f0f51 commit 36af1ed

File tree

9 files changed

+78
-64
lines changed

9 files changed

+78
-64
lines changed

config/config.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ updateUrl = https://downloads.arduino.cc/
77
origins = https://local.arduino.cc:8000
88
#httpProxy = http://your.proxy:port # Proxy server for HTTP requests
99
crashreport = false # enable crashreport logging
10-
autostartMacOS = true # the Arduino Create Agent is able to start automatically after login on macOS (launchd agent)
10+
autostartMacOS = true # the Arduino Create Agent is able to start automatically after login on macOS (launchd agent)

conn.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"os"
2828
"path/filepath"
2929

30+
"github.com/arduino/arduino-create-agent/tools"
3031
"github.com/arduino/arduino-create-agent/upload"
3132
"github.com/arduino/arduino-create-agent/utilities"
3233
"github.com/gin-gonic/gin"
@@ -80,7 +81,7 @@ type Upload struct {
8081

8182
var uploadStatusStr = "ProgrammerStatus"
8283

83-
func uploadHandler(h *hub, pubKey *rsa.PublicKey) func(*gin.Context) {
84+
func uploadHandler(h *hub, pubKey *rsa.PublicKey, tools *tools.Tools) func(*gin.Context) {
8485
return func(c *gin.Context) {
8586
data := new(Upload)
8687
if err := c.BindJSON(data); err != nil {
@@ -162,7 +163,7 @@ func uploadHandler(h *hub, pubKey *rsa.PublicKey) func(*gin.Context) {
162163

163164
go func() {
164165
// Resolve commandline
165-
commandline, err := upload.PartiallyResolve(data.Board, filePath, tmpdir, data.Commandline, data.Extra, Tools)
166+
commandline, err := upload.PartiallyResolve(data.Board, filePath, tmpdir, data.Commandline, data.Extra, tools)
166167
if err != nil {
167168
send(h, map[string]string{uploadStatusStr: "Error", "Msg": err.Error()})
168169
return

hub.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strconv"
2626
"strings"
2727

28+
"github.com/arduino/arduino-create-agent/tools"
2829
"github.com/arduino/arduino-create-agent/upload"
2930
log "github.com/sirupsen/logrus"
3031
)
@@ -50,9 +51,11 @@ type hub struct {
5051
serialHub *serialhub
5152

5253
serialPortList *serialPortList
54+
55+
tools *tools.Tools
5356
}
5457

55-
func newHub(serialhub *serialhub, serialList *serialPortList) *hub {
58+
func newHub(serialhub *serialhub, serialList *serialPortList, tools *tools.Tools) *hub {
5659
hub := &hub{
5760
broadcast: make(chan []byte, 1000),
5861
broadcastSys: make(chan []byte, 1000),
@@ -61,6 +64,7 @@ func newHub(serialhub *serialhub, serialList *serialPortList) *hub {
6164
connections: make(map[*connection]bool),
6265
serialHub: serialhub,
6366
serialPortList: serialList,
67+
tools: tools,
6468
}
6569

6670
hub.serialHub.OnRegister = func(port *serport) {
@@ -235,7 +239,12 @@ func (h *hub) checkCmd(m []byte) {
235239
behaviour = args[4]
236240
}
237241

238-
err := Tools.Download(pack, tool, toolVersion, behaviour)
242+
reportPendingProgress := func(msg string) {
243+
mapD := map[string]string{"DownloadStatus": "Pending", "Msg": msg}
244+
mapB, _ := json.Marshal(mapD)
245+
h.broadcastSys <- mapB
246+
}
247+
err := h.tools.Download(pack, tool, toolVersion, behaviour, reportPendingProgress)
239248
if err != nil {
240249
mapD := map[string]string{"DownloadStatus": "Error", "Msg": err.Error()}
241250
mapB, _ := json.Marshal(mapD)

main.go

+18-32
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package main
2020

2121
import (
2222
_ "embed"
23-
"encoding/json"
2423
"flag"
2524
"html/template"
2625
"io"
@@ -100,13 +99,6 @@ var homeTemplate = template.Must(template.New("home").Parse(homeTemplateHTML))
10099
//go:embed home.html
101100
var homeTemplateHTML string
102101

103-
// global clients
104-
var (
105-
Tools *tools.Tools
106-
Systray systray.Systray
107-
Index *index.Resource
108-
)
109-
110102
// FIXME; the loggerWS is useind in the multiwrite in the hub
111103
// type logWriter struct{}
112104

@@ -160,9 +152,9 @@ func main() {
160152
if src, err := os.Executable(); err != nil {
161153
panic(err)
162154
} else if restartPath := updater.Start(src); restartPath != "" {
163-
Systray.RestartWith(restartPath)
155+
stray.RestartWith(restartPath)
164156
} else {
165-
Systray.Start()
157+
stray.Start()
166158
}
167159
}
168160

@@ -183,15 +175,21 @@ func loop(stray *systray.Systray) {
183175
os.Exit(0)
184176
}
185177

186-
serialPorts := newSerialPortList()
178+
// Instantiate Index and Tools
179+
index := index.Init(*indexURL, config.GetDataDir())
180+
if signatureKey == nil || len(*signatureKey) == 0 {
181+
log.Panicf("signature public key should be set")
182+
}
183+
signaturePubKey, err := utilities.ParseRsaPublicKey([]byte(*signatureKey))
184+
if err != nil {
185+
log.Panicf("cannot parse signature key '%s'. %s", *signatureKey, err)
186+
}
187+
tools := tools.New(config.GetDataDir(), index, signaturePubKey)
188+
189+
serialPorts := newSerialPortList(tools)
187190
serialHub := newSerialHub()
188-
hub := newHub(serialHub, serialPorts)
189191

190-
logger := func(msg string) {
191-
mapD := map[string]string{"DownloadStatus": "Pending", "Msg": msg}
192-
mapB, _ := json.Marshal(mapD)
193-
hub.broadcastSys <- mapB
194-
}
192+
hub := newHub(serialHub, serialPorts, tools)
195193

196194
// Let's handle the config
197195
configDir := config.GetDefaultConfigDir()
@@ -263,7 +261,7 @@ func loop(stray *systray.Systray) {
263261
if err != nil {
264262
log.Panicf("cannot parse arguments: %s", err)
265263
}
266-
Systray.SetCurrentConfigFile(configPath)
264+
stray.SetCurrentConfigFile(configPath)
267265

268266
// Parse additional ini config if defined
269267
if len(*additionalConfig) > 0 {
@@ -283,18 +281,6 @@ func loop(stray *systray.Systray) {
283281
}
284282
}
285283

286-
if signatureKey == nil || len(*signatureKey) == 0 {
287-
log.Panicf("signature public key should be set")
288-
}
289-
signaturePubKey, err := utilities.ParseRsaPublicKey([]byte(*signatureKey))
290-
if err != nil {
291-
log.Panicf("cannot parse signature key '%s'. %s", *signatureKey, err)
292-
}
293-
294-
// Instantiate Index and Tools
295-
Index = index.Init(*indexURL, config.GetDataDir())
296-
Tools = tools.New(config.GetDataDir(), Index, logger, signaturePubKey)
297-
298284
// see if we are supposed to wait 5 seconds
299285
if *isLaunchSelf {
300286
launchSelfLater()
@@ -467,7 +453,7 @@ func loop(stray *systray.Systray) {
467453
r.LoadHTMLFiles("templates/nofirefox.html")
468454

469455
r.GET("/", homeHandler)
470-
r.POST("/upload", uploadHandler(hub, signaturePubKey))
456+
r.POST("/upload", uploadHandler(hub, signaturePubKey, tools))
471457
r.GET("/socket.io/", socketHandler)
472458
r.POST("/socket.io/", socketHandler)
473459
r.Handle("WS", "/socket.io/", socketHandler)
@@ -477,7 +463,7 @@ func loop(stray *systray.Systray) {
477463
r.POST("/update", updateHandler(stray))
478464

479465
// Mount goa handlers
480-
goa := v2.Server(config.GetDataDir().String(), Index, signaturePubKey)
466+
goa := v2.Server(config.GetDataDir().String(), index, signaturePubKey)
481467
r.Any("/v2/*path", gin.WrapH(goa))
482468

483469
go func() {

main_test.go

+25-9
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ import (
2929
"testing"
3030

3131
"github.com/arduino/arduino-create-agent/config"
32-
"github.com/arduino/arduino-create-agent/gen/tools"
32+
genTools "github.com/arduino/arduino-create-agent/gen/tools"
3333
"github.com/arduino/arduino-create-agent/globals"
3434
"github.com/arduino/arduino-create-agent/index"
35+
"github.com/arduino/arduino-create-agent/tools"
3536
"github.com/arduino/arduino-create-agent/upload"
3637
"github.com/arduino/arduino-create-agent/utilities"
3738
v2 "github.com/arduino/arduino-create-agent/v2"
@@ -56,7 +57,15 @@ func TestValidSignatureKey(t *testing.T) {
5657

5758
func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
5859
r := gin.New()
59-
r.POST("/", uploadHandler(newHub(newSerialHub(), newSerialPortList()), utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))))
60+
61+
index := index.Init(*indexURL, config.GetDataDir())
62+
signaturePubKey, err := utilities.ParseRsaPublicKey([]byte(*signatureKey))
63+
require.NoError(t, err)
64+
tools := tools.New(config.GetDataDir(), index, signaturePubKey)
65+
hub := newHub(newSerialHub(), newSerialPortList(tools), tools)
66+
pubkey := utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))
67+
68+
r.POST("/", uploadHandler(hub, pubkey, tools))
6069
ts := httptest.NewServer(r)
6170

6271
uploadEvilFileName := Upload{
@@ -93,7 +102,14 @@ func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
93102
func TestUploadHandlerAgainstBase64WithoutPaddingMustFail(t *testing.T) {
94103
r := gin.New()
95104

96-
r.POST("/", uploadHandler(newHub(newSerialHub(), newSerialPortList()), utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))))
105+
index := index.Init(*indexURL, config.GetDataDir())
106+
signaturePubKey, err := utilities.ParseRsaPublicKey([]byte(*signatureKey))
107+
require.NoError(t, err)
108+
tools := tools.New(config.GetDataDir(), index, signaturePubKey)
109+
hub := newHub(newSerialHub(), newSerialPortList(tools), tools)
110+
pubkey := utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))
111+
112+
r.POST("/", uploadHandler(hub, pubkey, tools))
97113
ts := httptest.NewServer(r)
98114
defer ts.Close()
99115

@@ -127,15 +143,15 @@ func TestInstallToolV2(t *testing.T) {
127143
ts := httptest.NewServer(r)
128144

129145
type test struct {
130-
request tools.ToolPayload
146+
request genTools.ToolPayload
131147
responseCode int
132148
responseBody string
133149
}
134150

135151
bossacURL := "http://downloads.arduino.cc/tools/bossac-1.7.0-arduino3-linux64.tar.gz"
136152
bossacChecksum := "SHA-256:1ae54999c1f97234a5c603eb99ad39313b11746a4ca517269a9285afa05f9100"
137153
bossacSignature := "382898a97b5a86edd74208f10107d2fecbf7059ffe9cc856e045266fb4db4e98802728a0859cfdcda1c0b9075ec01e42dbea1f430b813530d5a6ae1766dfbba64c3e689b59758062dc2ab2e32b2a3491dc2b9a80b9cda4ae514fbe0ec5af210111b6896976053ab76bac55bcecfcececa68adfa3299e3cde6b7f117b3552a7d80ca419374bb497e3c3f12b640cf5b20875416b45e662fc6150b99b178f8e41d6982b4c0a255925ea39773683f9aa9201dc5768b6fc857c87ff602b6a93452a541b8ec10ca07f166e61a9e9d91f0a6090bd2038ed4427af6251039fb9fe8eb62ec30d7b0f3df38bc9de7204dec478fb86f8eb3f71543710790ee169dce039d3e0"
138-
bossacInstallURLOK := tools.ToolPayload{
154+
bossacInstallURLOK := genTools.ToolPayload{
139155
Name: "bossac",
140156
Version: "1.7.0-arduino3",
141157
Packager: "arduino",
@@ -147,7 +163,7 @@ func TestInstallToolV2(t *testing.T) {
147163
esptoolURL := "https://github.com/earlephilhower/esp-quick-toolchain/releases/download/2.5.0-3/x86_64-linux-gnu.esptool-f80ae31.tar.gz"
148164
esptoolChecksum := "SHA-256:bded1dca953377838b6086a9bcd40a1dc5286ba5f69f2372c22a1d1819baad24"
149165
esptoolSignature := "852b58871419ce5e5633ecfaa72c0f0fa890ceb51164b362b8133bc0e3e003a21cec48935b8cdc078f4031219cbf17fb7edd9d7c9ca8ed85492911c9ca6353c9aa4691eb91fda99563a6bd49aeca0d9981fb05ec76e45c6024f8a6822862ad1e34ddc652fbbf4fa909887a255d4f087398ec386577efcec523c21203be3d10fc9e9b0f990a7536875a77dc2bc5cbffea7734b62238e31719111b718bacccebffc9be689545540e81d23b81caa66214376f58a0d6a45cf7efc5d3af62ab932b371628162fffe403906f41d5534921e5be081c5ac2ecc9db5caec03a105cc44b00ce19a95ad079843501eb8182e0717ce327867380c0e39d2b48698547fc1d0d66"
150-
esptoolInstallURLOK := tools.ToolPayload{
166+
esptoolInstallURLOK := genTools.ToolPayload{
151167
Name: "esptool",
152168
Version: "2.5.0-3-20ed2b9",
153169
Packager: "esp8266",
@@ -157,7 +173,7 @@ func TestInstallToolV2(t *testing.T) {
157173
}
158174

159175
wrongSignature := "wr0ngs1gn4tur3"
160-
bossacInstallWrongSig := tools.ToolPayload{
176+
bossacInstallWrongSig := genTools.ToolPayload{
161177
Name: "bossac",
162178
Version: "1.7.0-arduino3",
163179
Packager: "arduino",
@@ -167,7 +183,7 @@ func TestInstallToolV2(t *testing.T) {
167183
}
168184

169185
wrongChecksum := "wr0ngch3cksum"
170-
bossacInstallWrongCheck := tools.ToolPayload{
186+
bossacInstallWrongCheck := genTools.ToolPayload{
171187
Name: "bossac",
172188
Version: "1.7.0-arduino3",
173189
Packager: "arduino",
@@ -176,7 +192,7 @@ func TestInstallToolV2(t *testing.T) {
176192
Signature: &bossacSignature,
177193
}
178194

179-
bossacInstallNoURL := tools.ToolPayload{
195+
bossacInstallNoURL := genTools.ToolPayload{
180196
Name: "bossac",
181197
Version: "1.7.0-arduino3",
182198
Packager: "arduino",

serial.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"sync"
2626
"time"
2727

28+
"github.com/arduino/arduino-create-agent/tools"
2829
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
2930
"github.com/sirupsen/logrus"
3031
)
@@ -45,15 +46,17 @@ func newSerialHub() *serialhub {
4546
}
4647

4748
type serialPortList struct {
49+
tools *tools.Tools
50+
4851
Ports []*SpPortItem
4952
portsLock sync.Mutex
5053

5154
OnList func([]byte) `json:"-"`
5255
OnErr func(string) `json:"-"`
5356
}
5457

55-
func newSerialPortList() *serialPortList {
56-
return &serialPortList{}
58+
func newSerialPortList(tools *tools.Tools) *serialPortList {
59+
return &serialPortList{tools: tools}
5760
}
5861

5962
// SpPortItem is the serial port item
@@ -130,11 +133,12 @@ func (sp *serialPortList) Run() {
130133

131134
func (sp *serialPortList) runSerialDiscovery() {
132135
// First ensure that all the discoveries are available
133-
if err := Tools.Download("builtin", "serial-discovery", "latest", "keep"); err != nil {
136+
noOpProgress := func(msg string) {}
137+
if err := sp.tools.Download("builtin", "serial-discovery", "latest", "keep", noOpProgress); err != nil {
134138
logrus.Errorf("Error downloading serial-discovery: %s", err)
135139
panic(err)
136140
}
137-
sd, err := Tools.GetLocation("serial-discovery")
141+
sd, err := sp.tools.GetLocation("serial-discovery")
138142
if err != nil {
139143
logrus.Errorf("Error downloading serial-discovery: %s", err)
140144
panic(err)

tools/download.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import (
4242
// If version is not "latest" and behaviour is "replace", it will download the
4343
// version again. If instead behaviour is "keep" it will not download the version
4444
// if it already exists.
45-
func (t *Tools) Download(pack, name, version, behaviour string) error {
45+
func (t *Tools) Download(pack, name, version, behaviour string, report func(msg string)) error {
4646

4747
t.tools.SetBehaviour(behaviour)
4848
_, err := t.tools.Install(context.Background(), &tools.ToolPayload{Name: name, Version: version, Packager: pack})
@@ -58,24 +58,24 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
5858

5959
// if the tool contains a post_install script, run it: it means it is a tool that needs to install drivers
6060
// AFAIK this is only the case for the windows-driver tool
61-
err = t.installDrivers(safePath)
61+
err = t.installDrivers(safePath, report)
6262
if err != nil {
6363
return err
6464
}
6565

6666
// Ensure that the files are executable
67-
t.logger("Ensure that the files are executable")
67+
report("Ensure that the files are executable")
6868

6969
// Update the tool map
70-
t.logger("Updating map with location " + safePath)
70+
report("Updating map with location " + safePath)
7171

7272
t.setMapValue(name, safePath)
7373
t.setMapValue(name+"-"+version, safePath)
7474

7575
return nil
7676
}
7777

78-
func (t *Tools) installDrivers(location string) error {
78+
func (t *Tools) installDrivers(location string, report func(msg string)) error {
7979
OkPressed := 6
8080
extension := ".bat"
8181
// add .\ to force locality
@@ -86,11 +86,11 @@ func (t *Tools) installDrivers(location string) error {
8686
preamble = "./"
8787
}
8888
if _, err := os.Stat(filepath.Join(location, "post_install"+extension)); err == nil {
89-
t.logger("Installing drivers")
89+
report("Installing drivers")
9090
ok := MessageBox("Installing drivers", "We are about to install some drivers needed to use Arduino/Genuino boards\nDo you want to continue?")
9191
if ok == OkPressed {
9292
os.Chdir(location)
93-
t.logger(preamble + "post_install" + extension)
93+
report(preamble + "post_install" + extension)
9494
oscmd := exec.Command(preamble + "post_install" + extension)
9595
if runtime.GOOS != "linux" {
9696
// spawning a shell could be the only way to let the user type his password

0 commit comments

Comments
 (0)