Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature mod aliases #1426

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/content/configuration/download.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ download "github.com/pkg/*" {
mode = "redirect"
downloadURL = "https://gocenter.io"
}

alias "github.com/gomods/athens-proxy" {
sourceURL = "github.com/gomods/athens"
}
```

The first two lines describe the behavior and the destination of all packages: redirect to `https://proxy.golang.org` and asynchronously persist the module to storage.
Expand Down
4 changes: 4 additions & 0 deletions download.example.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ download "github.com/pkg/*" {
mode = "redirect"
downloadURL = "https://gocenter.io"
}

alias "github.com/gomods/athens-proxy" {
sourceURL = "github.com/gomods/athens"
}
2 changes: 2 additions & 0 deletions pkg/download/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func LatestHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Hand
return
}

mod = df.Alias(mod)

info, err := dp.Latest(r.Context(), mod)
if err != nil {
severityLevel := errors.Expect(err, errors.KindNotFound)
Expand Down
2 changes: 2 additions & 0 deletions pkg/download/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func ListHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handle
return
}

mod = df.Alias(mod)

versions, err := dp.List(r.Context(), mod)
if err != nil {
severityLevel := errors.Expect(err, errors.KindNotFound)
Expand Down
22 changes: 22 additions & 0 deletions pkg/download/mode/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type DownloadFile struct {
Mode Mode `hcl:"mode"`
DownloadURL string `hcl:"downloadURL"`
Paths []*DownloadPath `hcl:"download,block"`
Aliases []*ModAlias `hcl:"alias,block"`
}

// DownloadPath represents a custom Mode for
Expand All @@ -44,6 +45,13 @@ type DownloadPath struct {
DownloadURL string `hcl:"downloadURL,optional"`
}

// ModAlias represents a custom source URL for
// a module name.
type ModAlias struct {
Name string `hcl:"name,label"`
SourceURL string `hcl:"sourceURL"`
}

// NewFile takes a mode and returns a DownloadFile.
// Mode can be one of the constants declared above
// or a custom HCL file. To pass a custom HCL file,
Expand Down Expand Up @@ -125,6 +133,20 @@ func (d *DownloadFile) Match(mod string) Mode {
return d.Mode
}

// Alias returns the SourceURL that matches the given
// module. A name is prioritized by order in
// which it appears in the HCL file, while the
// original URL will be returned if no aliases
// exist.
func (d *DownloadFile) Alias(mod string) string {
for _, p := range d.Aliases {
if p.Name == mod {
return p.SourceURL
}
}
return mod
}

// URL returns the redirect URL that applies
// to the given module. If no pattern matches,
// the top level downloadURL is returned.
Expand Down
29 changes: 24 additions & 5 deletions pkg/download/mode/mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
)

var testCases = []struct {
name string
file *DownloadFile
input string
expectedMode Mode
expectedURL string
name string
file *DownloadFile
input string
expectedMode Mode
expectedURL string
expectedSourceURL string
}{
{
name: "sync",
Expand Down Expand Up @@ -101,6 +102,18 @@ var testCases = []struct {
expectedMode: Redirect,
expectedURL: "proxy.golang.org",
},
{
name: "alias",
file: &DownloadFile{
Mode: Sync,
Aliases: []*ModAlias{
{Name: "github.com/gomods/athens-proxy", SourceURL: "github.com/gomods/athens"},
},
},
input: "github.com/gomods/athens-proxy",
expectedSourceURL: "github.com/gomods/athens",
expectedMode: Sync,
},
}

func TestMode(t *testing.T) {
Expand All @@ -114,6 +127,12 @@ func TestMode(t *testing.T) {
if givenURL != tc.expectedURL {
t.Fatalf("expected matched DownloadURL to be %q but got %q", tc.expectedURL, givenURL)
}
if tc.expectedSourceURL != "" {
givenSourceURL := tc.file.Alias(tc.input)
if givenSourceURL != tc.expectedSourceURL {
t.Fatalf("expected matched source url to be %q but got %q", tc.expectedSourceURL, givenSourceURL)
}
}
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/download/version_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func InfoHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handle
w.WriteHeader(errors.Kind(err))
return
}

mod = df.Alias(mod)

info, err := dp.Info(r.Context(), mod, ver)
if err != nil {
severityLevel := errors.Expect(err, errors.KindNotFound, errors.KindRedirect)
Expand Down
3 changes: 3 additions & 0 deletions pkg/download/version_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func ModuleHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Hand
w.WriteHeader(errors.Kind(err))
return
}

mod = df.Alias(mod)

modBts, err := dp.GoMod(r.Context(), mod, ver)
if err != nil {
severityLevel := errors.Expect(err, errors.KindNotFound, errors.KindRedirect)
Expand Down
3 changes: 3 additions & 0 deletions pkg/download/version_zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func ZipHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler
w.WriteHeader(errors.Kind(err))
return
}

mod = df.Alias(mod)

zip, err := dp.Zip(r.Context(), mod, ver)
if err != nil {
severityLevel := errors.Expect(err, errors.KindNotFound, errors.KindRedirect)
Expand Down