lang: Add a URL result to the import name parser

This is meant to be useful for the downloader. This will probably get
more complicated over time, but for now the goal is to have it simple
enough to work for 80% of use cases.
This commit is contained in:
James Shubin
2018-11-17 21:47:11 -05:00
parent 07d57e1a64
commit 08f24fb272
2 changed files with 45 additions and 1 deletions

View File

@@ -258,6 +258,10 @@ type ImportData struct {
// fqdn of the import. // fqdn of the import.
// TODO: should system imports put something here? // TODO: should system imports put something here?
Path string Path string
// URL is the path that a `git clone` operation should use as the URL.
// If it is a local import, then this is the empty value.
URL string
} }
// ParseImportName parses an import name and returns the default namespace name // ParseImportName parses an import name and returns the default namespace name
@@ -328,11 +332,25 @@ func ParseImportName(name string) (*ImportData, error) {
xpath = xpath + "/" xpath = xpath + "/"
} }
// build a url to clone from if we're not local...
// TODO: consider adding some logic that is similar to the logic in:
// https://github.com/golang/go/blob/054640b54df68789d9df0e50575d21d9dbffe99f/src/cmd/go/internal/get/vcs.go#L972
// so that we can more correctly figure out the correct url to clone...
xurl := ""
if !local {
u.Fragment = ""
// TODO: maybe look for ?sha1=... or ?tag=... to pick a real ref
u.RawQuery = ""
u.ForceQuery = false
xurl = u.String()
}
return &ImportData{ return &ImportData{
Name: name, // save the original value here Name: name, // save the original value here
Alias: alias, Alias: alias,
System: system, System: system,
Local: local, Local: local,
Path: xpath, Path: xpath,
URL: xurl,
}, nil }, nil
} }

View File

