add support for cloning private repos in mod get command

Signed-off-by: Tihomir Jovicic <tihomir.jovicic.develop@gmail.com>
This commit is contained in:
Tihomir Jovicic 2021-08-10 12:23:03 +02:00
parent 4f8128abcb
commit 050d1fcb3f
6 changed files with 37 additions and 21 deletions

View File

@ -28,7 +28,6 @@ func parseGithubRepoName(arg string) (*require, error) {
} }
return &require{ return &require{
prefix: "https://",
repo: repoMatches[1], repo: repoMatches[1],
path: repoMatches[2], path: repoMatches[2],
version: repoMatches[3], version: repoMatches[3],
@ -48,7 +47,6 @@ func parseDaggerRepoName(arg string) (*require, error) {
} }
return &require{ return &require{
prefix: "https://",
repo: "alpha.dagger.io", repo: "alpha.dagger.io",
path: repoMatches[1], path: repoMatches[1],
version: repoMatches[2], version: repoMatches[2],

View File

@ -118,7 +118,6 @@ func (f *file) search(r *require) *require {
} }
type require struct { type require struct {
prefix string
repo string repo string
path string path string
version string version string
@ -127,10 +126,6 @@ type require struct {
clonePath string clonePath string
} }
func (r *require) cloneURL() string {
return fmt.Sprintf("%s%s", r.prefix, r.cloneRepo)
}
func (r *require) fullPath() string { func (r *require) fullPath() string {
return path.Join(r.repo, r.path) return path.Join(r.repo, r.path)
} }

View File

@ -32,13 +32,11 @@ func TestReadFile(t *testing.T) {
module: "alpha.dagger.io", module: "alpha.dagger.io",
require: []*require{ require: []*require{
{ {
prefix: "https://",
repo: "github.com/tjovicic/test", repo: "github.com/tjovicic/test",
path: "", path: "",
version: "xyz", version: "xyz",
}, },
{ {
prefix: "https://",
repo: "github.com/bla/bla", repo: "github.com/bla/bla",
path: "", path: "",
version: "abc", version: "abc",

View File

@ -81,7 +81,9 @@ func processRequire(workspacePath string, req *require, modFile *file) error {
} }
defer os.RemoveAll(tmpPath) defer os.RemoveAll(tmpPath)
r, err := clone(req, tmpPath) privateKeyFile := viper.GetString("private-key-file")
privateKeyPassword := viper.GetString("private-key-password")
r, err := clone(req, tmpPath, privateKeyFile, privateKeyPassword)
if err != nil { if err != nil {
return fmt.Errorf("error downloading package %s: %w", req, err) return fmt.Errorf("error downloading package %s: %w", req, err)
} }
@ -136,6 +138,9 @@ func compareVersions(reqV1, reqV2 string) (int, error) {
} }
func init() { func init() {
getCmd.Flags().String("private-key-file", "~/.ssh/id_rsa", "Private ssh key")
getCmd.Flags().String("private-key-password", "", "Private ssh key password")
if err := viper.BindPFlags(getCmd.Flags()); err != nil { if err := viper.BindPFlags(getCmd.Flags()); err != nil {
panic(err) panic(err)
} }

View File

@ -5,6 +5,9 @@ import (
"os" "os"
"path" "path"
"sort" "sort"
"strings"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
@ -16,10 +19,22 @@ type repo struct {
contents *git.Repository contents *git.Repository
} }
func clone(require *require, dir string) (*repo, error) { func clone(require *require, dir string, privateKeyFile, privateKeyPassword string) (*repo, error) {
r, err := git.PlainClone(dir, false, &git.CloneOptions{ o := git.CloneOptions{
URL: require.cloneURL(), URL: fmt.Sprintf("https://%s", require.cloneRepo),
}) }
if privateKeyFile != "" {
publicKeys, err := ssh.NewPublicKeysFromFile("git", privateKeyFile, privateKeyPassword)
if err != nil {
return nil, err
}
o.Auth = publicKeys
o.URL = fmt.Sprintf("git@%s", strings.Replace(require.cloneRepo, "/", ":", 1))
}
r, err := git.PlainClone(dir, false, &o)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -97,7 +112,6 @@ func (r *repo) latestTag() (string, error) {
versions[i] = v versions[i] = v
} }
// After this, the versions are properly sorted
sort.Sort(version.Collection(versions)) sort.Sort(version.Collection(versions))
return versions[len(versions)-1].Original(), nil return versions[len(versions)-1].Original(), nil

View File

@ -14,7 +14,6 @@ func TestClone(t *testing.T) {
{ {
name: "resolving shorter hash version", name: "resolving shorter hash version",
require: require{ require: require{
prefix: "https://",
cloneRepo: "github.com/tjovicic/gcpcloudrun-cue", cloneRepo: "github.com/tjovicic/gcpcloudrun-cue",
clonePath: "", clonePath: "",
version: "d530f2ea2099", version: "d530f2ea2099",
@ -23,7 +22,6 @@ func TestClone(t *testing.T) {
{ {
name: "resolving branch name", name: "resolving branch name",
require: require{ require: require{
prefix: "https://",
cloneRepo: "github.com/tjovicic/gcpcloudrun-cue", cloneRepo: "github.com/tjovicic/gcpcloudrun-cue",
clonePath: "", clonePath: "",
version: "main", version: "main",
@ -32,12 +30,21 @@ func TestClone(t *testing.T) {
{ {
name: "resolving tag", name: "resolving tag",
require: require{ require: require{
prefix: "https://",
cloneRepo: "github.com/tjovicic/gcpcloudrun-cue", cloneRepo: "github.com/tjovicic/gcpcloudrun-cue",
clonePath: "", clonePath: "",
version: "v0.3", version: "v0.3",
}, },
}, },
{
name: "alpha.dagger.io",
require: require{
cloneRepo: "github.com/dagger/dagger",
clonePath: "",
version: "",
repo: "alpha.dagger.io",
},
},
} }
for _, c := range cases { for _, c := range cases {
@ -49,7 +56,7 @@ func TestClone(t *testing.T) {
defer os.Remove(tmpDir) defer os.Remove(tmpDir)
_, err = clone(&c.require, tmpDir) _, err = clone(&c.require, tmpDir, "", "")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -65,11 +72,10 @@ func TestListTags(t *testing.T) {
defer os.Remove(tmpDir) defer os.Remove(tmpDir)
r, err := clone(&require{ r, err := clone(&require{
prefix: "https://",
cloneRepo: "github.com/tjovicic/gcpcloudrun-cue", cloneRepo: "github.com/tjovicic/gcpcloudrun-cue",
clonePath: "", clonePath: "",
version: "", version: "",
}, tmpDir) }, tmpDir, "", "")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }