Skip to content

Commit ec4d8d6

Browse files
mirkoCrobumirkoCrobualessio-perugini
authored
add sketch logs for API and CLI at start up time (#474)
Co-authored-by: mirkoCrobu <mirkocrobu@NB-0531.localdomain> Co-authored-by: Alessio Perugini <alessio@perugini.xyz>
1 parent d9e2d2f commit ec4d8d6

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

cmd/arduino-app-cli/app.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func startHandler(ctx context.Context, app app.ArduinoApp) error {
258258
for message := range stream {
259259
switch message.GetType() {
260260
case orchestrator.ProgressType:
261-
fmt.Fprintf(out, "Progress: %.0f%%\n", float64(message.GetProgress().Progress)*100)
261+
fmt.Fprintf(out, "Progress: %.0f%%\n", message.GetProgress().Progress)
262262
case orchestrator.InfoType:
263263
fmt.Fprintln(out, "[INFO]", message.GetData())
264264
case orchestrator.ErrorType:
@@ -283,7 +283,7 @@ func stopHandler(ctx context.Context, app app.ArduinoApp) error {
283283
for message := range orchestrator.StopApp(ctx, app) {
284284
switch message.GetType() {
285285
case orchestrator.ProgressType:
286-
fmt.Fprintf(out, "Progress: %.0f%%\n", float64(message.GetProgress().Progress)*100)
286+
fmt.Fprintf(out, "Progress: %.0f%%\n", message.GetProgress().Progress)
287287
case orchestrator.InfoType:
288288
fmt.Fprintln(out, "[INFO]", message.GetData())
289289
case orchestrator.ErrorType:

internal/orchestrator/orchestrator.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/bcmi-labs/orchestrator/internal/orchestrator/app"
2929
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricksindex"
3030
"github.com/bcmi-labs/orchestrator/internal/orchestrator/modelsindex"
31+
"github.com/bcmi-labs/orchestrator/pkg/helpers"
3132
"github.com/bcmi-labs/orchestrator/pkg/micro"
3233
"github.com/bcmi-labs/orchestrator/pkg/x/fatomic"
3334
)
@@ -122,6 +123,10 @@ func StartApp(ctx context.Context, docker *dockerClient.Client, provisioner *Pro
122123
})
123124

124125
if app.MainSketchPath != nil {
126+
if !yield(StreamMessage{data: "compiling and updating sketch..."}) {
127+
cancel()
128+
return
129+
}
125130
if err := compileUploadSketch(ctx, &app, callbackWriter); err != nil {
126131
yield(StreamMessage{error: err})
127132
return
@@ -879,8 +884,31 @@ func compileUploadSketch(ctx context.Context, arduinoApp *app.ArduinoApp, w io.W
879884

880885
if err := srv.Init(
881886
initReq,
882-
// TODO: implement progress callback function
883-
commands.InitStreamResponseToCallbackFunction(ctx, func(r *rpc.InitResponse) error { return nil }),
887+
commands.InitStreamResponseToCallbackFunction(ctx, func(r *rpc.InitResponse) error {
888+
var response string
889+
switch msg := r.GetMessage().(type) {
890+
case *rpc.InitResponse_InitProgress:
891+
if progress := msg.InitProgress.GetTaskProgress(); progress != nil {
892+
response = helpers.ArduinoCLITaskProgressToString(progress)
893+
}
894+
if progress := msg.InitProgress.GetDownloadProgress(); progress != nil {
895+
response = helpers.ArduinoCLIDownloadProgressToString(progress)
896+
}
897+
case *rpc.InitResponse_Error:
898+
response = "Error: " + msg.Error.String()
899+
case *rpc.InitResponse_Profile:
900+
response = fmt.Sprintf(
901+
"Sketch profile configured: FQBN=%q, Port=%q",
902+
msg.Profile.GetFqbn(),
903+
msg.Profile.GetPort(),
904+
)
905+
}
906+
if _, err := w.Write([]byte(response + "\n")); err != nil {
907+
return err
908+
}
909+
910+
return nil
911+
}),
884912
); err != nil {
885913
return err
886914
}

internal/update/arduino/arduino.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package arduino
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"log/slog"
87
"sync"
98
"time"
@@ -15,6 +14,7 @@ import (
1514

1615
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
1716
"github.com/bcmi-labs/orchestrator/internal/update"
17+
"github.com/bcmi-labs/orchestrator/pkg/helpers"
1818
)
1919

2020
type ArduinoPlatformUpdater struct {
@@ -111,12 +111,12 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
111111
eventsCh := make(chan update.Event, 100)
112112

113113
downloadProgressCB := func(curr *rpc.DownloadProgress) {
114-
data := arduinoCLIDownloadProgressToString(curr)
114+
data := helpers.ArduinoCLIDownloadProgressToString(curr)
115115
slog.Debug("Download progress", slog.String("download_progress", data))
116116
eventsCh <- update.Event{Type: update.UpgradeLineEvent, Data: data}
117117
}
118118
taskProgressCB := func(msg *rpc.TaskProgress) {
119-
data := arduinoCLITaskProgressToString(msg)
119+
data := helpers.ArduinoCLITaskProgressToString(msg)
120120
slog.Debug("Task progress", slog.String("task_progress", data))
121121
eventsCh <- update.Event{Type: update.UpgradeLineEvent, Data: data}
122122
}
@@ -262,28 +262,3 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
262262

263263
return eventsCh, nil
264264
}
265-
266-
func arduinoCLIDownloadProgressToString(progress *rpc.DownloadProgress) string {
267-
switch {
268-
case progress.GetStart() != nil:
269-
return fmt.Sprintf("Download started: %s", progress.GetStart().GetUrl())
270-
case progress.GetUpdate() != nil:
271-
return fmt.Sprintf("Download progress: %s", progress.GetUpdate())
272-
case progress.GetEnd() != nil:
273-
return fmt.Sprintf("Download completed: %s", progress.GetEnd())
274-
}
275-
return progress.String()
276-
}
277-
278-
func arduinoCLITaskProgressToString(progress *rpc.TaskProgress) string {
279-
data := fmt.Sprintf("Task %s:", progress.GetName())
280-
if progress.GetMessage() != "" {
281-
data += fmt.Sprintf(" (%s)", progress.GetMessage())
282-
}
283-
if progress.GetCompleted() {
284-
data += " completed"
285-
} else {
286-
data += fmt.Sprintf(" %.2f%%", progress.GetPercent())
287-
}
288-
return data
289-
}

pkg/helpers/helpers.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package helpers
2+
3+
import (
4+
"fmt"
5+
6+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
7+
)
8+
9+
func ArduinoCLIDownloadProgressToString(progress *rpc.DownloadProgress) string {
10+
switch {
11+
case progress.GetStart() != nil:
12+
return fmt.Sprintf("Download started: %s", progress.GetStart().GetUrl())
13+
case progress.GetUpdate() != nil:
14+
return fmt.Sprintf("Download progress: %s", progress.GetUpdate())
15+
case progress.GetEnd() != nil:
16+
return fmt.Sprintf("Download completed: %s", progress.GetEnd())
17+
}
18+
return progress.String()
19+
}
20+
21+
func ArduinoCLITaskProgressToString(progress *rpc.TaskProgress) string {
22+
data := fmt.Sprintf("Task %s:", progress.GetName())
23+
if progress.GetMessage() != "" {
24+
data += fmt.Sprintf(" (%s)", progress.GetMessage())
25+
}
26+
if progress.GetCompleted() {
27+
data += " completed"
28+
} else {
29+
data += fmt.Sprintf(" %.2f%%", progress.GetPercent())
30+
}
31+
return data
32+
}

0 commit comments

Comments
 (0)