Skip to content

Commit 925b82e

Browse files
committed
cmd/pkgsite: support the module cache
With the -cache flag, fetch modules from the module cache. For golang/go#47780 Change-Id: I2aa6467955cd90a80ccae559f27928cca6e06079 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/345272 Trust: Jonathan Amsterdam <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]> Reviewed-by: Julie Qiu <[email protected]>
1 parent 64e62fe commit 925b82e

File tree

7 files changed

+64
-10
lines changed

7 files changed

+64
-10
lines changed

cmd/pkgsite/main.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"fmt"
2727
"net/http"
2828
"os"
29+
"os/exec"
30+
"path/filepath"
2931
"strings"
3032
"time"
3133

@@ -47,6 +49,8 @@ var (
4749
_ = flag.String("static", "static", "path to folder containing static files served")
4850
gopathMode = flag.Bool("gopath_mode", false, "assume that local modules' paths are relative to GOPATH/src")
4951
httpAddr = flag.String("http", defaultAddr, "HTTP service address to listen for incoming requests on")
52+
useCache = flag.Bool("cache", false, "fetch from the module cache")
53+
cacheDir = flag.String("cachedir", "", "module cache directory (defaults to `go env GOMODCACHE`)")
5054
useProxy = flag.Bool("proxy", false, "fetch from GOPROXY if not found locally")
5155
)
5256

@@ -64,6 +68,23 @@ func main() {
6468
paths = []string{"."}
6569
}
6670

71+
var downloadDir string
72+
if *useCache {
73+
downloadDir = *cacheDir
74+
if downloadDir == "" {
75+
var err error
76+
downloadDir, err = defaultCacheDir()
77+
if err != nil {
78+
die("%v", err)
79+
}
80+
if downloadDir == "" {
81+
die("empty value for GOMODCACHE")
82+
}
83+
}
84+
// We actually serve from the download subdirectory.
85+
downloadDir = filepath.Join(downloadDir, "cache", "download")
86+
}
87+
6788
var prox *proxy.Client
6889
if *useProxy {
6990
fmt.Fprintf(os.Stderr, "BYPASSING LICENSE CHECKING: MAY DISPLAY NON-REDISTRIBUTABLE INFORMATION\n")
@@ -77,7 +98,7 @@ func main() {
7798
die("connecting to proxy: %s", err)
7899
}
79100
}
80-
server, err := newServer(ctx, paths, *gopathMode, prox)
101+
server, err := newServer(ctx, paths, *gopathMode, downloadDir, prox)
81102
if err != nil {
82103
die("%s", err)
83104
}
@@ -101,8 +122,11 @@ func collectPaths(args []string) []string {
101122
return paths
102123
}
103124

104-
func newServer(ctx context.Context, paths []string, gopathMode bool, prox *proxy.Client) (*frontend.Server, error) {
125+
func newServer(ctx context.Context, paths []string, gopathMode bool, downloadDir string, prox *proxy.Client) (*frontend.Server, error) {
105126
getters := buildGetters(ctx, paths, gopathMode)
127+
if downloadDir != "" {
128+
getters = append(getters, fetch.NewFSProxyModuleGetter(downloadDir))
129+
}
106130
if prox != nil {
107131
getters = append(getters, fetch.NewProxyModuleGetter(prox))
108132
}
@@ -148,3 +172,11 @@ func buildGetters(ctx context.Context, paths []string, gopathMode bool) []fetch.
148172
}
149173
return getters
150174
}
175+
176+
func defaultCacheDir() (string, error) {
177+
out, err := exec.Command("go", "env", "GOMODCACHE").CombinedOutput()
178+
if err != nil {
179+
return "", fmt.Errorf("running 'go env GOMODCACHE': %v: %s", err, out)
180+
}
181+
return strings.TrimSpace(string(out)), nil
182+
}

cmd/pkgsite/main_test.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"net/http/httptest"
1212
"path/filepath"
13+
"strings"
1314
"testing"
1415

1516
"github.com/google/go-cmp/cmp"
@@ -18,25 +19,41 @@ import (
1819

1920
func Test(t *testing.T) {
2021
repoPath := func(fn string) string { return filepath.Join("..", "..", fn) }
22+
2123
localModule := repoPath("internal/fetch/testdata/has_go_mod")
24+
cacheDir := repoPath("internal/fetch/testdata/modcache")
2225
flag.Set("static", repoPath("static"))
2326
testModules := proxytest.LoadTestModules(repoPath("internal/proxy/testdata"))
2427
prox, teardown := proxytest.SetupTestClient(t, testModules)
2528
defer teardown()
2629

27-
server, err := newServer(context.Background(), []string{localModule}, false, prox)
30+
server, err := newServer(context.Background(), []string{localModule}, false, cacheDir, prox)
2831
if err != nil {
2932
t.Fatal(err)
3033
}
3134
mux := http.NewServeMux()
3235
server.Install(mux.Handle, nil, nil)
33-
w := httptest.NewRecorder()
3436

35-
for _, url := range []string{"/example.com/testmod", "/example.com/single/pkg"} {
36-
mux.ServeHTTP(w, httptest.NewRequest("GET", url, nil))
37-
if w.Code != http.StatusOK {
38-
t.Errorf("%q: got status code = %d, want %d", url, w.Code, http.StatusOK)
39-
}
37+
for _, test := range []struct {
38+
name string
39+
url string
40+
wantInBody string
41+
}{
42+
{"local", "example.com/testmod", "There is no documentation for this package."},
43+
{"modcache", "[email protected]", "var V = 1"},
44+
{"proxy", "example.com/single/pkg", "G is new in v1.1.0"},
45+
} {
46+
t.Run(test.name, func(t *testing.T) {
47+
w := httptest.NewRecorder()
48+
mux.ServeHTTP(w, httptest.NewRequest("GET", "/"+test.url, nil))
49+
if w.Code != http.StatusOK {
50+
t.Fatalf("got status code = %d, want %d", w.Code, http.StatusOK)
51+
}
52+
body := w.Body.String()
53+
if !strings.Contains(body, test.wantInBody) {
54+
t.Fatalf("body is missing %q\n%s", test.wantInBody, body)
55+
}
56+
})
4057
}
4158
}
4259

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module exists only in this module cache.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Version":"v1.0.0","Time":"2019-03-30T17:04:38Z"}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module modcache.com
2+
3+
go 1.12
Binary file not shown.

internal/fetchdatasource/fetchdatasource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (ds *FetchDataSource) GetLatestInfo(ctx context.Context, unitPath, modulePa
251251
}
252252

253253
if latestUnitMeta == nil {
254-
latestUnitMeta, err = ds.GetUnitMeta(ctx, unitPath, internal.UnknownModulePath, version.Latest)
254+
latestUnitMeta, err = ds.GetUnitMeta(ctx, unitPath, modulePath, version.Latest)
255255
if err != nil {
256256
return latest, err
257257
}

0 commit comments

Comments
 (0)