diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/lib.go | 27 | ||||
| -rw-r--r-- | internal/lib_test.go | 23 |
2 files changed, 50 insertions, 0 deletions
diff --git a/internal/lib.go b/internal/lib.go new file mode 100644 index 0000000..742a9c7 --- /dev/null +++ b/internal/lib.go @@ -0,0 +1,27 @@ +// Copyright (c) 2019, Grégoire Duchêne <gduchene@awhk.org> +// +// Use of this source code is governed by the ISC license that can be +// found in the LICENSE file. + +package internal + +import ( + "fmt" + "os" + "strings" +) + +func GetBody(pkg string) string { + dest := GetDest(os.Getenv("PREFIX"), os.Getenv("DEST"), pkg) + return fmt.Sprintf(`<!doctype html> +<meta name="go-import" content="%s %s %s"> +<title>go-import-redirect</title> +`, pkg, os.Getenv("VCS"), dest) +} + +func GetDest(srcPrefix, destPrefix, pkg string) string { + srcPrefix = strings.TrimRight(srcPrefix, "/") + destPrefix = strings.TrimRight(destPrefix, "/") + path := strings.TrimLeft(strings.TrimPrefix(pkg, srcPrefix), "/") + return destPrefix + "/" + strings.Split(path, "/")[0] +} diff --git a/internal/lib_test.go b/internal/lib_test.go new file mode 100644 index 0000000..19a880d --- /dev/null +++ b/internal/lib_test.go @@ -0,0 +1,23 @@ +// Copyright (c) 2019, Grégoire Duchêne <gduchene@awhk.org> +// +// Use of this source code is governed by the ISC license that can be +// found in the LICENSE file. + +package internal + +import "testing" + +func TestGetDest(t *testing.T) { + cs := []struct{ srcPrefix, destPrefix, pkg, expected string }{ + {"src.example.com/x/", "https://example.com/git/", "src.example.com/x/foo", "https://example.com/git/foo"}, + {"src.example.com/x/", "https://example.com/git/", "src.example.com/x/foo/bar", "https://example.com/git/foo"}, + {"src.example.com/x", "https://example.com/git", "src.example.com/x/foo", "https://example.com/git/foo"}, + {"src.example.com/x", "https://example.com/git", "src.example.com/x/foo/bar", "https://example.com/git/foo"}, + } + for _, c := range cs { + actual := GetDest(c.srcPrefix, c.destPrefix, c.pkg) + if actual != c.expected { + t.Errorf("expected %s, got %s", c.expected, actual) + } + } +} |
