This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve vanity URLs to proper import paths
This resolves imports like "rsc.io/pdf" to "github.com/rsc/pdf"
- Loading branch information
Showing
8 changed files
with
90 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+:\/\/`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters