-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_file.go
More file actions
145 lines (105 loc) · 3.11 KB
/
app_file.go
File metadata and controls
145 lines (105 loc) · 3.11 KB
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
package core
import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"strings"
)
const ProcyonAppFilePropertySource = "ProcyonAppFilePropertySource"
const ProcyonAppFilePrefix = "procyon"
const ProcyonAppFilePath = "test-resources/"
const ProcyonAppFileSuffix = ".yaml"
const ProcyonDefaultProfile = "default"
type AppFilePropertySource struct {
propertyMap map[string]interface{}
}
func NewAppFilePropertySource(profiles string) *AppFilePropertySource {
if profiles == "" {
profiles = ProcyonDefaultProfile
}
profileArr := strings.FieldsFunc(profiles, func(r rune) bool {
return r == ','
})
filePaths := make([]string, 0)
for _, profile := range profileArr {
path := ProcyonAppFilePath + ProcyonAppFilePrefix
if profile == ProcyonDefaultProfile {
path = path + ProcyonAppFileSuffix
} else {
path = path + "." + strings.Trim(profile, " ") + ProcyonAppFileSuffix
}
filePaths = append(filePaths, path)
}
propertyMap, err := NewAppFileParser().Parse(filePaths)
if err != nil {
panic(err)
}
propertySource := &AppFilePropertySource{
propertyMap: propertyMap,
}
return propertySource
}
func (propertySource *AppFilePropertySource) GetName() string {
return ProcyonAppFilePropertySource
}
func (propertySource *AppFilePropertySource) GetSource() interface{} {
return propertySource.propertyMap
}
func (propertySource *AppFilePropertySource) GetProperty(name string) interface{} {
if propertySource.ContainsProperty(name) {
return propertySource.propertyMap[name]
}
return nil
}
func (propertySource *AppFilePropertySource) ContainsProperty(name string) bool {
if _, ok := propertySource.propertyMap[name]; ok {
return true
}
return false
}
func (propertySource *AppFilePropertySource) GetPropertyNames() []string {
keys := make([]string, 0, len(propertySource.propertyMap))
for key, _ := range propertySource.propertyMap {
keys = append(keys, key)
}
return keys
}
type AppFileParser struct {
}
func NewAppFileParser() *AppFileParser {
return &AppFileParser{}
}
func (parser *AppFileParser) Parse(filePaths []string) (map[string]interface{}, error) {
propertyMap := make(map[string]interface{})
for _, filePath := range filePaths {
resultMap, err := parser.parseFile(filePath)
if err != nil {
return nil, err
}
propertyMap = parser.mergeFlattenMap(propertyMap, resultMap)
}
return propertyMap, nil
}
func (parser *AppFileParser) parseFile(filePath string) (map[string]interface{}, error) {
if _, err := os.Stat(filePath); err != nil {
return nil, fmt.Errorf("app file does not exist : %s", filePath)
}
data, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("could not open app file '%s', %s", filePath, err.Error())
}
propertyMap := make(map[string]interface{})
err = yaml.Unmarshal(data, propertyMap)
if err != nil {
return nil, fmt.Errorf("could not read app file '%s', %s", filePath, err.Error())
}
flattenMap := FlatMap(propertyMap)
return flattenMap, nil
}
func (parser *AppFileParser) mergeFlattenMap(map1, map2 map[string]interface{}) map[string]interface{} {
for key, value := range map2 {
map1[key] = value
}
return map1
}