Skip to content

Commit

Permalink
[BREAKING] Add protoc oneof clause where appropriate. (#2608)
Browse files Browse the repository at this point in the history
* Added oneof clause to all Platform* responses

* Added oneof clause to all Debug* and Library* responses

* Added oneof clause to MonitorResponse

* Updated Docs

* Fix typo
  • Loading branch information
cmaglie committed May 16, 2024
1 parent 63a48c5 commit b0cc1a9
Show file tree
Hide file tree
Showing 22 changed files with 2,399 additions and 869 deletions.
6 changes: 5 additions & 1 deletion commands/service_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ func (s *arduinoCoreServerImpl) Debug(stream rpc.ArduinoCoreService_DebugServer)
// Launch debug recipe attaching stdin and out to grpc streaming
signalChan := make(chan os.Signal)
defer close(signalChan)
outStream := feedStreamTo(func(data []byte) { stream.Send(&rpc.DebugResponse{Data: data}) })
outStream := feedStreamTo(func(data []byte) {
stream.Send(&rpc.DebugResponse{Message: &rpc.DebugResponse_Data{
Data: data,
}})
})
resp, debugErr := Debug(stream.Context(), req,
consumeStreamFrom(func() ([]byte, error) {
command, err := stream.Recv()
Expand Down
16 changes: 12 additions & 4 deletions commands/service_debug_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func Debug(ctx context.Context, req *rpc.GetDebugConfigRequest, inStream io.Read
// Get stdIn pipe from tool
in, err := cmd.StdinPipe()
if err != nil {
return &rpc.DebugResponse{Error: err.Error()}, nil
return &rpc.DebugResponse{Message: &rpc.DebugResponse_Result_{
Result: &rpc.DebugResponse_Result{Error: err.Error()},
}}, nil
}
defer in.Close()

Expand All @@ -81,7 +83,9 @@ func Debug(ctx context.Context, req *rpc.GetDebugConfigRequest, inStream io.Read

// Start the debug command
if err := cmd.Start(); err != nil {
return &rpc.DebugResponse{Error: err.Error()}, nil
return &rpc.DebugResponse{Message: &rpc.DebugResponse_Result_{
Result: &rpc.DebugResponse_Result{Error: err.Error()},
}}, nil
}

if interrupt != nil {
Expand All @@ -107,9 +111,13 @@ func Debug(ctx context.Context, req *rpc.GetDebugConfigRequest, inStream io.Read

// Wait for process to finish
if err := cmd.Wait(); err != nil {
return &rpc.DebugResponse{Error: err.Error()}, nil
return &rpc.DebugResponse{Message: &rpc.DebugResponse_Result_{
Result: &rpc.DebugResponse_Result{Error: err.Error()},
}}, nil
}
return &rpc.DebugResponse{}, nil
return &rpc.DebugResponse{Message: &rpc.DebugResponse_Result_{
Result: &rpc.DebugResponse_Result{},
}}, nil
}

// getCommandLine compose a debug command represented by a core recipe
Expand Down
12 changes: 10 additions & 2 deletions commands/service_library_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func LibraryDownloadStreamResponseToCallbackFunction(ctx context.Context, downlo
func (s *arduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadRequest, stream rpc.ArduinoCoreService_LibraryDownloadServer) error {
syncSend := NewSynchronizedSend(stream.Send)
ctx := stream.Context()
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryDownloadResponse{Progress: p}) }
downloadCB := func(p *rpc.DownloadProgress) {
syncSend.Send(&rpc.LibraryDownloadResponse{
Message: &rpc.LibraryDownloadResponse_Progress{Progress: p},
})
}

var downloadsDir *paths.Path
if pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance()); err != nil {
Expand Down Expand Up @@ -70,7 +74,11 @@ func (s *arduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadRequest,
return err
}

return syncSend.Send(&rpc.LibraryDownloadResponse{})
return syncSend.Send(&rpc.LibraryDownloadResponse{
Message: &rpc.LibraryDownloadResponse_Result_{
Result: &rpc.LibraryDownloadResponse_Result{},
},
})
}

func downloadLibrary(_ context.Context, downloadsDir *paths.Path, libRelease *librariesindex.Release,
Expand Down
39 changes: 35 additions & 4 deletions commands/service_library_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ func LibraryInstallStreamResponseToCallbackFunction(ctx context.Context, downloa
func (s *arduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, stream rpc.ArduinoCoreService_LibraryInstallServer) error {
ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryInstallResponse{Progress: p}) }
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryInstallResponse{TaskProgress: p}) }
downloadCB := func(p *rpc.DownloadProgress) {
syncSend.Send(&rpc.LibraryInstallResponse{
Message: &rpc.LibraryInstallResponse_Progress{Progress: p},
})
}
taskCB := func(p *rpc.TaskProgress) {
syncSend.Send(&rpc.LibraryInstallResponse{
Message: &rpc.LibraryInstallResponse_TaskProgress{TaskProgress: p},
})
}

// Obtain the library index from the manager
li, err := instances.GetLibrariesIndex(req.GetInstance())
Expand Down Expand Up @@ -162,6 +170,11 @@ func (s *arduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, s
return err
}

syncSend.Send(&rpc.LibraryInstallResponse{
Message: &rpc.LibraryInstallResponse_Result_{
Result: &rpc.LibraryInstallResponse_Result{},
},
})
return nil
}

Expand Down Expand Up @@ -202,7 +215,11 @@ func ZipLibraryInstallStreamResponseToCallbackFunction(ctx context.Context, task
func (s *arduinoCoreServerImpl) ZipLibraryInstall(req *rpc.ZipLibraryInstallRequest, stream rpc.ArduinoCoreService_ZipLibraryInstallServer) error {
ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.ZipLibraryInstallResponse{TaskProgress: p}) }
taskCB := func(p *rpc.TaskProgress) {
syncSend.Send(&rpc.ZipLibraryInstallResponse{
Message: &rpc.ZipLibraryInstallResponse_TaskProgress{TaskProgress: p},
})
}

lm, err := instances.GetLibraryManager(req.GetInstance())
if err != nil {
Expand All @@ -214,6 +231,11 @@ func (s *arduinoCoreServerImpl) ZipLibraryInstall(req *rpc.ZipLibraryInstallRequ
return &cmderrors.FailedLibraryInstallError{Cause: err}
}
taskCB(&rpc.TaskProgress{Message: tr("Library installed"), Completed: true})
syncSend.Send(&rpc.ZipLibraryInstallResponse{
Message: &rpc.ZipLibraryInstallResponse_Result_{
Result: &rpc.ZipLibraryInstallResponse_Result{},
},
})
return nil
}

Expand All @@ -231,7 +253,11 @@ func GitLibraryInstallStreamResponseToCallbackFunction(ctx context.Context, task
// GitLibraryInstall FIXMEDOC
func (s *arduinoCoreServerImpl) GitLibraryInstall(req *rpc.GitLibraryInstallRequest, stream rpc.ArduinoCoreService_GitLibraryInstallServer) error {
syncSend := NewSynchronizedSend(stream.Send)
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.GitLibraryInstallResponse{TaskProgress: p}) }
taskCB := func(p *rpc.TaskProgress) {
syncSend.Send(&rpc.GitLibraryInstallResponse{
Message: &rpc.GitLibraryInstallResponse_TaskProgress{TaskProgress: p},
})
}
lm, err := instances.GetLibraryManager(req.GetInstance())
if err != nil {
return err
Expand All @@ -245,5 +271,10 @@ func (s *arduinoCoreServerImpl) GitLibraryInstall(req *rpc.GitLibraryInstallRequ
return &cmderrors.FailedLibraryInstallError{Cause: err}
}
taskCB(&rpc.TaskProgress{Message: tr("Library installed"), Completed: true})
syncSend.Send(&rpc.GitLibraryInstallResponse{
Message: &rpc.GitLibraryInstallResponse_Result_{
Result: &rpc.GitLibraryInstallResponse_Result{},
},
})
return nil
}
14 changes: 11 additions & 3 deletions commands/service_library_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ func LibraryUninstallStreamResponseToCallbackFunction(ctx context.Context, taskC

// LibraryUninstall uninstalls a library
func (s *arduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallRequest, stream rpc.ArduinoCoreService_LibraryUninstallServer) error {
// ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUninstallResponse{TaskProgress: p}) }
taskCB := func(p *rpc.TaskProgress) {
syncSend.Send(&rpc.LibraryUninstallResponse{
Message: &rpc.LibraryUninstallResponse_TaskProgress{TaskProgress: p},
})
}

lm, err := instances.GetLibraryManager(req.GetInstance())
if err != nil {
Expand All @@ -57,14 +60,19 @@ func (s *arduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallReques
libs := lmi.FindByReference(req.GetName(), version, libraries.User)
if len(libs) == 0 {
taskCB(&rpc.TaskProgress{Message: tr("Library %s is not installed", req.GetName()), Completed: true})
syncSend.Send(&rpc.LibraryUninstallResponse{
Message: &rpc.LibraryUninstallResponse_Result_{Result: &rpc.LibraryUninstallResponse_Result{}},
})
return nil
}

if len(libs) == 1 {
taskCB(&rpc.TaskProgress{Name: tr("Uninstalling %s", libs)})
// TODO: pass context
lmi.Uninstall(libs[0])
taskCB(&rpc.TaskProgress{Completed: true})
syncSend.Send(&rpc.LibraryUninstallResponse{
Message: &rpc.LibraryUninstallResponse_Result_{Result: &rpc.LibraryUninstallResponse_Result{}},
})
return nil
}

Expand Down
44 changes: 37 additions & 7 deletions commands/service_library_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@ func LibraryUpgradeAllStreamResponseToCallbackFunction(ctx context.Context, down
func (s *arduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, stream rpc.ArduinoCoreService_LibraryUpgradeAllServer) error {
ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryUpgradeAllResponse{Progress: p}) }
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUpgradeAllResponse{TaskProgress: p}) }
downloadCB := func(p *rpc.DownloadProgress) {
syncSend.Send(&rpc.LibraryUpgradeAllResponse{
Message: &rpc.LibraryUpgradeAllResponse_Progress{Progress: p},
})
}
taskCB := func(p *rpc.TaskProgress) {
syncSend.Send(&rpc.LibraryUpgradeAllResponse{
Message: &rpc.LibraryUpgradeAllResponse_TaskProgress{TaskProgress: p},
})
}

li, err := instances.GetLibrariesIndex(req.GetInstance())
if err != nil {
Expand All @@ -67,6 +75,11 @@ func (s *arduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequ
return err
}

syncSend.Send(&rpc.LibraryUpgradeAllResponse{
Message: &rpc.LibraryUpgradeAllResponse_Result_{
Result: &rpc.LibraryUpgradeAllResponse_Result{},
},
})
return nil
}

Expand All @@ -88,8 +101,16 @@ func LibraryUpgradeStreamResponseToCallbackFunction(ctx context.Context, downloa
func (s *arduinoCoreServerImpl) LibraryUpgrade(req *rpc.LibraryUpgradeRequest, stream rpc.ArduinoCoreService_LibraryUpgradeServer) error {
ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryUpgradeResponse{Progress: p}) }
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUpgradeResponse{TaskProgress: p}) }
downloadCB := func(p *rpc.DownloadProgress) {
syncSend.Send(&rpc.LibraryUpgradeResponse{
Message: &rpc.LibraryUpgradeResponse_Progress{Progress: p},
})
}
taskCB := func(p *rpc.TaskProgress) {
syncSend.Send(&rpc.LibraryUpgradeResponse{
Message: &rpc.LibraryUpgradeResponse_TaskProgress{TaskProgress: p},
})
}

li, err := instances.GetLibrariesIndex(req.GetInstance())
if err != nil {
Expand All @@ -111,12 +132,21 @@ func (s *arduinoCoreServerImpl) LibraryUpgrade(req *rpc.LibraryUpgradeRequest, s
return &cmderrors.LibraryNotFoundError{Library: name}
}
if lib.Available == nil {
// library already at the latest version
taskCB(&rpc.TaskProgress{Message: tr("Library %s is already at the latest version", name), Completed: true})
return nil
} else {
// Install update
if err := s.libraryUpgrade(ctx, req.GetInstance(), []*installedLib{lib}, downloadCB, taskCB); err != nil {
return err
}
}

// Install update
return s.libraryUpgrade(ctx, req.GetInstance(), []*installedLib{lib}, downloadCB, taskCB)
syncSend.Send(&rpc.LibraryUpgradeResponse{
Message: &rpc.LibraryUpgradeResponse_Result_{
Result: &rpc.LibraryUpgradeResponse_Result{},
},
})
return nil
}

func (s *arduinoCoreServerImpl) libraryUpgrade(ctx context.Context, instance *rpc.Instance, libs []*installedLib, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
Expand Down
12 changes: 6 additions & 6 deletions commands/service_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (s *arduinoCoreServerImpl) Monitor(stream rpc.ArduinoCoreService_MonitorSer

// Send a message with Success set to true to notify the caller of the port being now active
syncSend := NewSynchronizedSend(stream.Send)
_ = syncSend.Send(&rpc.MonitorResponse{Success: true})
_ = syncSend.Send(&rpc.MonitorResponse{Message: &rpc.MonitorResponse_Success{Success: true}})

ctx, cancel := context.WithCancel(stream.Context())
gracefulCloseInitiated := &atomic.Bool{}
Expand All @@ -175,13 +175,13 @@ func (s *arduinoCoreServerImpl) Monitor(stream rpc.ArduinoCoreService_MonitorSer
return
}
if err != nil {
syncSend.Send(&rpc.MonitorResponse{Error: err.Error()})
syncSend.Send(&rpc.MonitorResponse{Message: &rpc.MonitorResponse_Error{Error: err.Error()}})
return
}
if conf := msg.GetUpdatedConfiguration(); conf != nil {
for _, c := range conf.GetSettings() {
if err := monitor.Configure(c.GetSettingId(), c.GetValue()); err != nil {
syncSend.Send(&rpc.MonitorResponse{Error: err.Error()})
syncSend.Send(&rpc.MonitorResponse{Message: &rpc.MonitorResponse_Error{Error: err.Error()}})
}
}
}
Expand All @@ -199,7 +199,7 @@ func (s *arduinoCoreServerImpl) Monitor(stream rpc.ArduinoCoreService_MonitorSer
return
}
if err != nil {
syncSend.Send(&rpc.MonitorResponse{Error: err.Error()})
syncSend.Send(&rpc.MonitorResponse{Message: &rpc.MonitorResponse_Error{Error: err.Error()}})
return
}
tx = tx[n:]
Expand All @@ -217,10 +217,10 @@ func (s *arduinoCoreServerImpl) Monitor(stream rpc.ArduinoCoreService_MonitorSer
break
}
if err != nil {
syncSend.Send(&rpc.MonitorResponse{Error: err.Error()})
syncSend.Send(&rpc.MonitorResponse{Message: &rpc.MonitorResponse_Error{Error: err.Error()}})
break
}
if err := syncSend.Send(&rpc.MonitorResponse{RxData: buff[:n]}); err != nil {
if err := syncSend.Send(&rpc.MonitorResponse{Message: &rpc.MonitorResponse_RxData{RxData: buff[:n]}}); err != nil {
break
}
}
Expand Down
12 changes: 10 additions & 2 deletions commands/service_platform_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ func (s *arduinoCoreServerImpl) PlatformDownload(req *rpc.PlatformDownloadReques
return &cmderrors.PlatformNotFoundError{Platform: ref.String(), Cause: err}
}

downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformDownloadResponse{Progress: p}) }
downloadCB := func(p *rpc.DownloadProgress) {
syncSend.Send(&rpc.PlatformDownloadResponse{
Message: &rpc.PlatformDownloadResponse_Progress{
Progress: p,
},
})
}

// TODO: pass context
// ctx := stream.Context()
Expand All @@ -78,5 +84,7 @@ func (s *arduinoCoreServerImpl) PlatformDownload(req *rpc.PlatformDownloadReques
}
}

return syncSend.Send(&rpc.PlatformDownloadResponse{})
return syncSend.Send(&rpc.PlatformDownloadResponse{Message: &rpc.PlatformDownloadResponse_Result_{
Result: &rpc.PlatformDownloadResponse_Result{},
}})
}
22 changes: 19 additions & 3 deletions commands/service_platform_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,20 @@ func PlatformInstallStreamResponseToCallbackFunction(ctx context.Context, downlo
func (s *arduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallRequest, stream rpc.ArduinoCoreService_PlatformInstallServer) error {
ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformInstallResponse{TaskProgress: p}) }
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformInstallResponse{Progress: p}) }
taskCB := func(p *rpc.TaskProgress) {
syncSend.Send(&rpc.PlatformInstallResponse{
Message: &rpc.PlatformInstallResponse_TaskProgress{
TaskProgress: p,
},
})
}
downloadCB := func(p *rpc.DownloadProgress) {
syncSend.Send(&rpc.PlatformInstallResponse{
Message: &rpc.PlatformInstallResponse_Progress{
Progress: p,
},
})
}

