Skip to content

Commit eab7ad5

Browse files
committed
Added GRPC UpdateLibrariesIndex command
1 parent 1a1f21f commit eab7ad5

File tree

7 files changed

+286
-83
lines changed

7 files changed

+286
-83
lines changed

cli/cli.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ func InitInstance(libManagerOnly bool) *rpc.InitResp {
101101
os.Exit(ErrGeneric)
102102
}
103103
if resp.GetLibrariesIndexError() != "" {
104-
commands.UpdateLibrariesIndex(context.Background(), commands.GetLibraryManager(resp), OutputProgressBar())
104+
commands.UpdateLibrariesIndex(context.Background(),
105+
&rpc.UpdateLibrariesIndexReq{Instance: resp.GetInstance()}, OutputProgressBar())
105106
rescResp, err := commands.Rescan(context.Background(), &rpc.RescanReq{Instance: resp.GetInstance()})
106107
if rescResp.GetLibrariesIndexError() != "" {
107108
formatter.PrintErrorMessage("Error loading library index: " + rescResp.GetLibrariesIndexError())

cli/lib/update_index.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/arduino/arduino-cli/cli"
2525
"github.com/arduino/arduino-cli/commands"
2626
"github.com/arduino/arduino-cli/common/formatter"
27+
"github.com/arduino/arduino-cli/rpc"
2728
"github.com/spf13/cobra"
2829
)
2930

@@ -35,8 +36,10 @@ func initUpdateIndexCommand() *cobra.Command {
3536
Example: " " + cli.AppName + " lib update-index",
3637
Args: cobra.NoArgs,
3738
Run: func(cmd *cobra.Command, args []string) {
38-
lm := cli.InitLibraryManager(cli.Config)
39-
err := commands.UpdateLibrariesIndex(context.Background(), lm, cli.OutputProgressBar())
39+
instance := cli.CreateInstance()
40+
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexReq{
41+
Instance: instance,
42+
}, cli.OutputProgressBar())
4043
if err != nil {
4144
formatter.PrintError(err, "Error updating library index")
4245
os.Exit(cli.ErrGeneric)

commands/instances.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,12 @@ func Destroy(ctx context.Context, req *rpc.DestroyReq) (*rpc.DestroyResp, error)
126126
}
127127

128128
// UpdateLibrariesIndex updates the library_index.json
129-
func UpdateLibrariesIndex(ctx context.Context, lm *librariesmanager.LibrariesManager, downloadCB func(*rpc.DownloadProgress)) error {
129+
func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexReq, downloadCB func(*rpc.DownloadProgress)) error {
130130
logrus.Info("Updating libraries index")
131+
lm := GetLibraryManager(req)
132+
if lm == nil {
133+
return fmt.Errorf("invalid handle")
134+
}
131135
d, err := lm.UpdateIndex()
132136
if err != nil {
133137
return err
@@ -136,6 +140,9 @@ func UpdateLibrariesIndex(ctx context.Context, lm *librariesmanager.LibrariesMan
136140
if d.Error() != nil {
137141
return d.Error()
138142
}
143+
if _, err := Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
144+
return fmt.Errorf("rescanning filesystem: %s", err)
145+
}
139146
return nil
140147
}
141148

@@ -182,7 +189,9 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB Downlo
182189
return nil, fmt.Errorf("saving downloaded index %s: %s", URL, err)
183190
}
184191
}
185-
Rescan(ctx, &rpc.RescanReq{Instance: req.Instance})
192+
if _, err := Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil {
193+
return nil, fmt.Errorf("rescanning filesystem: %s", err)
194+
}
186195
return &rpc.UpdateIndexResp{}, nil
187196
}
188197

daemon/client/client.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,29 @@ func main() {
292292
}
293293
}
294294

295+
// LIB UPDATE INDEX
296+
fmt.Println("=== calling UpdateLibrariesIndex()")
297+
libIdxUpdateStream, err := client.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexReq{Instance: instance})
298+
if err != nil {
299+
fmt.Printf("Error updating libraries index: %s\n", err)
300+
os.Exit(1)
301+
}
302+
for {
303+
resp, err := libIdxUpdateStream.Recv()
304+
if err == io.EOF {
305+
fmt.Printf("---> %+v\n", resp)
306+
fmt.Println()
307+
break
308+
}
309+
if err != nil {
310+
fmt.Printf("Error updating libraries index: %s\n", err)
311+
os.Exit(1)
312+
}
313+
if resp.GetDownloadProgress() != nil {
314+
fmt.Printf(">> DOWNLOAD: %s\n", resp.GetDownloadProgress())
315+
}
316+
}
317+
295318
// LIB DOWNLOAD
296319
fmt.Println("=== calling LibraryDownload([email protected])")
297320
downloadRespStream, err := client.LibraryDownload(context.Background(), &rpc.LibraryDownloadReq{

daemon/daemon.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ func (s *ArduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexReq, stream rpc.
7676
return err
7777
}
7878

79+
func (s *ArduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesIndexReq, stream rpc.ArduinoCore_UpdateLibrariesIndexServer) error {
80+
err := commands.UpdateLibrariesIndex(stream.Context(), req,
81+
func(p *rpc.DownloadProgress) { stream.Send(&rpc.UpdateLibrariesIndexResp{DownloadProgress: p}) },
82+
)
83+
if err != nil {
84+
return err
85+
}
86+
return stream.Send(&rpc.UpdateLibrariesIndexResp{})
87+
}
88+
7989
func (s *ArduinoCoreServerImpl) Init(ctx context.Context, req *rpc.InitReq) (*rpc.InitResp, error) {
8090
return commands.Init(ctx, req)
8191
}
@@ -88,8 +98,10 @@ func (s *ArduinoCoreServerImpl) Compile(req *rpc.CompileReq, stream rpc.ArduinoC
8898
func(p *rpc.TaskProgress) { stream.Send(&rpc.CompileResp{TaskProgress: p}) },
8999
func(p *rpc.DownloadProgress) { stream.Send(&rpc.CompileResp{DownloadProgress: p}) },
90100
)
91-
stream.Send(resp)
92-
return err
101+
if err != nil {
102+
return err
103+
}
104+
return stream.Send(resp)
93105
}
94106

95107
func (s *ArduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallReq, stream rpc.ArduinoCore_PlatformInstallServer) error {

0 commit comments

Comments
 (0)