Skip to content

Commit 46b4cc6

Browse files
committed
Add overwrite option to zip and git lib install for gRPC interface
1 parent efa6823 commit 46b4cc6

File tree

5 files changed

+107
-57
lines changed

5 files changed

+107
-57
lines changed

arduino/libraries/librariesmanager/install.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
9494
}
9595

9696
//InstallZipLib installs a Zip library on the specified path.
97-
func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath string) error {
97+
func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath string, overwrite bool) error {
9898
libsDir := lm.getUserLibrariesDir()
9999
if libsDir == nil {
100100
return fmt.Errorf("User directory not set")
@@ -116,6 +116,10 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
116116
if err := extract.Archive(ctx, file, tmpDir.String(), nil); err != nil {
117117
return fmt.Errorf("extracting archive: %w", err)
118118
}
119+
defer func() {
120+
// Deletes temp dir used to extract archive
121+
tmpDir.RemoveAll()
122+
}()
119123

120124
paths, err := tmpDir.ReadDir()
121125
if err != nil {
@@ -126,11 +130,29 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
126130
return fmt.Errorf("archive is not valid: multiple files found in zip file top level")
127131
}
128132

129-
libraryName := paths[0].Base()
133+
extractionPath := paths[0]
134+
libraryName := extractionPath.Base()
130135
installPath := libsDir.Join(libraryName)
131136

132-
// Deletes libraries folder if already installed
133-
if _, ok := lm.Libraries[libraryName]; ok {
137+
if err := installPath.Parent().MkdirAll(); err != nil {
138+
return err
139+
}
140+
defer func() {
141+
// Clean up install dir if installation failed
142+
files, err := installPath.Parent().ReadDir()
143+
if err != nil {
144+
return
145+
}
146+
if len(files) == 0 {
147+
installPath.Parent().RemoveAll()
148+
}
149+
}()
150+
151+
// Delete library folder if already installed
152+
if installPath.IsDir() {
153+
if !overwrite {
154+
return fmt.Errorf("library %s already installed", libraryName)
155+
}
134156
logrus.
135157
WithField("library name", libraryName).
136158
WithField("install path", installPath).
@@ -144,15 +166,16 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
144166
WithField("zip file", archivePath).
145167
Trace("Installing library")
146168

147-
if err := tmpDir.Join(libraryName).CopyDirTo(installPath); err != nil {
148-
return fmt.Errorf("copying library: %w", err)
169+
// Copy extracted library in the destination directory
170+
if err := extractionPath.CopyDirTo(installPath); err != nil {
171+
return fmt.Errorf("moving extracted archive to destination dir: %s", err)
149172
}
150173

151174
return nil
152175
}
153176

154177
//InstallGitLib installs a library hosted on a git repository on the specified path.
155-
func (lm *LibrariesManager) InstallGitLib(gitURL string) error {
178+
func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
156179
libsDir := lm.getUserLibrariesDir()
157180
if libsDir == nil {
158181
return fmt.Errorf("User directory not set")
@@ -170,6 +193,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string) error {
170193

171194
// Deletes libraries folder if already installed
172195
if _, ok := lm.Libraries[libraryName]; ok {
196+
if !overwrite {
197+
return fmt.Errorf("library %s already installed", libraryName)
198+
}
173199
logrus.
174200
WithField("library name", libraryName).
175201
WithField("install path", installPath).

cli/lib/install.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
7676

7777
if installFlags.zipPath {
7878
for _, path := range args {
79-
ziplibraryInstallReq := &rpc.ZipLibraryInstallReq{
80-
Instance: instance,
81-
Path: path,
82-
}
83-
err := lib.ZipLibraryInstall(context.Background(), ziplibraryInstallReq, output.TaskProgress())
79+
err := lib.ZipLibraryInstall(context.Background(), &rpc.ZipLibraryInstallReq{
80+
Instance: instance,
81+
Path: path,
82+
Overwrite: true,
83+
}, output.TaskProgress())
8484
if err != nil {
8585
feedback.Errorf("Error installing Zip Library: %v", err)
8686
os.Exit(errorcodes.ErrGeneric)
@@ -99,11 +99,11 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
9999
}
100100
url = wd.String()
101101
}
102-
gitlibraryInstallReq := &rpc.GitLibraryInstallReq{
103-
Instance: instance,
104-
Url: url,
105-
}
106-
err := lib.GitLibraryInstall(context.Background(), gitlibraryInstallReq, output.TaskProgress())
102+
err := lib.GitLibraryInstall(context.Background(), &rpc.GitLibraryInstallReq{
103+
Instance: instance,
104+
Url: url,
105+
Overwrite: true,
106+
}, output.TaskProgress())
107107
if err != nil {
108108
feedback.Errorf("Error installing Git Library: %v", err)
109109
os.Exit(errorcodes.ErrGeneric)

commands/lib/install.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *libraries
111111
//ZipLibraryInstall FIXMEDOC
112112
func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallReq, taskCB commands.TaskProgressCB) error {
113113
lm := commands.GetLibraryManager(req.GetInstance().GetId())
114-
archivePath := req.GetPath()
115-
if err := lm.InstallZipLib(ctx, archivePath); err != nil {
114+
if err := lm.InstallZipLib(ctx, req.Path, req.Overwrite); err != nil {
116115
return err
117116
}
118117
taskCB(&rpc.TaskProgress{Message: "Installed Archived Library", Completed: true})
@@ -122,8 +121,7 @@ func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallReq, taskC
122121
//GitLibraryInstall FIXMEDOC
123122
func GitLibraryInstall(ctx context.Context, req *rpc.GitLibraryInstallReq, taskCB commands.TaskProgressCB) error {
124123
lm := commands.GetLibraryManager(req.GetInstance().GetId())
125-
url := req.GetUrl()
126-
if err := lm.InstallGitLib(url); err != nil {
124+
if err := lm.InstallGitLib(req.Url, req.Overwrite); err != nil {
127125
return err
128126
}
129127
taskCB(&rpc.TaskProgress{Message: "Installed Library from Git URL", Completed: true})

rpc/commands/lib.pb.go

Lines changed: 58 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/commands/lib.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ message ZipLibraryInstallReq {
302302
Instance instance = 1;
303303
//Path to the archived library
304304
string Path = 2;
305+
// Set to true to overwrite an already installed library with the same name. Defaults to false.
306+
bool overwrite = 3;
305307
}
306308

307309
message ZipLibraryInstallResp {
@@ -314,6 +316,8 @@ message GitLibraryInstallReq {
314316
Instance instance = 1;
315317
// URL to the repository containing the library
316318
string url = 2;
319+
// Set to true to overwrite an already installed library with the same name. Defaults to false.
320+
bool overwrite = 3;
317321
}
318322

319323
message GitLibraryInstallResp {

0 commit comments

Comments
 (0)