-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_state.go
71 lines (64 loc) · 1.76 KB
/
cache_state.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"path"
)
type cacheState struct {
State map[string]string `json:"state"`
}
func getStateFile(cacheLowerRootDir string) string {
return path.Join(cacheLowerRootDir, "cache-state.json")
}
func getCacheState(fileLocationDir string) (*cacheState, error) {
stateFile := getStateFile(fileLocationDir)
_, err := os.Stat(stateFile)
if err != nil {
volumes := make(map[string]string)
data := cacheState{
State: volumes,
}
fileData, err := json.Marshal(data)
if err != nil {
return &cacheState{}, err
}
return &data, ioutil.WriteFile(stateFile, fileData, 0600)
} else {
fileData, err := ioutil.ReadFile(stateFile)
if err != nil {
return &cacheState{}, err
}
var data cacheState
e := json.Unmarshal(fileData, &data)
if e != nil {
return &cacheState{}, err
}
return &data, nil
}
}
func (cacheState *cacheState) updateState(cacheLowerRootDir, newLatest string) error {
cacheState.State["latest"] = newLatest
return cacheState.save(cacheLowerRootDir)
}
func (cacheState *cacheState) getBaseBuild(cacheLowerRootDir string) (string, error) {
if baseBuild, ok := cacheState.State["latest"]; ok {
return baseBuild, nil
} else {
baseBuild := "0"
cacheState.State["latest"] = "0"
cacheState.save(cacheLowerRootDir)
if err := os.MkdirAll(path.Join(cacheLowerRootDir, baseBuild), 0755); err != nil {
return "", err
}
return baseBuild, nil
}
}
func getBasePath(jobName, buildNumber, cacheLowerRootDir string) string {
return path.Join(cacheLowerRootDir, jobName, buildNumber)
}
func (cacheState *cacheState) save(cacheLowerRootDir string) error {
stateFile := getStateFile(cacheLowerRootDir)
fileData, _ := json.Marshal(cacheState)
return ioutil.WriteFile(stateFile, fileData, 0600)
}