Skip to content

Commit a81069d

Browse files
committed
Inlining methods in ArduinoCoreServiceImpl (part 6: Init)
Added helpers to get init progress.
1 parent 8bd0eef commit a81069d

19 files changed

+103
-53
lines changed

commands/grpc_streaming_helpers.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,45 @@ func (w *streamingResponseProxyToChan[T]) SetHeader(metadata.MD) error {
7676

7777
func (w *streamingResponseProxyToChan[T]) SetTrailer(tr metadata.MD) {
7878
}
79+
80+
// streamingResponseProxyToCallback is a streaming response proxy that
81+
// forwards the responses to a callback function
82+
type streamingResponseProxyToCallback[T any] struct {
83+
ctx context.Context
84+
cb func(*T) error
85+
}
86+
87+
// creates a streaming response proxy that forwards the responses to a callback function
88+
func streamResponseToCallback[T any](ctx context.Context, cb func(*T) error) *streamingResponseProxyToCallback[T] {
89+
if cb == nil {
90+
cb = func(*T) error { return nil }
91+
}
92+
return &streamingResponseProxyToCallback[T]{ctx: ctx, cb: cb}
93+
}
94+
95+
func (w *streamingResponseProxyToCallback[T]) Send(resp *T) error {
96+
return w.cb(resp)
97+
}
98+
99+
func (w *streamingResponseProxyToCallback[T]) Context() context.Context {
100+
return w.ctx
101+
}
102+
103+
func (w *streamingResponseProxyToCallback[T]) RecvMsg(m any) error {
104+
return errors.New("RecvMsg not implemented")
105+
}
106+
107+
func (w *streamingResponseProxyToCallback[T]) SendHeader(metadata.MD) error {
108+
return errors.New("SendHeader not implemented")
109+
}
110+
111+
func (w *streamingResponseProxyToCallback[T]) SendMsg(m any) error {
112+
return errors.New("SendMsg not implemented")
113+
}
114+
115+
func (w *streamingResponseProxyToCallback[T]) SetHeader(metadata.MD) error {
116+
return errors.New("SetHeader not implemented")
117+
}
118+
119+
func (w *streamingResponseProxyToCallback[T]) SetTrailer(tr metadata.MD) {
120+
}

commands/instances.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,30 @@ func (s *arduinoCoreServerImpl) Create(ctx context.Context, req *rpc.CreateReque
9696
return &rpc.CreateResponse{Instance: inst}, nil
9797
}
9898

99+
// InitStreamResponseToCallbackFunction returns a gRPC stream to be used in Init that sends
100+
// all responses to the callback function.
101+
func InitStreamResponseToCallbackFunction(ctx context.Context, cb func(r *rpc.InitResponse) error) rpc.ArduinoCoreService_InitServer {
102+
return streamResponseToCallback(ctx, cb)
103+
}
104+
99105
// Init loads installed libraries and Platforms in CoreInstance with specified ID,
100106
// a gRPC status error is returned if the CoreInstance doesn't exist.
101107
// All responses are sent through responseCallback, can be nil to ignore all responses.
102108
// Failures don't stop the loading process, in case of loading failure the Platform or library
103109
// is simply skipped and an error gRPC status is sent to responseCallback.
104-
func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) error {
105-
if responseCallback == nil {
106-
responseCallback = func(r *rpc.InitResponse) {}
107-
}
110+
func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCoreService_InitServer) error {
108111
instance := req.GetInstance()
109112
if !instances.IsValid(instance) {
110113
return &cmderrors.InvalidInstanceError{}
111114
}
112115

113116
// Setup callback functions
114-
if responseCallback == nil {
115-
responseCallback = func(r *rpc.InitResponse) {}
117+
var responseCallback func(*rpc.InitResponse) error
118+
if stream != nil {
119+
syncSend := NewSynchronizedSend(stream.Send)
120+
responseCallback = syncSend.Send
121+
} else {
122+
responseCallback = func(*rpc.InitResponse) error { return nil }
116123
}
117124
responseError := func(st *status.Status) {
118125
responseCallback(&rpc.InitResponse{

commands/service.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesInd
7878
return err
7979
}
8080

81-
// Init FIXMEDOC
82-
func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCoreService_InitServer) error {
83-
syncSend := NewSynchronizedSend(stream.Send)
84-
return Init(req, func(message *rpc.InitResponse) { syncSend.Send(message) })
85-
}
86-
8781
// Version FIXMEDOC
8882
func (s *arduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionRequest) (*rpc.VersionResponse, error) {
8983
return &rpc.VersionResponse{Version: s.versionString}, nil
@@ -144,7 +138,7 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
144138
func (s *arduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallRequest, stream rpc.ArduinoCoreService_PlatformInstallServer) error {
145139
syncSend := NewSynchronizedSend(stream.Send)
146140
resp, err := PlatformInstall(
147-
stream.Context(), req,
141+
stream.Context(), s, req,
148142
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformInstallResponse{Progress: p}) },
149143
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformInstallResponse{TaskProgress: p}) },
150144
)
@@ -171,7 +165,7 @@ func (s *arduinoCoreServerImpl) PlatformDownload(req *rpc.PlatformDownloadReques
171165
func (s *arduinoCoreServerImpl) PlatformUninstall(req *rpc.PlatformUninstallRequest, stream rpc.ArduinoCoreService_PlatformUninstallServer) error {
172166
syncSend := NewSynchronizedSend(stream.Send)
173167
resp, err := PlatformUninstall(
174-
stream.Context(), req,
168+
stream.Context(), s, req,
175169
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformUninstallResponse{TaskProgress: p}) },
176170
)
177171
if err != nil {
@@ -184,7 +178,7 @@ func (s *arduinoCoreServerImpl) PlatformUninstall(req *rpc.PlatformUninstallRequ
184178
func (s *arduinoCoreServerImpl) PlatformUpgrade(req *rpc.PlatformUpgradeRequest, stream rpc.ArduinoCoreService_PlatformUpgradeServer) error {
185179
syncSend := NewSynchronizedSend(stream.Send)
186180
resp, err := PlatformUpgrade(
187-
stream.Context(), req,
181+
stream.Context(), s, req,
188182
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformUpgradeResponse{Progress: p}) },
189183
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformUpgradeResponse{TaskProgress: p}) },
190184
)
@@ -304,7 +298,7 @@ func (s *arduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadRequest,
304298
func (s *arduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, stream rpc.ArduinoCoreService_LibraryInstallServer) error {
305299
syncSend := NewSynchronizedSend(stream.Send)
306300
return LibraryInstall(
307-
stream.Context(), req,
301+
stream.Context(), s, req,
308302
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryInstallResponse{Progress: p}) },
309303
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryInstallResponse{TaskProgress: p}) },
310304
)
@@ -314,7 +308,7 @@ func (s *arduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, s
314308
func (s *arduinoCoreServerImpl) LibraryUpgrade(req *rpc.LibraryUpgradeRequest, stream rpc.ArduinoCoreService_LibraryUpgradeServer) error {
315309
syncSend := NewSynchronizedSend(stream.Send)
316310
return LibraryUpgrade(
317-
stream.Context(), req,
311+
stream.Context(), s, req,
318312
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryUpgradeResponse{Progress: p}) },
319313
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUpgradeResponse{TaskProgress: p}) },
320314
)
@@ -331,7 +325,7 @@ func (s *arduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallReques
331325
// LibraryUpgradeAll FIXMEDOC
332326
func (s *arduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, stream rpc.ArduinoCoreService_LibraryUpgradeAllServer) error {
333327
syncSend := NewSynchronizedSend(stream.Send)
334-
return LibraryUpgradeAll(req,
328+
return LibraryUpgradeAll(s, req,
335329
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryUpgradeAllResponse{Progress: p}) },
336330
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUpgradeAllResponse{TaskProgress: p}) },
337331
)

