Skip to content

Add overwrite option to zip and git lib install for gRPC interface #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions arduino/libraries/librariesmanager/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
}

//InstallZipLib installs a Zip library on the specified path.
func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath string) error {
func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath string, overwrite bool) error {
libsDir := lm.getUserLibrariesDir()
if libsDir == nil {
return fmt.Errorf("User directory not set")
Expand All @@ -104,6 +104,8 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
if err != nil {
return err
}
// Deletes temp dir used to extract archive when finished
defer tmpDir.RemoveAll()

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

libraryName := paths[0].Base()
extractionPath := paths[0]
libraryName := extractionPath.Base()
installPath := libsDir.Join(libraryName)

// Deletes libraries folder if already installed
if _, ok := lm.Libraries[libraryName]; ok {
if err := libsDir.MkdirAll(); err != nil {
return err
}
defer func() {
// Clean up install dir if installation failed
files, err := installPath.ReadDir()
if err == nil && len(files) == 0 {
installPath.RemoveAll()
}
}()

// Delete library folder if already installed
if installPath.IsDir() {
if !overwrite {
return fmt.Errorf("library %s already installed", libraryName)
}
logrus.
WithField("library name", libraryName).
WithField("install path", installPath).
Expand All @@ -144,15 +161,16 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
WithField("zip file", archivePath).
Trace("Installing library")

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

return nil
}

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

// Deletes libraries folder if already installed
if _, ok := lm.Libraries[libraryName]; ok {
if !overwrite {
return fmt.Errorf("library %s already installed", libraryName)
}
logrus.
WithField("library name", libraryName).
WithField("install path", installPath).
Expand Down
20 changes: 10 additions & 10 deletions cli/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func runInstallCommand(cmd *cobra.Command, args []string) {

if installFlags.zipPath {
for _, path := range args {
ziplibraryInstallReq := &rpc.ZipLibraryInstallReq{
Instance: instance,
Path: path,
}
err := lib.ZipLibraryInstall(context.Background(), ziplibraryInstallReq, output.TaskProgress())
err := lib.ZipLibraryInstall(context.Background(), &rpc.ZipLibraryInstallReq{
Instance: instance,
Path: path,
Overwrite: true,
}, output.TaskProgress())
if err != nil {
feedback.Errorf("Error installing Zip Library: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand All @@ -99,11 +99,11 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
}
url = wd.String()
}
gitlibraryInstallReq := &rpc.GitLibraryInstallReq{
Instance: instance,
Url: url,
}
err := lib.GitLibraryInstall(context.Background(), gitlibraryInstallReq, output.TaskProgress())
err := lib.GitLibraryInstall(context.Background(), &rpc.GitLibraryInstallReq{
Instance: instance,
Url: url,
Overwrite: true,
}, output.TaskProgress())
if err != nil {
feedback.Errorf("Error installing Git Library: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
6 changes: 2 additions & 4 deletions commands/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ func installLibrary(lm *librariesmanager.LibrariesManager, libRelease *libraries
//ZipLibraryInstall FIXMEDOC
func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallReq, taskCB commands.TaskProgressCB) error {
lm := commands.GetLibraryManager(req.GetInstance().GetId())
archivePath := req.GetPath()
if err := lm.InstallZipLib(ctx, archivePath); err != nil {
if err := lm.InstallZipLib(ctx, req.Path, req.Overwrite); err != nil {
return err
}
taskCB(&rpc.TaskProgress{Message: "Installed Archived Library", Completed: true})
Expand All @@ -122,8 +121,7 @@ func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallReq, taskC
//GitLibraryInstall FIXMEDOC
func GitLibraryInstall(ctx context.Context, req *rpc.GitLibraryInstallReq, taskCB commands.TaskProgressCB) error {
lm := commands.GetLibraryManager(req.GetInstance().GetId())
url := req.GetUrl()
if err := lm.InstallGitLib(url); err != nil {
if err := lm.InstallGitLib(req.Url, req.Overwrite); err != nil {
return err
}
taskCB(&rpc.TaskProgress{Message: "Installed Library from Git URL", Completed: true})
Expand Down
94 changes: 58 additions & 36 deletions rpc/commands/lib.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions rpc/commands/lib.proto
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ message ZipLibraryInstallReq {
Instance instance = 1;
//Path to the archived library
string Path = 2;
// Set to true to overwrite an already installed library with the same name. Defaults to false.
bool overwrite = 3;
}

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

message GitLibraryInstallResp {
Expand Down