-
Notifications
You must be signed in to change notification settings - Fork 927
/
manifest_disk_repository.go
123 lines (99 loc) · 2.45 KB
/
manifest_disk_repository.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
package manifest
import (
"errors"
"generic"
"github.com/cloudfoundry/gamble"
"io"
"io/ioutil"
"os"
"path/filepath"
)
type ManifestRepository interface {
ReadManifest(string) (manifest *Manifest, path string, errors ManifestErrors)
}
type ManifestDiskRepository struct{}
func NewManifestDiskRepository() (repo ManifestRepository) {
return ManifestDiskRepository{}
}
func (repo ManifestDiskRepository) ReadManifest(inputPath string) (m *Manifest, manifestPath string, errs ManifestErrors) {
m = NewEmptyManifest()
basePath, fileName, err := repo.manifestPath(inputPath)
if err != nil {
errs = append(errs, errors.New("Error finding manifest: "+err.Error()))
return
}
manifestPath = filepath.Join(basePath, fileName)
mapp, err := repo.readAllYAMLFiles(manifestPath)
if err != nil {
errs = append(errs, err)
return
}
m, errs = NewManifest(basePath, mapp)
if !errs.Empty() {
return
}
return
}
func (repo ManifestDiskRepository) readAllYAMLFiles(path string) (mergedMap generic.Map, err error) {
file, err := os.Open(filepath.Clean(path))
if err != nil {
return
}
defer file.Close()
mapp, err := parseManifest(file)
if err != nil {
return
}
if !mapp.Has("inherit") {
mergedMap = mapp
return
}
inheritedPath, ok := mapp.Get("inherit").(string)
if !ok {
err = errors.New("invalid inherit path in manifest")
return
}
if !filepath.IsAbs(inheritedPath) {
inheritedPath = filepath.Join(filepath.Dir(path), inheritedPath)
}
inheritedMap, err := repo.readAllYAMLFiles(inheritedPath)
if err != nil {
return
}
mergedMap = generic.DeepMerge(inheritedMap, mapp)
return
}
func parseManifest(file io.Reader) (yamlMap generic.Map, err error) {
yamlBytes, err := ioutil.ReadAll(file)
if err != nil {
return
}
document, err := gamble.Parse(string(yamlBytes))
if err != nil {
return
}
if !generic.IsMappable(document) {
err = errors.New("Invalid manifest. Expected a map")
return
}
yamlMap = generic.NewMap(document)
return
}
func (repo ManifestDiskRepository) manifestPath(userSpecifiedPath string) (manifestDir, manifestFilename string, err error) {
fileInfo, err := os.Stat(userSpecifiedPath)
if err != nil {
return
}
if fileInfo.IsDir() {
manifestDir = userSpecifiedPath
manifestFilename = "manifest.yml"
_, err = os.Stat(filepath.Join(manifestDir, manifestFilename))
if err != nil {
return
}
} else {
manifestDir = filepath.Dir(userSpecifiedPath)
manifestFilename = fileInfo.Name()
}
return
}