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

View File

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

View File

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

View File

@ -81,7 +81,9 @@ func processRequire(workspacePath string, req *require, modFile *file) error {
}
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 {
return fmt.Errorf("error downloading package %s: %w", req, err)
}
@ -136,6 +138,9 @@ func compareVersions(reqV1, reqV2 string) (int, error) {
}
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 {
panic(err)
}

View File

@ -5,6 +5,9 @@ import (
"os"
"path"
"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/plumbing"
@ -16,10 +19,22 @@ type repo struct {
contents *git.Repository
}
func clone(require *require, dir string) (*repo, error) {
r, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: require.cloneURL(),
})
func clone(require *require, dir string, privateKeyFile, privateKeyPassword string) (*repo, error) {
o := git.CloneOptions{
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 {
return nil, err
}
@ -97,7 +112,6 @@ func (r *repo) latestTag() (string, error) {
versions[i] = v
}
// After this, the versions are properly sorted
sort.Sort(version.Collection(versions))
return versions[len(versions)-1].Original(), nil

View File

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