forked from twitchtv/twirp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
72 lines (59 loc) · 1.13 KB
/
manifest.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
package main
import (
"encoding/json"
"os"
"path/filepath"
)
const manifestFile = "manifest.json"
type manifest map[string]string
func getManifest() manifest {
m := manifest{}
file, err := os.Open(filepath.Join(toolDirPath, manifestFile))
if err != nil {
return m
}
defer func() {
_ = file.Close()
}()
err = json.NewDecoder(file).Decode(&m)
if err != nil {
fatal("Failed to decode manifest", err)
}
return m
}
func (m manifest) write() {
f, err := os.Create(filepath.Join(toolDirPath, manifestFile))
if err != nil {
return
}
defer func() {
_ = f.Close()
}()
bytes, err := json.MarshalIndent(m, "", " ")
if err != nil {
return
}
_, _ = f.Write(bytes)
}
func (m manifest) outOfDate(ts []*tool) bool {
// Make a copy to check for elements in ts but not m
m2 := make(map[string]string)
for k, v := range m {
m2[k] = v
}
for _, t := range ts {
if v, ok := m[t.Repository]; !ok || v != t.Commit {
return true
}
delete(m2, t.Repository)
}
return len(m2) != 0
}
func (m manifest) replace(ts []*tool) {
for k := range m {
delete(m, k)
}
for _, t := range ts {
m[t.Repository] = t.Commit
}
}