Skip to content

Commit ead7e11

Browse files
committed
Add flag to skip libraries dependencies installation to gRPC LibraryInstall function
1 parent 59b6277 commit ead7e11

File tree

6 files changed

+424
-330
lines changed

6 files changed

+424
-330
lines changed

cli/lib/install.go

+5-38
Original file line numberDiff line numberDiff line change
@@ -118,49 +118,16 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
118118
os.Exit(errorcodes.ErrBadArgument)
119119
}
120120

121-
toInstall := map[string]*rpc.LibraryDependencyStatus{}
122-
if installFlags.noDeps {
123-
for _, libRef := range libRefs {
124-
toInstall[libRef.Name] = &rpc.LibraryDependencyStatus{
125-
Name: libRef.Name,
126-
VersionRequired: libRef.Version,
127-
}
128-
}
129-
} else {
130-
for _, libRef := range libRefs {
131-
depsResp, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesReq{
132-
Instance: instance,
133-
Name: libRef.Name,
134-
Version: libRef.Version,
135-
})
136-
if err != nil {
137-
feedback.Errorf("Error resolving dependencies for %s: %s", libRef, err)
138-
os.Exit(errorcodes.ErrGeneric)
139-
}
140-
for _, dep := range depsResp.GetDependencies() {
141-
feedback.Printf("%s depends on %s@%s", libRef, dep.GetName(), dep.GetVersionRequired())
142-
if existingDep, has := toInstall[dep.GetName()]; has {
143-
if existingDep.GetVersionRequired() != dep.GetVersionRequired() {
144-
// TODO: make a better error
145-
feedback.Errorf("The library %s is required in two different versions: %s and %s",
146-
dep.GetName(), dep.GetVersionRequired(), existingDep.GetVersionRequired())
147-
os.Exit(errorcodes.ErrGeneric)
148-
}
149-
}
150-
toInstall[dep.GetName()] = dep
151-
}
152-
}
153-
}
154-
155-
for _, library := range toInstall {
121+
for _, libRef := range libRefs {
156122
libraryInstallReq := &rpc.LibraryInstallReq{
157123
Instance: instance,
158-
Name: library.Name,
159-
Version: library.VersionRequired,
124+
Name: libRef.Name,
125+
Version: libRef.Version,
126+
NoDeps: installFlags.noDeps,
160127
}
161128
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(), output.TaskProgress())
162129
if err != nil {
163-
feedback.Errorf("Error installing %s: %v", library, err)
130+
feedback.Errorf("Error installing %s: %v", libRef.Name, err)
164131
os.Exit(errorcodes.ErrGeneric)
165132
}
166133
}

client_example/main.go

+36
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ func main() {
207207
log.Println("calling LibraryInstall([email protected])")
208208
callLibInstall(client, instance, "0.15.2")
209209

210+
// Install a library skipping deps installation
211+
log.Println("calling LibraryInstall([email protected]) skipping dependencies")
212+
callLibInstallNoDeps(client, instance, "0.9.9")
213+
210214
// Upgrade all libs to latest
211215
log.Println("calling LibraryUpgradeAll()")
212216
callLibUpgradeAll(client, instance)
@@ -813,6 +817,38 @@ func callLibInstall(client rpc.ArduinoCoreClient, instance *rpc.Instance, versio
813817
}
814818
}
815819

820+
func callLibInstallNoDeps(client rpc.ArduinoCoreClient, instance *rpc.Instance, version string) {
821+
installRespStream, err := client.LibraryInstall(context.Background(),
822+
&rpc.LibraryInstallReq{
823+
Instance: instance,
824+
Name: "Arduino_MKRIoTCarrier",
825+
Version: version,
826+
NoDeps: true,
827+
})
828+
829+
if err != nil {
830+
log.Fatalf("Error installing library: %s", err)
831+
}
832+
833+
for {
834+
installResp, err := installRespStream.Recv()
835+
if err == io.EOF {
836+
log.Print("Lib install done")
837+
break
838+
}
839+
840+
if err != nil {
841+
log.Fatalf("Install error: %s", err)
842+
}
843+
844+
if installResp.GetProgress() != nil {
845+
log.Printf("DOWNLOAD: %s\n", installResp.GetProgress())
846+
}
847+
if installResp.GetTaskProgress() != nil {
848+
log.Printf("TASK: %s\n", installResp.GetTaskProgress())
849+
}
850+
}
851+
}
816852
func callLibUpgradeAll(client rpc.ArduinoCoreClient, instance *rpc.Instance) {
817853
libUpgradeAllRespStream, err := client.LibraryUpgradeAll(context.Background(),
818854
&rpc.LibraryUpgradeAllReq{

commands/lib/install.go

+41-9
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,49 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallReq,
3232

3333
lm := commands.GetLibraryManager(req.GetInstance().GetId())
3434

35-
libRelease, err := findLibraryIndexRelease(lm, req)
36-
if err != nil {
37-
return fmt.Errorf("looking for library: %s", err)
38-
}
39-
40-
if err := downloadLibrary(lm, libRelease, downloadCB, taskCB); err != nil {
41-
return fmt.Errorf("downloading library: %s", err)
35+
toInstall := map[string]*rpc.LibraryDependencyStatus{}
36+
if req.NoDeps {
37+
toInstall[req.Name] = &rpc.LibraryDependencyStatus{
38+
Name: req.Name,
39+
VersionRequired: req.Version,
40+
}
41+
} else {
42+
res, err := LibraryResolveDependencies(ctx, &rpc.LibraryResolveDependenciesReq{
43+
Instance: req.Instance,
44+
Name: req.Name,
45+
Version: req.Version,
46+
})
47+
if err != nil {
48+
return fmt.Errorf("Error resolving dependencies for %s@%s: %s", req.Name, req.Version, err)
49+
}
50+
51+
for _, dep := range res.Dependencies {
52+
if existingDep, has := toInstall[dep.Name]; has {
53+
if existingDep.VersionRequired != dep.VersionRequired {
54+
return fmt.Errorf("two different versions of the library %s are required: %s and %s",
55+
dep.Name, dep.VersionRequired, existingDep.VersionRequired)
56+
}
57+
}
58+
toInstall[dep.Name] = dep
59+
}
4260
}
4361

44-
if err := installLibrary(lm, libRelease, taskCB); err != nil {
45-
return err
62+
for _, lib := range toInstall {
63+
libRelease, err := findLibraryIndexRelease(lm, &rpc.LibraryInstallReq{
64+
Name: lib.Name,
65+
Version: lib.VersionRequired,
66+
})
67+
if err != nil {
68+
return fmt.Errorf("looking for library: %s", err)
69+
}
70+
71+
if err := downloadLibrary(lm, libRelease, downloadCB, taskCB); err != nil {
72+
return fmt.Errorf("downloading library: %s", err)
73+
}
74+
75+
if err := installLibrary(lm, libRelease, taskCB); err != nil {
76+
return err
77+
}
4678
}
4779

4880
if _, err := commands.Rescan(req.GetInstance().GetId()); err != nil {

0 commit comments

Comments
 (0)