Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Resolve vanity URLs to proper import paths
Browse files Browse the repository at this point in the history
This resolves imports like "rsc.io/pdf" to "github.com/rsc/pdf"
  • Loading branch information
mitchellh committed Nov 18, 2018
1 parent b4b520a commit a0c85d7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 9 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,3 @@ at any of these, please do and contribute!
a GitHub project changes licenses. `golicense` uses the GitHub API which only
returns the license currently detected; we can't lookup licenses for specific
commit hashes.

**Import Redirects:** Import paths that redirect to GitHub projects
(such as `gonum.org/v1/gonum`) aren't properly translated currently. To fix
this we should use the `go get` HTTP protocol to detect these and do the
proper translation. For now, you can work around this using explicit overrides
via a configuration file.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793
golang.org/x/exp v0.0.0-20181112044915-a3060d491354 // indirect
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b
gonum.org/v1/gonum v0.0.0-20181115051416-e2f95e5c31f6 // indirect
gopkg.in/neurosnap/sentences.v1 v1.0.6 // indirect
gopkg.in/russross/blackfriday.v2 v2.0.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilG
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b h1:7tibmaEqrQYA+q6ri7NQjuxqSwechjtDHKq6/e85S38=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gonum.org/v1/gonum v0.0.0-20181115051416-e2f95e5c31f6 h1:Sv4KG3MNOeKJKMiAP1fqkOfZQJoYhqGf+jgelT7jH2o=
gonum.org/v1/gonum v0.0.0-20181115051416-e2f95e5c31f6/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
Expand Down
2 changes: 1 addition & 1 deletion license/golang/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ func (t Translator) Translate(ctx context.Context, m module.Module) (module.Modu
// re is the regexp matching the package for a GoPkg import. This is taken
// almost directly from the GoPkg source code itself so it should match
// perfectly.
var re = regexp.MustCompile(`^golang\.org/x/([^/]+)$`)
var re = regexp.MustCompile(`^go\.googlesource\.com/([^/]+)$`)
4 changes: 2 additions & 2 deletions license/golang/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestTranslator(t *testing.T) {
},

{
"golang.org/x/crypto",
"github.com/golang/crypto",
"go.googlesource.com/text",
"github.com/golang/text",
},
}

Expand Down
35 changes: 35 additions & 0 deletions license/resolver/translate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package resolver

import (
"context"
"fmt"
"regexp"

"github.com/mitchellh/golicense/license"
"github.com/mitchellh/golicense/module"
"golang.org/x/tools/go/vcs"
)

// Translator resolves import paths to their proper VCS location. For
// example: "rsc.io/pdf" turns into "github.com/rsc/pdf".
type Translator struct{}

func (t Translator) Translate(ctx context.Context, m module.Module) (module.Module, bool) {
root, err := vcs.RepoRootForImportPath(m.Path, false)
if err != nil {
return module.Module{}, false
}

path := hostStripRe.ReplaceAllString(root.Repo, "")
if m.Path == path {
return module.Module{}, false
}

license.UpdateStatus(ctx, license.StatusNormal, fmt.Sprintf(
"translated %q to %q", m.Path, path))
m.Path = path
return m, true
}

// hostStripRe is a simple regexp to strip the schema from a URL.
var hostStripRe = regexp.MustCompile(`^\w+:\/\/`)
48 changes: 48 additions & 0 deletions license/resolver/translate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package resolver

import (
"context"
"testing"

"github.com/mitchellh/golicense/module"
"github.com/stretchr/testify/require"
)

func TestTranslator(t *testing.T) {
cases := []struct {
Input string
Output string
}{
{
"github.com/foo/bar",
"",
},

{
"golang.org/x/text",
"go.googlesource.com/text",
},

{
"gonum.org/v1/gonum",
"github.com/gonum/gonum",
},
}

for _, tt := range cases {
t.Run(tt.Input, func(t *testing.T) {
var tr Translator
actual, ok := tr.Translate(context.Background(), module.Module{
Path: tt.Input,
})

if tt.Output == "" {
require.False(t, ok)
return
}

require.True(t, ok)
require.Equal(t, tt.Output, actual.Path)
})
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/mitchellh/golicense/license/golang"
"github.com/mitchellh/golicense/license/gopkg"
"github.com/mitchellh/golicense/license/mapper"
"github.com/mitchellh/golicense/license/resolver"
"github.com/mitchellh/golicense/module"
)

Expand Down Expand Up @@ -131,6 +132,7 @@ func realMain() int {
// Build our translators and license finders
ts := []license.Translator{
&mapper.Translator{Map: cfg.Translate},
&resolver.Translator{},
&golang.Translator{},
&gopkg.Translator{},
}
Expand Down

0 comments on commit a0c85d7

Please sign in to comment.