forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest_disk_repository.go
126 lines (100 loc) · 2.5 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
124
125
126
package manifest
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/generic"
"gopkg.in/yaml.v2"
)
//go:generate counterfeiter . ManifestRepository
type ManifestRepository interface {
ReadManifest(string) (*Manifest, error)
}
type ManifestDiskRepository struct{}
func NewManifestDiskRepository() (repo ManifestRepository) {
return ManifestDiskRepository{}
}
func (repo ManifestDiskRepository) ReadManifest(inputPath string) (*Manifest, error) {
m := NewEmptyManifest()
manifestPath, err := repo.manifestPath(inputPath)
if err != nil {
return m, fmt.Errorf("%s: %s", T("Error finding manifest"), err.Error())
}
m.Path = manifestPath
mapp, err := repo.readAllYAMLFiles(manifestPath)
if err != nil {
return m, err
}
m.Data = mapp
return m, nil
}
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(T("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) {
manifest, err := ioutil.ReadAll(file)
if err != nil {
return
}
mmap := make(map[interface{}]interface{})
err = yaml.Unmarshal(manifest, &mmap)
if err != nil {
return
}
if !generic.IsMappable(mmap) || len(mmap) == 0 {
err = errors.New(T("Invalid manifest. Expected a map"))
return
}
yamlMap = generic.NewMap(mmap)
return
}
func (repo ManifestDiskRepository) manifestPath(userSpecifiedPath string) (string, error) {
fileInfo, err := os.Stat(userSpecifiedPath)
if err != nil {
return "", err
}
if fileInfo.IsDir() {
manifestPaths := []string{
filepath.Join(userSpecifiedPath, "manifest.yml"),
filepath.Join(userSpecifiedPath, "manifest.yaml"),
}
var err error
for _, manifestPath := range manifestPaths {
if _, err = os.Stat(manifestPath); err == nil {
return manifestPath, err
}
}
return "", err
}
return userSpecifiedPath, nil
}