...
1
15
16 package repo
17
18 import (
19 "errors"
20 "fmt"
21 "os"
22
23 "github.com/bazelbuild/bazel-gazelle/pathtools"
24 "golang.org/x/tools/go/vcs"
25 )
26
27 func NewStubRemoteCache(rs []Repo) *RemoteCache {
28 rc, _ := NewRemoteCache(rs)
29 rc.tmpDir = os.DevNull
30 rc.tmpErr = errors.New("stub remote cache cannot use temp dir")
31 rc.RepoRootForImportPath = stubRepoRootForImportPath
32 rc.HeadCmd = stubHeadCmd
33 rc.ModInfo = stubModInfo
34 rc.ModVersionInfo = stubModVersionInfo
35 return rc
36 }
37
38
39 func stubRepoRootForImportPath(importPath string, verbose bool) (*vcs.RepoRoot, error) {
40 if pathtools.HasPrefix(importPath, "example.com/repo.git") {
41 return &vcs.RepoRoot{
42 VCS: vcs.ByCmd("git"),
43 Repo: "https://example.com/repo.git",
44 Root: "example.com/repo.git",
45 }, nil
46 }
47
48 if pathtools.HasPrefix(importPath, "example.com/repo") {
49 return &vcs.RepoRoot{
50 VCS: vcs.ByCmd("git"),
51 Repo: "https://example.com/repo.git",
52 Root: "example.com/repo",
53 }, nil
54 }
55
56 if pathtools.HasPrefix(importPath, "example.com") {
57 return &vcs.RepoRoot{
58 VCS: vcs.ByCmd("git"),
59 Repo: "https://example.com",
60 Root: "example.com",
61 }, nil
62 }
63
64 return nil, fmt.Errorf("could not resolve import path: %q", importPath)
65 }
66
67 func stubHeadCmd(remote, vcs string) (string, error) {
68 if vcs == "git" && remote == "https://example.com/repo" {
69 return "abcdef", nil
70 }
71 return "", fmt.Errorf("could not resolve remote: %q", remote)
72 }
73
74 func stubModInfo(importPath string) (string, error) {
75 if pathtools.HasPrefix(importPath, "example.com/stub/v2") {
76 return "example.com/stub/v2", nil
77 }
78 if pathtools.HasPrefix(importPath, "example.com/stub") {
79 return "example.com/stub", nil
80 }
81 return "", fmt.Errorf("could not find module path for %s", importPath)
82 }
83
84 func stubModVersionInfo(modPath, query string) (version, sum string, err error) {
85 if modPath == "example.com/known" || modPath == "example.com/unknown" {
86 return "v1.2.3", "h1:abcdef", nil
87 }
88 return "", "", fmt.Errorf("no such module: %s", modPath)
89 }
90
View as plain text