commands/service_library_install.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
)
3232

3333
// LibraryInstall resolves the library dependencies, then downloads and installs the libraries into the install location.
34-
func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
34+
func LibraryInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.LibraryInstallRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
3535
// Obtain the library index from the manager
3636
li, err := instances.GetLibrariesIndex(req.GetInstance())
3737
if err != nil {
@@ -136,7 +136,8 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
136136
}
137137
}
138138

139-
if err := Init(&rpc.InitRequest{Instance: req.GetInstance()}, nil); err != nil {
139+
stream := InitStreamResponseToCallbackFunction(ctx, nil)
140+
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, stream); err != nil {
140141
return err
141142
}
142143

commands/service_library_upgrade.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
)
2525

2626
// LibraryUpgradeAll upgrades all the available libraries
27-
func LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
27+
func LibraryUpgradeAll(srv rpc.ArduinoCoreServiceServer, req *rpc.LibraryUpgradeAllRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
28+
ctx := context.Background()
29+
2830
li, err := instances.GetLibrariesIndex(req.GetInstance())
2931
if err != nil {
3032
return err
@@ -37,19 +39,20 @@ func LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, downloadCB rpc.Downloa
3739
libsToUpgrade := listLibraries(lme, li, true, false)
3840
release()
3941

40-
if err := upgrade(req.GetInstance(), libsToUpgrade, downloadCB, taskCB); err != nil {
42+
if err := upgrade(ctx, srv, req.GetInstance(), libsToUpgrade, downloadCB, taskCB); err != nil {
4143
return err
4244
}
4345

44-
if err := Init(&rpc.InitRequest{Instance: req.GetInstance()}, nil); err != nil {
46+
stream := InitStreamResponseToCallbackFunction(ctx, nil)
47+
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, stream); err != nil {
4548
return err
4649
}
4750

4851
return nil
4952
}
5053

5154
// LibraryUpgrade upgrades a library
52-
func LibraryUpgrade(ctx context.Context, req *rpc.LibraryUpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
55+
func LibraryUpgrade(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.LibraryUpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
5356
li, err := instances.GetLibrariesIndex(req.GetInstance())
5457
if err != nil {
5558
return err
@@ -75,10 +78,10 @@ func LibraryUpgrade(ctx context.Context, req *rpc.LibraryUpgradeRequest, downloa
7578
}
7679

7780
// Install update
78-
return upgrade(req.GetInstance(), []*installedLib{lib}, downloadCB, taskCB)
81+
return upgrade(ctx, srv, req.GetInstance(), []*installedLib{lib}, downloadCB, taskCB)
7982
}
8083

81-
func upgrade(instance *rpc.Instance, libs []*installedLib, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
84+
func upgrade(ctx context.Context, srv rpc.ArduinoCoreServiceServer, instance *rpc.Instance, libs []*installedLib, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
8285
for _, lib := range libs {
8386
libInstallReq := &rpc.LibraryInstallRequest{
8487
Instance: instance,
@@ -87,7 +90,7 @@ func upgrade(instance *rpc.Instance, libs []*installedLib, downloadCB rpc.Downlo
8790
NoDeps: false,
8891
NoOverwrite: false,
8992
}
90-
err := LibraryInstall(context.Background(), libInstallReq, downloadCB, taskCB)
93+
err := LibraryInstall(ctx, srv, libInstallReq, downloadCB, taskCB)
9194
if err != nil {
9295
return err
9396
}

commands/service_platform_install.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
// PlatformInstall FIXMEDOC
29-
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (*rpc.PlatformInstallResponse, error) {
29+
func PlatformInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.PlatformInstallRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (*rpc.PlatformInstallResponse, error) {
3030
install := func() error {
3131
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
3232
if err != nil {
@@ -73,7 +73,9 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest, downl
7373
if err := install(); err != nil {
7474
return nil, err
7575
}
76-
if err := Init(&rpc.InitRequest{Instance: req.GetInstance()}, nil); err != nil {
76+
77+
stream := InitStreamResponseToCallbackFunction(ctx, nil)
78+
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, stream); err != nil {
7779
return nil, err
7880
}
7981
return &rpc.PlatformInstallResponse{}, nil

commands/service_platform_search_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestPlatformSearch(t *testing.T) {
4646
inst := createResp.GetInstance()
4747
require.NotNil(t, inst)
4848

49-
err = Init(&rpc.InitRequest{Instance: inst}, nil)
49+
err = srv.Init(&rpc.InitRequest{Instance: inst}, InitStreamResponseToCallbackFunction(ctx, nil))
5050
require.NoError(t, err)
5151

5252
t.Run("SearchAllVersions", func(t *testing.T) {
@@ -347,7 +347,7 @@ func TestPlatformSearchSorting(t *testing.T) {
347347
require.NoError(t, err)
348348
inst := createResp.GetInstance()
349349
require.NotNil(t, inst)
350-
err = Init(&rpc.InitRequest{Instance: inst}, nil)
350+
err = srv.Init(&rpc.InitRequest{Instance: inst}, InitStreamResponseToCallbackFunction(ctx, nil))
351351
require.NoError(t, err)
352352

353353
res, stat := PlatformSearch(&rpc.PlatformSearchRequest{

commands/service_platform_uninstall.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import (
2525
)
2626

2727
// PlatformUninstall FIXMEDOC
28-
func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallRequest, taskCB rpc.TaskProgressCB) (*rpc.PlatformUninstallResponse, error) {
28+
func PlatformUninstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.PlatformUninstallRequest, taskCB rpc.TaskProgressCB) (*rpc.PlatformUninstallResponse, error) {
2929
if err := platformUninstall(req, taskCB); err != nil {
3030
return nil, err
3131
}
32-
if err := Init(&rpc.InitRequest{Instance: req.GetInstance()}, nil); err != nil {
32+
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, InitStreamResponseToCallbackFunction(ctx, nil)); err != nil {
3333
return nil, err
3434
}
3535
return &rpc.PlatformUninstallResponse{}, nil

commands/service_platform_upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
// PlatformUpgrade FIXMEDOC
28-
func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (*rpc.PlatformUpgradeResponse, error) {
28+
func PlatformUpgrade(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.PlatformUpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (*rpc.PlatformUpgradeResponse, error) {
2929
upgrade := func() (*cores.PlatformRelease, error) {
3030
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
3131
if err != nil {
@@ -57,7 +57,7 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeRequest, downl
5757
if err != nil {
5858
return &rpc.PlatformUpgradeResponse{Platform: rpcPlatform}, err
5959
}
60-
if err := Init(&rpc.InitRequest{Instance: req.GetInstance()}, nil); err != nil {
60+
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, InitStreamResponseToCallbackFunction(ctx, nil)); err != nil {
6161
return nil, err
6262
}
6363

internal/cli/core/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func runInstallCommand(srv rpc.ArduinoCoreServiceServer, args []string, scriptFl
7676
NoOverwrite: noOverwrite,
7777
SkipPreUninstall: scriptFlags.DetectSkipPreUninstallValue(),
7878
}
79-
_, err := commands.PlatformInstall(context.Background(), platformInstallRequest, feedback.ProgressBar(), feedback.TaskProgress())
79+
_, err := commands.PlatformInstall(ctx, srv, platformInstallRequest, feedback.ProgressBar(), feedback.TaskProgress())
8080
if err != nil {
8181
feedback.Fatal(tr("Error during install: %v", err), feedback.ErrGeneric)
8282
}

0 commit comments

Comments
 (0)