forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_reader.go
174 lines (140 loc) · 3.87 KB
/
dir_reader.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package release
import (
gopath "path"
"sort"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshjob "github.com/cloudfoundry/bosh-cli/release/job"
boshlic "github.com/cloudfoundry/bosh-cli/release/license"
boshpkg "github.com/cloudfoundry/bosh-cli/release/pkg"
)
type DirReader struct {
jobDirReader boshjob.DirReader
pkgDirReader boshpkg.DirReader
licDirReader boshlic.DirReader
fs boshsys.FileSystem
logTag string
logger boshlog.Logger
}
func NewDirReader(
jobDirReader boshjob.DirReader,
pkgDirReader boshpkg.DirReader,
licDirReader boshlic.DirReader,
fs boshsys.FileSystem,
logger boshlog.Logger,
) DirReader {
return DirReader{
jobDirReader: jobDirReader,
pkgDirReader: pkgDirReader,
licDirReader: licDirReader,
fs: fs,
logTag: "release.DirReader",
logger: logger,
}
}
func (r DirReader) Read(path string) (Release, error) {
var packages []*boshpkg.Package
var jobs []*boshjob.Job
var errs []error
var err error
pkgMatches, err := r.fs.Glob(gopath.Join(path, "packages", "*"))
if err != nil {
errs = append(errs, bosherr.WrapError(err, "Listing packages in directory"))
} else {
packages, err = r.newPackages(pkgMatches)
if err != nil {
errs = append(errs, bosherr.WrapError(err, "Constructing packages from directory"))
}
}
jobMatches, err := r.fs.Glob(gopath.Join(path, "jobs", "*"))
if err != nil {
errs = append(errs, bosherr.WrapError(err, "Listing jobs in directory"))
} else {
jobs, err = r.newJobs(packages, jobMatches)
if err != nil {
errs = append(errs, bosherr.WrapError(err, "Constructing jobs from manifest"))
}
}
license, err := r.newLicense(path)
if err != nil {
errs = append(errs, bosherr.WrapError(err, "Constructing license from manifest"))
}
if len(errs) > 0 {
return nil, bosherr.NewMultiError(errs...)
}
release := &release{
jobs: jobs,
packages: packages,
license: license,
// no compiled packages
// no clean up
}
return release, nil
}
func (r DirReader) newJobs(packages []*boshpkg.Package, jobMatches []string) ([]*boshjob.Job, error) {
var jobs []*boshjob.Job
var errs []error
for _, jobMatch := range jobMatches {
info, err := r.fs.Stat(jobMatch)
if err != nil {
errs = append(errs, bosherr.WrapErrorf(err, "Reading job from '%s'", jobMatch))
continue
}
if info.IsDir() {
job, err := r.jobDirReader.Read(jobMatch)
if err != nil {
errs = append(errs, bosherr.WrapErrorf(err, "Reading job from '%s'", jobMatch))
continue
}
err = job.AttachPackages(packages)
if err != nil {
errs = append(errs, err)
continue
}
jobs = append(jobs, job)
}
}
if len(errs) > 0 {
return nil, bosherr.NewMultiError(errs...)
}
sort.Sort(boshjob.ByName(jobs))
return jobs, nil
}
func (r DirReader) newPackages(pkgMatches []string) ([]*boshpkg.Package, error) {
var packages []*boshpkg.Package
var errs []error
for _, pkgMatch := range pkgMatches {
info, err := r.fs.Stat(pkgMatch)
if err != nil {
errs = append(errs, bosherr.WrapErrorf(err, "Reading package from '%s'", pkgMatch))
continue
}
if info.IsDir() {
pkg, err := r.pkgDirReader.Read(pkgMatch)
if err != nil {
errs = append(errs, bosherr.WrapErrorf(err, "Reading package from '%s'", pkgMatch))
continue
}
packages = append(packages, pkg)
}
}
for _, pkg := range packages {
err := pkg.AttachDependencies(packages)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return []*boshpkg.Package{}, bosherr.NewMultiError(errs...)
}
sort.Sort(boshpkg.ByName(packages))
return packages, nil
}
func (r DirReader) newLicense(path string) (*boshlic.License, error) {
lic, err := r.licDirReader.Read(path)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Reading license from '%s'", path)
}
return lic, nil
}