forked from Masterminds/glide
/
cache.go
116 lines (99 loc) · 2.36 KB
/
cache.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package repo
import (
"encoding/json"
"errors"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"time"
//"github.com/Masterminds/glide/msg"
)
var cacheEnabled = true
var errCacheDisabled = errors.New("Cache disabled")
// EnsureCacheDir Creates the $HOME/.glide/cache directory (unless home is
// specified to be different) if it does not exist.
/*
func EnsureCacheDir(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
home := p.Get("home", "").(string)
if home == "" {
cacheEnabled = false
msg.Warn("Unable to locate home directory")
return false, nil
}
err := os.MkdirAll(filepath.Join(home, "cache", "info"), os.ModeDir|os.ModePerm)
if err != nil {
cacheEnabled = false
Warn("Error creating Glide directory %s", home)
}
return false, nil
}
*/
// Pass in a repo location and get a cache key from it.
func cacheCreateKey(repo string) (string, error) {
// A url needs a scheme. A git repo such as
// git@github.com:Masterminds/cookoo.git reworked to the url parser.
c := strings.Contains(repo, "://")
if !c {
repo = "ssh://" + repo
}
u, err := url.Parse(repo)
if err != nil {
return "", err
}
if !c {
u.Scheme = ""
}
var key string
if u.Scheme != "" {
key = u.Scheme + "-"
}
if u.User != nil && u.User.Username() != "" {
key = key + u.User.Username() + "-"
}
key = key + u.Host
if u.Path != "" {
key = key + strings.Replace(u.Path, "/", "-", -1)
}
key = strings.Replace(key, ":", "-", -1)
return key, nil
}
type cacheRepoInfo struct {
DefaultBranch string `json:"default-branch"`
LastUpdate string `json:"last-update"`
}
func saveCacheRepoData(key string, data cacheRepoInfo, location string) error {
if !cacheEnabled {
return errCacheDisabled
}
data.LastUpdate = time.Now().String()
d, err := json.Marshal(data)
if err != nil {
return err
}
p := filepath.Join(location, "cache", "info", key+".json")
f, err := os.Create(p)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(d)
return err
}
func cacheRepoData(key, location string) (*cacheRepoInfo, error) {
if !cacheEnabled {
return &cacheRepoInfo{}, errCacheDisabled
}
c := &cacheRepoInfo{}
p := filepath.Join(location, "cache", "info", key+".json")
f, err := ioutil.ReadFile(p)
if err != nil {
return &cacheRepoInfo{}, err
}
err = json.Unmarshal(f, c)
if err != nil {
return &cacheRepoInfo{}, err
}
return c, nil
}