-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodcache.go
More file actions
47 lines (42 loc) · 1.02 KB
/
modcache.go
File metadata and controls
47 lines (42 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package mod
import (
"fmt"
"os"
"path/filepath"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
)
// Cache for faster subsequent requests
var modCacheDir string
// getModDir returns the module cache directory
func getModCacheDir() string {
if modCacheDir != "" {
return modCacheDir
}
env := os.Getenv("GOMODCACHE")
if env != "" {
modCacheDir = env
return env
}
modCacheDir = filepath.Join(GOPATH, "pkg", "mod")
return modCacheDir
}
// getModuleDir returns an absolute path to the required module.
func getModuleDir(modulePath, version string) (string, error) {
enc, err := module.EscapePath(modulePath)
if err != nil {
return "", err
}
if !semver.IsValid(version) {
return "", fmt.Errorf("non-semver module version %q", version)
}
if module.CanonicalVersion(version) != version {
return "", fmt.Errorf("non-canonical module version %q", version)
}
encVer, err := module.EscapeVersion(version)
if err != nil {
return "", err
}
dir := filepath.Join(getModCacheDir(), enc+"@"+encVer)
return dir, nil
}