@@ -1780,6 +1780,7 @@ func TestImportParsing0(t *testing.T) {
system bool system bool
local bool local bool
path string path string
url string
} }
testCases := []test{} testCases := []test{}
testCases = append(testCases, test{ // index: 0 testCases = append(testCases, test{ // index: 0
@@ -1795,54 +1796,66 @@ func TestImportParsing0(t *testing.T) {
alias: "mgmt", alias: "mgmt",
local: false, local: false,
path: "example.com/purpleidea/mgmt/", path: "example.com/purpleidea/mgmt/",
url: "git://example.com/purpleidea/mgmt",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/mgmt/", name: "git://example.com/purpleidea/mgmt/",
alias: "mgmt", alias: "mgmt",
local: false, local: false,
path: "example.com/purpleidea/mgmt/", path: "example.com/purpleidea/mgmt/",
url: "git://example.com/purpleidea/mgmt/",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/mgmt/foo/bar/", name: "git://example.com/purpleidea/mgmt/foo/bar/",
alias: "bar", alias: "bar",
local: false, local: false,
path: "example.com/purpleidea/mgmt/foo/bar/", path: "example.com/purpleidea/mgmt/foo/bar/",
// TODO: change this to be more clever about the clone URL
//url: "git://example.com/purpleidea/mgmt/",
// TODO: also consider changing `git` to `https` ?
url: "git://example.com/purpleidea/mgmt/foo/bar/",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/mgmt-foo", name: "git://example.com/purpleidea/mgmt-foo",
alias: "foo", // prefix is magic alias: "foo", // prefix is magic
local: false, local: false,
path: "example.com/purpleidea/mgmt-foo/", path: "example.com/purpleidea/mgmt-foo/",
url: "git://example.com/purpleidea/mgmt-foo",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/foo-bar", name: "git://example.com/purpleidea/foo-bar",
alias: "foo_bar", alias: "foo_bar",
local: false, local: false,
path: "example.com/purpleidea/foo-bar/", path: "example.com/purpleidea/foo-bar/",
url: "git://example.com/purpleidea/foo-bar",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/FOO-bar", name: "git://example.com/purpleidea/FOO-bar",
alias: "foo_bar", alias: "foo_bar",
local: false, local: false,
path: "example.com/purpleidea/FOO-bar/", path: "example.com/purpleidea/FOO-bar/",
url: "git://example.com/purpleidea/FOO-bar",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/foo-BAR", name: "git://example.com/purpleidea/foo-BAR",
alias: "foo_bar", alias: "foo_bar",
local: false, local: false,
path: "example.com/purpleidea/foo-BAR/", path: "example.com/purpleidea/foo-BAR/",
url: "git://example.com/purpleidea/foo-BAR",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/foo-BAR-baz", name: "git://example.com/purpleidea/foo-BAR-baz",
alias: "foo_bar_baz", alias: "foo_bar_baz",
local: false, local: false,
path: "example.com/purpleidea/foo-BAR-baz/", path: "example.com/purpleidea/foo-BAR-baz/",
url: "git://example.com/purpleidea/foo-BAR-baz",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/Module-Name", name: "git://example.com/purpleidea/Module-Name",
alias: "module_name", alias: "module_name",
local: false, local: false,
path: "example.com/purpleidea/Module-Name/", path: "example.com/purpleidea/Module-Name/",
url: "git://example.com/purpleidea/Module-Name",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/foo-", name: "git://example.com/purpleidea/foo-",
@@ -1871,24 +1884,29 @@ func TestImportParsing0(t *testing.T) {
alias: "module_name", alias: "module_name",
local: false, local: false,
path: "example.com/purpleidea/Module-Name/", path: "example.com/purpleidea/Module-Name/",
url: "git://example.com/purpleidea/Module-Name",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/Module-Name/?foo=bar&baz=42", name: "git://example.com/purpleidea/Module-Name/?foo=bar&baz=42",
alias: "module_name", alias: "module_name",
local: false, local: false,
path: "example.com/purpleidea/Module-Name/", path: "example.com/purpleidea/Module-Name/",
url: "git://example.com/purpleidea/Module-Name/",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/Module-Name/?sha1=25ad05cce36d55ce1c55fd7e70a3ab74e321b66e", name: "git://example.com/purpleidea/Module-Name/?sha1=25ad05cce36d55ce1c55fd7e70a3ab74e321b66e",
alias: "module_name", alias: "module_name",
local: false, local: false,
path: "example.com/purpleidea/Module-Name/", path: "example.com/purpleidea/Module-Name/",
url: "git://example.com/purpleidea/Module-Name/",
// TODO: report the query string info as an additional param
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "git://example.com/purpleidea/Module-Name/subpath/foo", name: "git://example.com/purpleidea/Module-Name/subpath/foo",
alias: "foo", alias: "foo",
local: false, local: false,
path: "example.com/purpleidea/Module-Name/subpath/foo/", path: "example.com/purpleidea/Module-Name/subpath/foo/",
url: "git://example.com/purpleidea/Module-Name/subpath/foo",
}) })
testCases = append(testCases, test{ testCases = append(testCases, test{
name: "foo/", name: "foo/",
@@ -1930,7 +1948,7 @@ func TestImportParsing0(t *testing.T) {
} }
names = append(names, tc.name) names = append(names, tc.name)
t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) { t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) {
name, fail, alias, system, local, path := tc.name, tc.fail, tc.alias, tc.system, tc.local, tc.path name, fail, alias, system, local, path, url := tc.name, tc.fail, tc.alias, tc.system, tc.local, tc.path, tc.url
output, err := ParseImportName(name) output, err := ParseImportName(name)
if !fail && err != nil { if !fail && err != nil {
@@ -1978,6 +1996,14 @@ func TestImportParsing0(t *testing.T) {
return return
} }
if url != output.URL {
t.Errorf("test #%d: unexpected value for: `URL`", index)
//t.Logf("test #%d: input: %s", index, name)
t.Logf("test #%d: output: %+v", index, output)
t.Logf("test #%d: url: %s", index, url)
return
}
}) })
} }
} }