@@ -17,15 +17,11 @@ limitations under the License.
17
17
package userconfig
18
18
19
19
import (
20
- "fmt"
21
- "io/ioutil"
22
-
23
20
"github.com/cortexlabs/cortex/pkg/lib/cast"
24
21
"github.com/cortexlabs/cortex/pkg/lib/configreader"
25
22
cr "github.com/cortexlabs/cortex/pkg/lib/configreader"
26
23
"github.com/cortexlabs/cortex/pkg/lib/errors"
27
24
"github.com/cortexlabs/cortex/pkg/lib/files"
28
- s "github.com/cortexlabs/cortex/pkg/lib/strings"
29
25
"github.com/cortexlabs/cortex/pkg/operator/api/resource"
30
26
)
31
27
@@ -39,25 +35,11 @@ var typeFieldValidation = &cr.StructFieldValidation{
39
35
Nil : true ,
40
36
}
41
37
42
- func mergeConfigs (target * Config , source * Config ) error {
43
- target .APIs = append (target .APIs , source .APIs ... )
44
-
45
- if source .App != nil {
46
- if target .App != nil {
47
- return ErrorDuplicateConfig (resource .AppType )
48
- }
49
- target .App = source .App
38
+ func (config * Config ) Validate () error {
39
+ if err := config .App .Validate (); err != nil {
40
+ return err
50
41
}
51
42
52
- return nil
53
- }
54
-
55
- func (config * Config ) ValidatePartial () error {
56
- if config .App != nil {
57
- if err := config .App .Validate (); err != nil {
58
- return err
59
- }
60
- }
61
43
if config .APIs != nil {
62
44
if err := config .APIs .Validate (); err != nil {
63
45
return err
@@ -67,33 +49,14 @@ func (config *Config) ValidatePartial() error {
67
49
return nil
68
50
}
69
51
70
- func (config * Config ) Validate () error {
71
- if config .App == nil {
72
- return ErrorMissingAppDefinition ()
73
- }
74
-
75
- return nil
76
- }
77
-
78
- func (config * Config ) MergeBytes (configBytes []byte , filePath string ) (* Config , error ) {
79
- sliceData , err := cr .ReadYAMLBytes (configBytes )
80
- if err != nil {
81
- return nil , errors .Wrap (err , filePath )
82
- }
83
-
84
- subConfig , err := newPartial (sliceData , filePath )
85
- if err != nil {
86
- return nil , err
87
- }
52
+ func New (filePath string , configBytes []byte , validate bool ) (* Config , error ) {
53
+ var err error
88
54
89
- err = mergeConfigs ( config , subConfig )
55
+ configData , err := cr . ReadYAMLBytes ( configBytes )
90
56
if err != nil {
91
- return nil , err
57
+ return nil , errors . Wrap ( err , filePath , ErrorParseConfig (). Error ())
92
58
}
93
- return config , nil
94
- }
95
59
96
- func newPartial (configData interface {}, filePath string ) (* Config , error ) {
97
60
configDataSlice , ok := cast .InterfaceToStrInterfaceMapSlice (configData )
98
61
if ! ok {
99
62
return nil , errors .Wrap (ErrorMalformedConfig (), filePath )
@@ -115,6 +78,9 @@ func newPartial(configData interface{}, filePath string) (*Config, error) {
115
78
var newResource Resource
116
79
switch resourceType {
117
80
case resource .AppType :
81
+ if config .App != nil {
82
+ return nil , errors .Wrap (ErrorDuplicateConfig (resource .AppType ), filePath )
83
+ }
118
84
app := & App {}
119
85
errs = cr .Struct (app , data , appValidation )
120
86
config .App = app
@@ -139,92 +105,28 @@ func newPartial(configData interface{}, filePath string) (*Config, error) {
139
105
}
140
106
}
141
107
142
- err := config .ValidatePartial ()
143
- if err != nil {
144
- return nil , err
145
- }
146
-
147
- return config , nil
148
- }
149
-
150
- func NewPartialPath (filePath string ) (* Config , error ) {
151
- configBytes , err := ioutil .ReadFile (filePath )
152
- if err != nil {
153
- return nil , errors .Wrap (err , filePath , ErrorReadConfig ().Error ())
154
- }
155
-
156
- configData , err := cr .ReadYAMLBytes (configBytes )
157
- if err != nil {
158
- return nil , errors .Wrap (err , filePath , ErrorParseConfig ().Error ())
108
+ if config .App == nil {
109
+ return nil , ErrorMissingAppDefinition ()
159
110
}
160
- return newPartial (configData , filePath )
161
- }
162
111
163
- func New (configs map [string ][]byte ) (* Config , error ) {
164
- var err error
165
- config := & Config {}
166
- for filePath , configBytes := range configs {
167
- if ! files .IsFilePathYAML (filePath ) {
168
- continue
169
- }
170
- config , err = config .MergeBytes (configBytes , filePath )
171
- if err != nil {
112
+ if validate {
113
+ if err := config .Validate (); err != nil {
172
114
return nil , err
173
115
}
174
116
}
175
-
176
- if err := config .Validate (); err != nil {
177
- return nil , err
178
- }
179
117
return config , nil
180
118
}
181
119
182
120
func ReadAppName (filePath string , relativePath string ) (string , error ) {
183
121
configBytes , err := files .ReadFileBytes (filePath )
184
122
if err != nil {
185
- return "" , errors .Wrap (err , ErrorReadConfig ().Error (), relativePath )
186
- }
187
- configData , err := cr .ReadYAMLBytes (configBytes )
188
- if err != nil {
189
- return "" , errors .Wrap (err , ErrorParseConfig ().Error (), relativePath )
123
+ return "" , errors .Wrap (err , relativePath , ErrorReadConfig ().Error ())
190
124
}
191
- configDataSlice , ok := cast .InterfaceToStrInterfaceMapSlice (configData )
192
- if ! ok {
193
- return "" , errors .Wrap (ErrorMalformedConfig (), relativePath )
194
- }
195
-
196
- if len (configDataSlice ) == 0 {
197
- return "" , errors .Wrap (ErrorMissingAppDefinition (), relativePath )
198
- }
199
-
200
- var appName string
201
- for i , configItem := range configDataSlice {
202
- kindStr , _ := configItem [KindKey ].(string )
203
- if resource .TypeFromKindString (kindStr ) == resource .AppType {
204
- if appName != "" {
205
- return "" , errors .Wrap (ErrorDuplicateConfig (resource .AppType ), relativePath )
206
- }
207
-
208
- wrapStr := fmt .Sprintf ("%s at %s" , resource .AppType .String (), s .Index (i ))
209
125
210
- appNameInter , ok := configItem [NameKey ]
211
- if ! ok {
212
- return "" , errors .Wrap (configreader .ErrorMustBeDefined (), relativePath , wrapStr , NameKey )
213
- }
214
-
215
- appName , ok = appNameInter .(string )
216
- if ! ok {
217
- return "" , errors .Wrap (configreader .ErrorInvalidPrimitiveType (appNameInter , configreader .PrimTypeString ), relativePath , wrapStr )
218
- }
219
- if appName == "" {
220
- return "" , errors .Wrap (configreader .ErrorCannotBeEmpty (), relativePath , wrapStr )
221
- }
222
- }
223
- }
224
-
225
- if appName == "" {
226
- return "" , errors .Wrap (ErrorMissingAppDefinition (), relativePath )
126
+ config , err := New (relativePath , configBytes , false )
127
+ if err != nil {
128
+ return "" , err
227
129
}
228
130
229
- return appName , nil
131
+ return config . App . Name , nil
230
132
}
0 commit comments