Skip to content

Commit 1e15f3f

Browse files
committed
Send pending download messages to websocket
1 parent 26dd201 commit 1e15f3f

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

‎main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
"encoding/json"
78
"flag"
89
"os"
910
"os/user"
@@ -94,7 +95,11 @@ func main() {
9495
Tools = tools.Tools{
9596
Directory: directory,
9697
IndexURL: *indexURL,
97-
Logger: log.New(),
98+
Logger: func(msg string) {
99+
mapD := map[string]string{"DownloadStatus": "Pending", "Msg": msg}
100+
mapB, _ := json.Marshal(mapD)
101+
h.broadcastSys <- mapB
102+
},
98103
}
99104
Tools.Init(requiredToolsAPILevel)
100105

‎tools/download.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,11 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
178178
var data index
179179
json.Unmarshal(body, &data)
180180

181-
t.Logger.Println(string(body))
182-
183181
// Find the tool by name
184182
correctTool, correctSystem := findTool(pack, name, version, data)
185183

186184
if correctTool.Name == "" || correctSystem.URL == "" {
187-
t.Logger.Println("We couldn't find a tool with the name " + name + " and version " + version + " packaged by " + pack)
188-
return nil
185+
return errors.New("We couldn't find a tool with the name " + name + " and version " + version + " packaged by " + pack)
189186
}
190187

191188
key := correctTool.Name + "-" + correctTool.Version
@@ -196,13 +193,13 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
196193
if ok && pathExists(location) {
197194
// overwrite the default tool with this one
198195
t.installed[correctTool.Name] = location
199-
t.Logger.Println("The tool is already present on the system")
196+
t.Logger("The tool is already present on the system")
200197
return t.writeMap()
201198
}
202199
}
203200

204201
// Download the tool
205-
t.Logger.Println("Downloading tool " + name + " from " + correctSystem.URL)
202+
t.Logger("Downloading tool " + name + " from " + correctSystem.URL)
206203
resp, err := http.Get(correctSystem.URL)
207204
if err != nil {
208205
return err
@@ -224,7 +221,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
224221
}
225222

226223
// Decompress
227-
t.Logger.Println("Unpacking tool " + name)
224+
t.Logger("Unpacking tool " + name)
228225

229226
location := path.Join(dir(), pack, correctTool.Name, correctTool.Version)
230227
err = os.RemoveAll(location)
@@ -251,7 +248,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
251248
}
252249

253250
if err != nil {
254-
t.Logger.Println("Error extracting the archive: ", err.Error())
251+
t.Logger("Error extracting the archive: " + err.Error())
255252
return err
256253
}
257254

@@ -261,10 +258,10 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
261258
}
262259

263260
// Ensure that the files are executable
264-
t.Logger.Println("Ensure that the files are executable")
261+
t.Logger("Ensure that the files are executable")
265262

266263
// Update the tool map
267-
t.Logger.Println("Updating map with location " + location)
264+
t.Logger("Updating map with location " + location)
268265

269266
t.installed[name] = location
270267
t.installed[name+"-"+correctTool.Version] = location
@@ -455,19 +452,17 @@ func (t *Tools) installDrivers(location string) error {
455452
preamble = "./"
456453
}
457454
if _, err := os.Stat(filepath.Join(location, "post_install"+extension)); err == nil {
458-
t.Logger.Println("Installing drivers")
455+
t.Logger("Installing drivers")
459456
ok := MessageBox("Installing drivers", "We are about to install some drivers needed to use Arduino/Genuino boards\nDo you want to continue?")
460-
t.Logger.Println(ok)
461457
if ok == OK_PRESSED {
462458
os.Chdir(location)
459+
t.Logger(preamble + "post_install" + extension)
463460
oscmd := exec.Command(preamble + "post_install" + extension)
464461
if runtime.GOOS != "linux" {
465462
// spawning a shell could be the only way to let the user type his password
466463
TellCommandNotToSpawnShell(oscmd)
467464
}
468-
t.Logger.Println(oscmd)
469465
err = oscmd.Run()
470-
t.Logger.Println(err)
471466
return err
472467
} else {
473468
return errors.New("Could not install drivers")

‎tools/tools.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111
"time"
1212

13-
log "github.com/Sirupsen/logrus"
1413
"github.com/xrash/smetrics"
1514
)
1615

@@ -33,7 +32,7 @@ type Tools struct {
3332
Directory string
3433
IndexURL string
3534
LastRefresh time.Time
36-
Logger log.StdLogger
35+
Logger func(msg string)
3736
installed map[string]string
3837
}
3938

@@ -51,7 +50,6 @@ func (t *Tools) Init(APIlevel string) {
5150
t.writeMap()
5251
t.readMap()
5352
}
54-
t.Logger.Println(t.installed)
5553
}
5654

5755
// GetLocation extracts the toolname from a command like

0 commit comments

Comments
 (0)