Skip to content

Commit

Permalink
Fix lib install with git url (#1143)
Browse files Browse the repository at this point in the history
* Fix lib install with git url

* Better git url handling
  • Loading branch information
silvanocerza committed Jan 20, 2021
1 parent c6be6fa commit ee076dd
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 136 deletions.
34 changes: 28 additions & 6 deletions arduino/libraries/librariesmanager/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"os"
"strings"

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

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

_, err := git.PlainClone(path.String(), false, &git.CloneOptions{
URL: url,
libraryName, err := parseGitURL(gitURL)
if err != nil {
return err
}

installPath := libsDir.Join(libraryName)

_, err = git.PlainClone(installPath.String(), false, &git.CloneOptions{
URL: gitURL,
Progress: os.Stdout,
})
if err != nil {
return err
}
return nil
}

func parseGitURL(gitURL string) (string, error) {
var res string
if strings.HasPrefix(gitURL, "git@") {
// We can't parse these as URLs
i := strings.LastIndex(gitURL, "/")
res = strings.TrimRight(gitURL[i+1:], ".git")
} else if path := paths.New(gitURL); path.Exist() {
res = path.Base()
} else if parsed, err := url.Parse(gitURL); err == nil {
i := strings.LastIndex(parsed.Path, "/")
res = strings.TrimRight(parsed.Path[i+1:], ".git")
} else {
return "", fmt.Errorf("invalid git url")
}
return res, nil
}
12 changes: 11 additions & 1 deletion cli/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/arduino/arduino-cli/commands/lib"
"github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/arduino/go-paths-helper"
"github.com/spf13/cobra"
)

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

if installFlags.gitURL {
url := args[0]
if url == "." {
wd, err := paths.Getwd()
if err != nil {
feedback.Errorf("Couldn't get current working directory: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
url = wd.String()
}
gitlibraryInstallReq := &rpc.GitLibraryInstallReq{
Instance: instance,
Url: args[0],
Url: url,
}
err := lib.GitLibraryInstall(context.Background(), gitlibraryInstallReq, output.TaskProgress())
if err != nil {
Expand Down

0 comments on commit ee076dd

Please sign in to comment.