forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
139 lines (112 loc) · 2.99 KB
/
resource.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package resource
import (
"fmt"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type ResourceImpl struct {
name string
fingerprint string
archivePath string
archiveSHA1 string
expectToExist bool
archive Archive
}
func NewResource(name, fp string, archive Archive) *ResourceImpl {
return &ResourceImpl{
name: name,
fingerprint: fp,
archive: archive,
}
}
func NewExistingResource(name, fp, sha1 string) *ResourceImpl {
return &ResourceImpl{
name: name,
fingerprint: fp,
archiveSHA1: sha1,
expectToExist: true,
}
}
func NewResourceWithBuiltArchive(name, fp, path, sha1 string) *ResourceImpl {
return &ResourceImpl{
name: name,
fingerprint: fp,
archivePath: path,
archiveSHA1: sha1,
expectToExist: true,
}
}
func (r *ResourceImpl) Name() string { return r.name }
func (r *ResourceImpl) Fingerprint() string { return r.fingerprint }
func (r *ResourceImpl) ArchivePath() string {
if len(r.archivePath) == 0 {
errMsg := "Internal inconsistency: Resource '%s/%s' must be found or built before getting its archive path"
panic(fmt.Sprintf(errMsg, r.name, r.fingerprint))
}
return r.archivePath
}
func (r *ResourceImpl) ArchiveSHA1() string {
if len(r.archiveSHA1) == 0 {
errMsg := "Internal inconsistency: Resource '%s/%s' must be found or built before getting its archive SHA1"
panic(fmt.Sprintf(errMsg, r.name, r.fingerprint))
}
return r.archiveSHA1
}
func (r *ResourceImpl) Build(devIndex, finalIndex ArchiveIndex) error {
if r.hasArchive() {
return nil
}
err := r.findAndAttach(devIndex, finalIndex, r.expectToExist)
if err != nil {
return err
}
if r.hasArchive() {
return nil
}
path, sha1, err := r.archive.Build(r.fingerprint)
if err != nil {
return err
}
newDevPath, newDevSHA1, err := devIndex.Add(r.name, r.fingerprint, path, sha1)
if err != nil {
return err
}
r.attachArchive(newDevPath, newDevSHA1)
return nil
}
func (r *ResourceImpl) Finalize(finalIndex ArchiveIndex) error {
finalPath, _, err := finalIndex.Find(r.name, r.fingerprint)
if err != nil {
return err
} else if len(finalPath) > 0 {
return nil
}
_, _, err = finalIndex.Add(r.name, r.fingerprint, r.ArchivePath(), r.ArchiveSHA1())
return err
}
func (r *ResourceImpl) findAndAttach(devIndex, finalIndex ArchiveIndex, errIfNotFound bool) error {
devPath, devSHA1, err := devIndex.Find(r.name, r.fingerprint)
if err != nil {
return err
} else if len(devPath) > 0 {
r.attachArchive(devPath, devSHA1)
return nil
}
finalPath, finalSHA1, err := finalIndex.Find(r.name, r.fingerprint)
if err != nil {
return err
} else if len(finalPath) > 0 {
r.attachArchive(finalPath, finalSHA1)
return nil
}
if errIfNotFound {
return bosherr.Errorf("Expected to find '%s/%s'", r.name, r.fingerprint)
}
return nil
}
func (r *ResourceImpl) attachArchive(path, sha1 string) {
r.archivePath = path
r.archiveSHA1 = sha1
}
func (r *ResourceImpl) hasArchive() bool {
return len(r.archivePath) > 0 && len(r.archiveSHA1) > 0
}