install := func() error {
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
Expand Down Expand Up @@ -102,5 +114,9 @@ func (s *arduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallRequest,
return err
}

return syncSend.Send(&rpc.PlatformInstallResponse{})
return syncSend.Send(&rpc.PlatformInstallResponse{
Message: &rpc.PlatformInstallResponse_Result_{
Result: &rpc.PlatformInstallResponse_Result{},
},
})
}
14 changes: 12 additions & 2 deletions commands/service_platform_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,24 @@ func PlatformUninstallStreamResponseToCallbackFunction(ctx context.Context, task
func (s *arduinoCoreServerImpl) PlatformUninstall(req *rpc.PlatformUninstallRequest, stream rpc.ArduinoCoreService_PlatformUninstallServer) error {
syncSend := NewSynchronizedSend(stream.Send)
ctx := stream.Context()
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformUninstallResponse{TaskProgress: p}) }
taskCB := func(p *rpc.TaskProgress) {
syncSend.Send(&rpc.PlatformUninstallResponse{
Message: &rpc.PlatformUninstallResponse_TaskProgress{
TaskProgress: p,
},
})
}
if err := platformUninstall(ctx, req, taskCB); err != nil {
return err
}
if err := s.Init(&rpc.InitRequest{Instance: req.GetInstance()}, InitStreamResponseToCallbackFunction(ctx, nil)); err != nil {
return err
}
return syncSend.Send(&rpc.PlatformUninstallResponse{})
return syncSend.Send(&rpc.PlatformUninstallResponse{
Message: &rpc.PlatformUninstallResponse_Result_{
Result: &rpc.PlatformUninstallResponse_Result{},
},
})
}

// platformUninstall is the implementation of platform unistaller
Expand Down
Loading

0 comments on commit b0cc1a9

Please sign in to comment.