Skip to content

Commit

Permalink
Redirect to Go package docs from a real browser, for certain repos (k…
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh authored Jan 6, 2022
1 parent a14a1eb commit d67f86d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
3 changes: 2 additions & 1 deletion golang/_redirects
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@
/networking/* go-get=1 /golang/networking.html 200
/operator/* go-get=1 /golang/operator.html 200
/pkg/* go-get=1 /golang/pkg.html 200
/pkg/* https://pkg.go.dev/knative.dev/pkg/:splat
/reconciler-test/* go-get=1 /golang/reconciler-test.html 200
/release/* go-get=1 /golang/release.html 200
/sample-controller/* go-get=1 /golang/sample-controller.html 200
/sample-source/* go-get=1 /golang/sample-source.html 200
/serving/* go-get=1 /golang/serving.html 200
/serving/* https://pkg.go.dev/knative.dev/serving/:splat
/specs/* go-get=1 /golang/specs.html 200
/test-infra/* go-get=1 /golang/test-infra.html 200
/ux/* go-get=1 /golang/ux.html 200
/website/* go-get=1 /golang/website.html 200
/wg-repository/* go-get=1 /golang/wg-repository.html 200
24 changes: 23 additions & 1 deletion tools/redir-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ var (
knativeOrgs = []string{"knative", "knative-sandbox"}
allowedRepoRe = regexp.MustCompile("^[a-z][-a-z0-9]+$")
archivedExceptions = []string{"eventing-contrib"}

// Repos known to contain Go libraries, so that requesting
// https://knative.dev/<repo> in a browser should redirect to
// https://pkg.go.dev/knative.dev/<repo>
redirectingGoRepos = []string{"pkg", "serving"}
)

// repoInfo provides a simple holder for GitHub repo information needed to
Expand Down Expand Up @@ -102,6 +107,8 @@ const (
`
redirText = `/{{.Repo}}/* go-get=1 /golang/{{.Repo}}.html 200
`
godocRedirText = `/{{.Repo}}/* https://pkg.go.dev/knative.dev/{{.Repo}}/:splat
`

autogenPrefix = `
# This file is AUTO-GENERATED
Expand All @@ -125,15 +132,21 @@ func appendRedirs(ris []repoInfo) error {
return fmt.Errorf("unable to write to %q: %w", redirFilename, err)
}
for _, ri := range ris {
if redirTemplate.Execute(redirFile, ri) != nil {
if err := redirTemplate.Execute(redirFile, ri); err != nil {
return fmt.Errorf("unable to write %s to %q: %w", ri.Repo, redirFilename, err)
}
if isGoRepo(ri.Repo) {
if err := godocRedirTemplate.Execute(redirFile, ri); err != nil {
return fmt.Errorf("unable to write godoc redirect for %s to %q: %w", ri.Repo, redirFilename, err)
}
}
}
return nil
}

var fileTemplate = template.Must(template.New("gohtml").Parse(goHTML))
var redirTemplate = template.Must(template.New("redir").Parse(redirText))
var godocRedirTemplate = template.Must(template.New("redir").Parse(godocRedirText))

// createGoGetFile creates a static HTML file providing a knative.dev mapping
// for the specified org and repo.
Expand Down Expand Up @@ -171,3 +184,12 @@ func inArchivedExceptions(n string) bool {
}
return false
}

func isGoRepo(n string) bool {
for _, r := range redirectingGoRepos {
if r == n {
return true
}
}
return false
}
4 changes: 0 additions & 4 deletions tools/redir-gen/redirtemplate.gohtml

This file was deleted.

0 comments on commit d67f86d

Please sign in to comment.