-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
cacheInfo.go
61 lines (54 loc) · 1.1 KB
/
cacheInfo.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
package models
import (
"encoding/json"
"github.com/hashload/boss/env"
"github.com/hashload/boss/msg"
"io/ioutil"
"os"
"path/filepath"
"time"
)
type RepoInfo struct {
Key string `json:"key"`
LastUpdate time.Time `json:"last_update"`
}
func SaveRepoData(key string) {
location := env.GetCacheDir()
data := &RepoInfo{}
data.Key = key
data.LastUpdate = time.Now()
d, err := json.Marshal(data)
if err != nil {
msg.Err(err.Error())
}
pp := filepath.Join(location, "info")
err = os.MkdirAll(pp, 0755)
if err != nil {
msg.Err(err.Error())
}
p := filepath.Join(pp, key+".json")
f, err := os.Create(p)
if err != nil {
msg.Err(err.Error())
}
defer f.Close()
_, err = f.Write(d)
if err != nil {
msg.Err(err.Error())
}
}
// RepoData retrieves cached information about a repo.
func RepoData(key string) (*RepoInfo, error) {
location := env.GetCacheDir()
c := &RepoInfo{}
p := filepath.Join(location, "info", key+".json")
f, err := ioutil.ReadFile(p)
if err != nil {
return &RepoInfo{}, err
}
err = json.Unmarshal(f, c)
if err != nil {
return &RepoInfo{}, err
}
return c, nil
}