forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive_reader.go
81 lines (64 loc) · 2.17 KB
/
archive_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
package job
import (
gopath "path"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
biproperty "github.com/cloudfoundry/bosh-utils/property"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshjobman "github.com/cloudfoundry/bosh-cli/release/job/manifest"
boshman "github.com/cloudfoundry/bosh-cli/release/manifest"
. "github.com/cloudfoundry/bosh-cli/release/resource"
)
type ArchiveReaderImpl struct {
extract bool
compressor boshcmd.Compressor
fs boshsys.FileSystem
}
func NewArchiveReaderImpl(
extract bool,
compressor boshcmd.Compressor,
fs boshsys.FileSystem,
) ArchiveReaderImpl {
return ArchiveReaderImpl{
extract: extract,
compressor: compressor,
fs: fs,
}
}
func (r ArchiveReaderImpl) Read(ref boshman.JobRef, path string) (*Job, error) {
job := NewJob(NewResourceWithBuiltArchive(ref.Name, ref.Fingerprint, path, ref.SHA1))
if r.extract {
extractPath, err := r.fs.TempDir("bosh-release-job")
if err != nil {
return nil, bosherr.WrapErrorf(err, "Creating temp directory to extract job '%s'", path)
}
// Used for future clean up
job.extractedPath = extractPath
job.fs = r.fs
err = r.compressor.DecompressFileToDir(path, extractPath, boshcmd.CompressorOptions{})
if err != nil {
return nil, bosherr.WrapErrorf(err, "Extracting job archive '%s'", path)
}
specPath := gopath.Join(extractPath, "job.MF")
manifest, err := boshjobman.NewManifestFromPath(specPath, r.fs)
if err != nil {
return nil, err
}
job.Templates = manifest.Templates
job.PackageNames = manifest.Packages
properties := make(map[string]PropertyDefinition, len(manifest.Properties))
for propertyName, rawPropertyDef := range manifest.Properties {
defaultValue, err := biproperty.Build(rawPropertyDef.Default)
if err != nil {
errMsg := "Parsing job '%s' property '%s' default: %#v"
return nil, bosherr.WrapErrorf(err, errMsg, job.Name(), propertyName, rawPropertyDef.Default)
}
properties[propertyName] = PropertyDefinition{
Description: rawPropertyDef.Description,
Default: defaultValue,
}
}
job.Properties = properties
}
return job, nil
}