Unified git repository management
Signed-off-by: Alvise88 <vitalvise@gmail.com>
This commit is contained in:
parent
e60b9dc268
commit
f5da25870b
@ -25,28 +25,24 @@ type Require struct {
|
|||||||
|
|
||||||
func newRequire(repoName, versionConstraint string) (*Require, error) {
|
func newRequire(repoName, versionConstraint string) (*Require, error) {
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(repoName, "github.com"):
|
|
||||||
return parseGithubRepoName(repoName, versionConstraint)
|
|
||||||
case strings.HasPrefix(repoName, pkg.AlphaModule):
|
case strings.HasPrefix(repoName, pkg.AlphaModule):
|
||||||
return parseDaggerRepoName(repoName, versionConstraint)
|
return parseDaggerRepoName(repoName, versionConstraint)
|
||||||
case strings.Contains(repoName, ".git"):
|
|
||||||
return parseGitRepoName(repoName, versionConstraint)
|
|
||||||
default:
|
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) {
|
func parseGitRepoName(repoName, versionConstraint string) (*Require, error) {
|
||||||
repoMatches := githubRepoNameRegex.FindStringSubmatch(repoName)
|
repoMatches := gitRepoNameRegex.FindStringSubmatch(repoName)
|
||||||
|
|
||||||
if len(repoMatches) < 4 {
|
if len(repoMatches) < 4 {
|
||||||
return nil, fmt.Errorf("issue when parsing github repo")
|
return nil, fmt.Errorf("issue when parsing github repo")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Require{
|
return &Require{
|
||||||
repo: repoMatches[1],
|
repo: strings.TrimSuffix(repoMatches[1], ".git"),
|
||||||
path: repoMatches[2],
|
path: repoMatches[2],
|
||||||
version: repoMatches[3],
|
version: repoMatches[3],
|
||||||
versionConstraint: versionConstraint,
|
versionConstraint: versionConstraint,
|
||||||
@ -76,27 +72,6 @@ func parseDaggerRepoName(repoName, versionConstraint string) (*Require, error) {
|
|||||||
}, nil
|
}, 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 {
|
func (r *Require) String() string {
|
||||||
return fmt.Sprintf("%s@%s", r.fullPath(), r.version)
|
return fmt.Sprintf("%s@%s", r.fullPath(), r.version)
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,7 @@ func TestParseArgument(t *testing.T) {
|
|||||||
name: "Custom git provider without folder",
|
name: "Custom git provider without folder",
|
||||||
in: "git.blocklayer.com/dagger/test.git@main",
|
in: "git.blocklayer.com/dagger/test.git@main",
|
||||||
want: &Require{
|
want: &Require{
|
||||||
repo: "git.blocklayer.com/dagger/test.git",
|
repo: "git.blocklayer.com/dagger/test",
|
||||||
path: "",
|
path: "",
|
||||||
version: "main",
|
version: "main",
|
||||||
},
|
},
|
||||||
@ -107,7 +107,7 @@ func TestParseArgument(t *testing.T) {
|
|||||||
name: "Custom git provider with folder and version",
|
name: "Custom git provider with folder and version",
|
||||||
in: "git.blocklayer.com/dagger/test.git/test@v1.1.0",
|
in: "git.blocklayer.com/dagger/test.git/test@v1.1.0",
|
||||||
want: &Require{
|
want: &Require{
|
||||||
repo: "git.blocklayer.com/dagger/test.git",
|
repo: "git.blocklayer.com/dagger/test",
|
||||||
path: "/test",
|
path: "/test",
|
||||||
version: "v1.1.0",
|
version: "v1.1.0",
|
||||||
},
|
},
|
||||||
@ -116,7 +116,7 @@ func TestParseArgument(t *testing.T) {
|
|||||||
name: "Custom git provider with folder and version",
|
name: "Custom git provider with folder and version",
|
||||||
in: "git.blocklayer.com/dagger/test.git/test@v1.1.0",
|
in: "git.blocklayer.com/dagger/test.git/test@v1.1.0",
|
||||||
want: &Require{
|
want: &Require{
|
||||||
repo: "git.blocklayer.com/dagger/test.git",
|
repo: "git.blocklayer.com/dagger/test",
|
||||||
path: "/test",
|
path: "/test",
|
||||||
version: "v1.1.0",
|
version: "v1.1.0",
|
||||||
},
|
},
|
||||||
@ -125,7 +125,7 @@ func TestParseArgument(t *testing.T) {
|
|||||||
name: "Custom git provider without folder",
|
name: "Custom git provider without folder",
|
||||||
in: "git.blocklayer.com/dagger/test.git",
|
in: "git.blocklayer.com/dagger/test.git",
|
||||||
want: &Require{
|
want: &Require{
|
||||||
repo: "git.blocklayer.com/dagger/test.git",
|
repo: "git.blocklayer.com/dagger/test",
|
||||||
path: "",
|
path: "",
|
||||||
version: "",
|
version: "",
|
||||||
},
|
},
|
||||||
@ -134,7 +134,7 @@ func TestParseArgument(t *testing.T) {
|
|||||||
name: "Custom git provider with folder, no version",
|
name: "Custom git provider with folder, no version",
|
||||||
in: "git.blocklayer.com/dagger/test.git/test",
|
in: "git.blocklayer.com/dagger/test.git/test",
|
||||||
want: &Require{
|
want: &Require{
|
||||||
repo: "git.blocklayer.com/dagger/test.git",
|
repo: "git.blocklayer.com/dagger/test",
|
||||||
path: "/test",
|
path: "/test",
|
||||||
version: "",
|
version: "",
|
||||||
},
|
},
|
||||||
@ -143,7 +143,7 @@ func TestParseArgument(t *testing.T) {
|
|||||||
name: "Custom git provider with custom port, folder, and version",
|
name: "Custom git provider with custom port, folder, and version",
|
||||||
in: "git.blocklayer.com:7999/ops/dagger.git/stuff/here@v5",
|
in: "git.blocklayer.com:7999/ops/dagger.git/stuff/here@v5",
|
||||||
want: &Require{
|
want: &Require{
|
||||||
repo: "git.blocklayer.com:7999/ops/dagger.git",
|
repo: "git.blocklayer.com:7999/ops/dagger",
|
||||||
path: "/stuff/here",
|
path: "/stuff/here",
|
||||||
version: "v5",
|
version: "v5",
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user