Skip to content

Commit 40f07bf

Browse files
committed
Refactored LibrariesManager.getLibrariesDir function
This helped to find out 2 places where the `installDir` was unnecessary.
1 parent bea6af8 commit 40f07bf

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

Diff for: arduino/libraries/librariesmanager/install.go

+3-16
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@ type LibraryInstallPlan struct {
5353
// install path, where the library should be installed and the possible library that is already
5454
// installed on the same folder and it's going to be replaced by the new one.
5555
func (lm *LibrariesManager) InstallPrerequisiteCheck(name string, version *semver.Version, installLocation libraries.LibraryLocation) (*LibraryInstallPlan, error) {
56-
installDir := lm.getLibrariesDir(installLocation)
57-
if installDir == nil {
58-
if installLocation == libraries.User {
59-
return nil, fmt.Errorf(tr("User directory not set"))
60-
}
61-
return nil, fmt.Errorf(tr("Builtin libraries directory not set"))
56+
installDir, err := lm.getLibrariesDir(installLocation)
57+
if err != nil {
58+
return nil, err
6259
}
6360

6461
libs := lm.FindByReference(&librariesindex.Reference{Name: name}, installLocation)
@@ -130,11 +127,6 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
130127

131128
// InstallZipLib installs a Zip library on the specified path.
132129
func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath *paths.Path, overwrite bool) error {
133-
installDir := lm.getLibrariesDir(libraries.User)
134-
if installDir == nil {
135-
return fmt.Errorf(tr("User directory not set"))
136-
}
137-
138130
// Clone library in a temporary directory
139131
tmpDir, err := paths.MkTempDir("", "")
140132
if err != nil {
@@ -207,11 +199,6 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath *path
207199

208200
// InstallGitLib installs a library hosted on a git repository on the specified path.
209201
func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
210-
installDir := lm.getLibrariesDir(libraries.User)
211-
if installDir == nil {
212-
return fmt.Errorf(tr("User directory not set"))
213-
}
214-
215202
gitLibraryName, ref, err := parseGitURL(gitURL)
216203
if err != nil {
217204
return err

Diff for: arduino/libraries/librariesmanager/librariesmanager.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package librariesmanager
1717

1818
import (
19+
"errors"
1920
"fmt"
2021
"os"
2122

@@ -140,13 +141,20 @@ func (lm *LibrariesManager) RescanLibraries() []*status.Status {
140141
return statuses
141142
}
142143

143-
func (lm *LibrariesManager) getLibrariesDir(installLocation libraries.LibraryLocation) *paths.Path {
144+
func (lm *LibrariesManager) getLibrariesDir(installLocation libraries.LibraryLocation) (*paths.Path, error) {
144145
for _, dir := range lm.LibrariesDir {
145146
if dir.Location == installLocation {
146-
return dir.Path
147+
return dir.Path, nil
147148
}
148149
}
149-
return nil
150+
switch installLocation {
151+
case libraries.User:
152+
return nil, errors.New(tr("user directory not set"))
153+
case libraries.IDEBuiltIn:
154+
return nil, errors.New(tr("built-in libraries directory not set"))
155+
default:
156+
return nil, fmt.Errorf("libraries directory not set: %s", installLocation.String())
157+
}
150158
}
151159

152160
// LoadLibrariesFromDir loads all libraries in the given directory. Returns

0 commit comments

Comments
 (0)