Skip to content

Commit ee076dd

Browse files
authored
Fix lib install with git url (#1143)
* Fix lib install with git url * Better git url handling
1 parent c6be6fa commit ee076dd

File tree

5 files changed

+319
-136
lines changed

5 files changed

+319
-136
lines changed

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

+28-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"errors"
2121
"fmt"
22+
"net/url"
2223
"os"
2324
"strings"
2425

@@ -111,21 +112,42 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
111112
}
112113

113114
//InstallGitLib installs a library hosted on a git repository on the specified path.
114-
func (lm *LibrariesManager) InstallGitLib(url string) error {
115+
func (lm *LibrariesManager) InstallGitLib(gitURL string) error {
115116
libsDir := lm.getUserLibrariesDir()
116117
if libsDir == nil {
117118
return fmt.Errorf("User directory not set")
118119
}
119-
i := strings.LastIndex(url, "/")
120-
folder := strings.TrimRight(url[i+1:], ".git")
121-
path := libsDir.Join(folder)
122120

123-
_, err := git.PlainClone(path.String(), false, &git.CloneOptions{
124-
URL: url,
121+
libraryName, err := parseGitURL(gitURL)
122+
if err != nil {
123+
return err
124+
}
125+
126+
installPath := libsDir.Join(libraryName)
127+
128+
_, err = git.PlainClone(installPath.String(), false, &git.CloneOptions{
129+
URL: gitURL,
125130
Progress: os.Stdout,
126131
})
127132
if err != nil {
128133
return err
129134
}
130135
return nil
131136
}
137+
138+
func parseGitURL(gitURL string) (string, error) {
139+
var res string
140+
if strings.HasPrefix(gitURL, "git@") {
141+
// We can't parse these as URLs
142+
i := strings.LastIndex(gitURL, "/")
143+
res = strings.TrimRight(gitURL[i+1:], ".git")
144+
} else if path := paths.New(gitURL); path.Exist() {
145+
res = path.Base()
146+
} else if parsed, err := url.Parse(gitURL); err == nil {
147+
i := strings.LastIndex(parsed.Path, "/")
148+
res = strings.TrimRight(parsed.Path[i+1:], ".git")
149+
} else {
150+
return "", fmt.Errorf("invalid git url")
151+
}
152+
return res, nil
153+
}

Diff for: cli/lib/install.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/arduino/arduino-cli/commands/lib"
3030
"github.com/arduino/arduino-cli/configuration"
3131
rpc "github.com/arduino/arduino-cli/rpc/commands"
32+
"github.com/arduino/go-paths-helper"
3233
"github.com/spf13/cobra"
3334
)
3435

@@ -85,9 +86,18 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
8586
}
8687

8788
if installFlags.gitURL {
89+
url := args[0]
90+
if url == "." {
91+
wd, err := paths.Getwd()
92+
if err != nil {
93+
feedback.Errorf("Couldn't get current working directory: %v", err)
94+
os.Exit(errorcodes.ErrGeneric)
95+
}
96+
url = wd.String()
97+
}
8898
gitlibraryInstallReq := &rpc.GitLibraryInstallReq{
8999
Instance: instance,
90-
Url: args[0],
100+
Url: url,
91101
}
92102
err := lib.GitLibraryInstall(context.Background(), gitlibraryInstallReq, output.TaskProgress())
93103
if err != nil {

0 commit comments

Comments
 (0)