Skip to content

Commit 5704138

Browse files
authored
Add overwrite option to zip and git lib install for gRPC interface (#1214)
* Add overwrite option to zip and git lib install for gRPC interface * Fix extraction tmp dir and install dir deletion on failure * [skip changelog] Made libraries dir creation more explicit
1 parent 436277f commit 5704138

File tree

5 files changed

+102
-57
lines changed

5 files changed

+102
-57
lines changed

arduino/libraries/librariesmanager/install.go

+28-7
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")
@@ -104,6 +104,8 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
104104
if err != nil {
105105
return err
106106
}
107+
// Deletes temp dir used to extract archive when finished
108+
defer tmpDir.RemoveAll()
107109

108110
file, err := os.Open(archivePath)
109111
if err != nil {
@@ -126,11 +128,26 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
126128
return fmt.Errorf("archive is not valid: multiple files found in zip file top level")
127129
}
128130

129-
libraryName := paths[0].Base()
131+
extractionPath := paths[0]
132+
libraryName := extractionPath.Base()
130133
installPath := libsDir.Join(libraryName)
131134

132-
// Deletes libraries folder if already installed
133-
if _, ok := lm.Libraries[libraryName]; ok {
135+
if err := libsDir.MkdirAll(); err != nil {
136+
return err
137+
}
138+
defer func() {
139+
// Clean up install dir if installation failed
140+
files, err := installPath.ReadDir()
141+
if err == nil && len(files) == 0 {
142+
installPath.RemoveAll()
143+
}
144+
}()
145+
146+
// Delete library folder if already installed
147+
if installPath.IsDir() {
148+
if !overwrite {
149+
return fmt.Errorf("library %s already installed", libraryName)
150+
}
134151
logrus.
135152
WithField("library name", libraryName).
136153
WithField("install path", installPath).
@@ -144,15 +161,16 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
144161
WithField("zip file", archivePath).
145162
Trace("Installing library")
146163

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

151169
return nil
152170
}
153171

154172
//InstallGitLib installs a library hosted on a git repository on the specified path.
155-
func (lm *LibrariesManager) InstallGitLib(gitURL string) error {
173+
func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
156174
libsDir := lm.getUserLibrariesDir()
157175
if libsDir == nil {
158176
return fmt.Errorf("User directory not set")
@@ -170,6 +188,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string) error {
170188

171189
// Deletes libraries folder if already installed
172190
if _, ok := lm.Libraries[libraryName]; ok {
191+
if !overwrite {
192+
return fmt.Errorf("library %s already installed", libraryName)
193+
}
173194
logrus.
174195
WithField("library name", libraryName).
175196
WithField("install path", installPath).

cli/lib/install.go

+10-10
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

+2-4
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

+58-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/commands/lib.proto

+4
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)