diff --git a/cmd/dagger/cmd/mod/arg.go b/cmd/dagger/cmd/mod/arg.go index 0efb2750..fe372206 100644 --- a/cmd/dagger/cmd/mod/arg.go +++ b/cmd/dagger/cmd/mod/arg.go @@ -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], diff --git a/cmd/dagger/cmd/mod/file.go b/cmd/dagger/cmd/mod/file.go index 9fe164fd..d26727e8 100644 --- a/cmd/dagger/cmd/mod/file.go +++ b/cmd/dagger/cmd/mod/file.go @@ -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) } diff --git a/cmd/dagger/cmd/mod/file_test.go b/cmd/dagger/cmd/mod/file_test.go index a94175f4..27fd91f1 100644 --- a/cmd/dagger/cmd/mod/file_test.go +++ b/cmd/dagger/cmd/mod/file_test.go @@ -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", diff --git a/cmd/dagger/cmd/mod/get.go b/cmd/dagger/cmd/mod/get.go index 406e520e..5ded1cf1 100644 --- a/cmd/dagger/cmd/mod/get.go +++ b/cmd/dagger/cmd/mod/get.go @@ -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) } diff --git a/cmd/dagger/cmd/mod/repo.go b/cmd/dagger/cmd/mod/repo.go index f9f5e1a9..e869dceb 100644 --- a/cmd/dagger/cmd/mod/repo.go +++ b/cmd/dagger/cmd/mod/repo.go @@ -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 diff --git a/cmd/dagger/cmd/mod/repo_test.go b/cmd/dagger/cmd/mod/repo_test.go index b4210aef..e28c6516 100644 --- a/cmd/dagger/cmd/mod/repo_test.go +++ b/cmd/dagger/cmd/mod/repo_test.go @@ -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) }