Skip to content

Fix lib install with git url #1143

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
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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
Loading