Skip to content

Commit 2ea3608

Browse files
cmaglieper1234
andauthored
Added --no-overwrite flag in lib install command (#1793)
* Added no_overwrite field in LibraryInstall * Added tests * Check if libraries needs to be installed prior to download/install * Update rpc/cc/arduino/cli/commands/v1/lib.proto Co-authored-by: per1234 <[email protected]> Co-authored-by: per1234 <[email protected]>
1 parent 38ee95c commit 2ea3608

File tree

6 files changed

+370
-312
lines changed

6 files changed

+370
-312
lines changed

Diff for: cli/lib/install.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ import (
3737
)
3838

3939
var (
40-
noDeps bool
41-
gitURL bool
42-
zipPath bool
40+
noDeps bool
41+
noOverwrite bool
42+
gitURL bool
43+
zipPath bool
4344
)
4445

4546
func initInstallCommand() *cobra.Command {
@@ -59,6 +60,7 @@ func initInstallCommand() *cobra.Command {
5960
},
6061
}
6162
installCommand.Flags().BoolVar(&noDeps, "no-deps", false, tr("Do not install dependencies."))
63+
installCommand.Flags().BoolVar(&noOverwrite, "no-overwrite", false, tr("Do not overwrite already installed libraries."))
6264
installCommand.Flags().BoolVar(&gitURL, "git-url", false, tr("Enter git url for libraries hosted on repositories"))
6365
installCommand.Flags().BoolVar(&zipPath, "zip-path", false, tr("Enter a path to zip file"))
6466
return installCommand
@@ -87,7 +89,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
8789
err := lib.ZipLibraryInstall(context.Background(), &rpc.ZipLibraryInstallRequest{
8890
Instance: instance,
8991
Path: path,
90-
Overwrite: true,
92+
Overwrite: !noOverwrite,
9193
}, output.TaskProgress())
9294
if err != nil {
9395
feedback.Errorf(tr("Error installing Zip Library: %v"), err)
@@ -110,7 +112,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
110112
err := lib.GitLibraryInstall(context.Background(), &rpc.GitLibraryInstallRequest{
111113
Instance: instance,
112114
Url: url,
113-
Overwrite: true,
115+
Overwrite: !noOverwrite,
114116
}, output.TaskProgress())
115117
if err != nil {
116118
feedback.Errorf(tr("Error installing Git Library: %v"), err)
@@ -128,10 +130,11 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
128130

129131
for _, libRef := range libRefs {
130132
libraryInstallRequest := &rpc.LibraryInstallRequest{
131-
Instance: instance,
132-
Name: libRef.Name,
133-
Version: libRef.Version,
134-
NoDeps: noDeps,
133+
Instance: instance,
134+
Name: libRef.Name,
135+
Version: libRef.Version,
136+
NoDeps: noDeps,
137+
NoOverwrite: noOverwrite,
135138
}
136139
err := lib.LibraryInstall(context.Background(), libraryInstallRequest, output.ProgressBar(), output.TaskProgress())
137140
if err != nil {

Diff for: commands/lib/install.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package lib
1818
import (
1919
"context"
2020
"errors"
21+
"fmt"
2122

2223
"github.com/arduino/arduino-cli/arduino"
2324
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
@@ -64,7 +65,8 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
6465
}
6566
}
6667

67-
didInstall := false
68+
// Find the libReleasesToInstall to install
69+
libReleasesToInstall := []*librariesindex.Release{}
6870
for _, lib := range toInstall {
6971
libRelease, err := findLibraryIndexRelease(lm, &rpc.LibraryInstallRequest{
7072
Name: lib.Name,
@@ -73,7 +75,31 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
7375
if err != nil {
7476
return err
7577
}
78+
libReleasesToInstall = append(libReleasesToInstall, libRelease)
79+
}
7680

81+
// Check if any of the libraries to install is already installed and remove it from the list
82+
j := 0
83+
for i, libRelease := range libReleasesToInstall {
84+
_, libReplaced, err := lm.InstallPrerequisiteCheck(libRelease)
85+
if errors.Is(err, librariesmanager.ErrAlreadyInstalled) {
86+
taskCB(&rpc.TaskProgress{Message: tr("Already installed %s", libRelease), Completed: true})
87+
} else if err != nil {
88+
return err
89+
} else {
90+
libReleasesToInstall[j] = libReleasesToInstall[i]
91+
j++
92+
}
93+
if req.GetNoOverwrite() {
94+
if libReplaced != nil {
95+
return fmt.Errorf(tr("Library %[1]s is already installed, but with a different version: %[2]s", libRelease, libReplaced))
96+
}
97+
}
98+
}
99+
libReleasesToInstall = libReleasesToInstall[:j]
100+
101+
didInstall := false
102+
for _, libRelease := range libReleasesToInstall {
77103
if err := downloadLibrary(lm, libRelease, downloadCB, taskCB); err != nil {
78104
return err
79105
}

0 commit comments

Comments
 (0)