Merge pull request #1431 from Alvise88/patch-2

Fixes inconsistencies in dagger mod get, issue #1378
This commit is contained in:
Gerhard Lazu 2022-01-18 09:11:21 +00:00 committed by GitHub
commit 455beae762
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 36 deletions

View File

@ -25,28 +25,24 @@ type Require struct {
func newRequire(repoName, versionConstraint string) (*Require, error) {
switch {
case strings.HasPrefix(repoName, "github.com"):
return parseGithubRepoName(repoName, versionConstraint)
case strings.HasPrefix(repoName, pkg.AlphaModule):
return parseDaggerRepoName(repoName, versionConstraint)
case strings.Contains(repoName, ".git"):
return parseGitRepoName(repoName, versionConstraint)
default:
return nil, fmt.Errorf("repo name does not match suported providers")
return parseGitRepoName(repoName, versionConstraint)
}
}
var githubRepoNameRegex = regexp.MustCompile(`(github.com/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+)([a-zA-Z0-9/_.-]*)@?([0-9a-zA-Z.-]*)`)
var gitRepoNameRegex = regexp.MustCompile(`([a-zA-Z0-9_.-]+(?::\d*)?/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+)([a-zA-Z0-9/_.-]*)@?([0-9a-zA-Z.-]*)`)
func parseGithubRepoName(repoName, versionConstraint string) (*Require, error) {
repoMatches := githubRepoNameRegex.FindStringSubmatch(repoName)
func parseGitRepoName(repoName, versionConstraint string) (*Require, error) {
repoMatches := gitRepoNameRegex.FindStringSubmatch(repoName)
if len(repoMatches) < 4 {
return nil, fmt.Errorf("issue when parsing github repo")
}
return &Require{
repo: repoMatches[1],
repo: strings.TrimSuffix(repoMatches[1], ".git"),
path: repoMatches[2],
version: repoMatches[3],
versionConstraint: versionConstraint,
@ -76,27 +72,6 @@ func parseDaggerRepoName(repoName, versionConstraint string) (*Require, error) {
}, nil
}
// TODO: Get real URL regex
var gitRepoNameRegex = regexp.MustCompile(`^([a-zA-Z0-9_.-]+\.[a-zA-Z0-9]+(?::\d*)?/[a-zA-Z0-9_.-/]+?\.git)([a-zA-Z0-9/_.-]*)?@?([0-9a-zA-Z.-]*)`)
func parseGitRepoName(repoName, versionConstraint string) (*Require, error) {
repoMatches := gitRepoNameRegex.FindStringSubmatch(repoName)
if len(repoMatches) < 3 {
return nil, fmt.Errorf("issue when parsing git repo")
}
return &Require{
repo: repoMatches[1],
path: repoMatches[2],
version: repoMatches[3],
versionConstraint: versionConstraint,
cloneRepo: repoMatches[1],
clonePath: repoMatches[2],
}, nil
}
func (r *Require) String() string {
return fmt.Sprintf("%s@%s", r.fullPath(), r.version)
}

View File

@ -98,7 +98,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider without folder",
in: "git.blocklayer.com/dagger/test.git@main",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "",
version: "main",
},
@ -107,7 +107,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider with folder and version",
in: "git.blocklayer.com/dagger/test.git/test@v1.1.0",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "/test",
version: "v1.1.0",
},
@ -116,7 +116,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider with folder and version",
in: "git.blocklayer.com/dagger/test.git/test@v1.1.0",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "/test",
version: "v1.1.0",
},
@ -125,7 +125,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider without folder",
in: "git.blocklayer.com/dagger/test.git",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "",
version: "",
},
@ -134,7 +134,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider with folder, no version",
in: "git.blocklayer.com/dagger/test.git/test",
want: &Require{
repo: "git.blocklayer.com/dagger/test.git",
repo: "git.blocklayer.com/dagger/test",
path: "/test",
version: "",
},
@ -143,7 +143,7 @@ func TestParseArgument(t *testing.T) {
name: "Custom git provider with custom port, folder, and version",
in: "git.blocklayer.com:7999/ops/dagger.git/stuff/here@v5",
want: &Require{
repo: "git.blocklayer.com:7999/ops/dagger.git",
repo: "git.blocklayer.com:7999/ops/dagger",
path: "/stuff/here",
version: "v5",
},