@@ -198,62 +198,81 @@ func validateConfig(yamlData []byte) error {
198
198
return err
199
199
}
200
200
201
- // defaults and repositories sections are mandatory
202
- if _ , ok := raw ["defaults" ]; ! ok {
203
- return fmt . Errorf ( "defaults config section is missing" )
201
+ // skip checks if ". repositories" is empty
202
+ if raw ["repositories" ] == nil {
203
+ return nil
204
204
}
205
205
206
- if _ , ok := raw ["repositories" ]; ! ok {
207
- return fmt .Errorf ("repositories config section is missing" )
208
- }
209
-
210
- // check config sections for unexpected keys
206
+ // check all root config sections for unexpected keys
211
207
allowedRepoPoolConfig := getAllowedKeys (mirror.RepoPoolConfig {})
212
208
if key := findUnexpectedKey (raw , allowedRepoPoolConfig ); key != "" {
213
209
return fmt .Errorf ("unexpected key: .%v" , key )
214
210
}
215
211
216
- // check "defaults" section
217
- defaultsMap , ok := raw ["defaults" ].(map [string ]interface {})
218
- if ! ok {
219
- return fmt .Errorf ("defaults section is missing or not valid" )
220
- }
221
- allowedDefaults := getAllowedKeys (mirror.DefaultConfig {})
212
+ // check ".defaults"
213
+ if _ , exists := raw ["defaults" ]; exists {
214
+ defaultsMap , ok := raw ["defaults" ].(map [string ]interface {})
215
+ if ! ok {
216
+ return fmt .Errorf (".defaults config is not valid" )
217
+ }
218
+ allowedDefaults := getAllowedKeys (mirror.DefaultConfig {})
222
219
223
- if key := findUnexpectedKey (defaultsMap , allowedDefaults ); key != "" {
224
- return fmt .Errorf ("unexpected key: .defaults.%v" , key )
225
- }
220
+ if key := findUnexpectedKey (defaultsMap , allowedDefaults ); key != "" {
221
+ return fmt .Errorf ("unexpected key: .defaults.%v" , key )
222
+ }
226
223
227
- // check "auth" section in "defaults"
228
- if authMap , ok := defaultsMap ["auth" ].(map [string ]interface {}); ok {
229
- allowedAuthKeys := getAllowedKeys (mirror.Auth {})
230
- if key := findUnexpectedKey (authMap , allowedAuthKeys ); key != "" {
231
- return fmt .Errorf ("unexpected key: .defaults.auth.%v" , key )
224
+ // check ".defaults.auth"
225
+ if authMap , ok := defaultsMap ["auth" ].(map [string ]interface {}); ok {
226
+ allowedAuthKeys := getAllowedKeys (mirror.Auth {})
227
+ if key := findUnexpectedKey (authMap , allowedAuthKeys ); key != "" {
228
+ return fmt .Errorf ("unexpected key: .defaults.auth.%v" , key )
229
+ }
232
230
}
233
231
}
234
232
235
- // check each repository in "repositories" section
233
+ // check ".repositories"
234
+ reposInterface , ok := raw ["repositories" ].([]interface {})
235
+ if ! ok {
236
+ return fmt .Errorf (".repositories config must be an array" )
237
+ }
238
+
239
+ // check each repository in ".repositories"
236
240
allowedRepoKeys := getAllowedKeys (mirror.RepositoryConfig {})
237
- for _ , repoInterface := range raw ["repositories" ].([]interface {}) {
241
+
242
+ for _ , repoInterface := range reposInterface {
238
243
repoMap , ok := repoInterface .(map [string ]interface {})
239
244
if ! ok {
240
- return fmt .Errorf ("repositories config section is not valid" )
245
+ return fmt .Errorf (". repositories config is not valid" )
241
246
}
242
247
243
248
if key := findUnexpectedKey (repoMap , allowedRepoKeys ); key != "" {
244
249
return fmt .Errorf ("unexpected key: .repositories[%v].%v" , repoMap ["remote" ], key )
245
250
}
246
251
247
- // check each "worktrees" section in each repository
248
- for _ , worktreeInterface := range repoMap ["worktrees" ].([] interface {}) {
249
- worktreeMap , ok := worktreeInterface .( map [ string ]interface {})
252
+ // check "worktrees" in each repository
253
+ if worktreesInterface , exists := repoMap ["worktrees" ]; exists {
254
+ _ , ok := repoMap [ "worktrees" ].([ ]interface {})
250
255
if ! ok {
251
- return fmt .Errorf ("worktrees config section is not valid in .repositories[%v]" , repoMap ["remote" ])
256
+ return fmt .Errorf ("worktrees config must be an array in .repositories[%v]" , repoMap ["remote" ])
252
257
}
253
258
254
- allowedWorktreeKeys := getAllowedKeys (mirror.WorktreeConfig {})
255
- if key := findUnexpectedKey (worktreeMap , allowedWorktreeKeys ); key != "" {
256
- return fmt .Errorf ("unexpected key: .repositories[%v].worktrees[%v].%v" , repoMap ["remote" ], worktreeMap ["link" ], key )
259
+ for _ , worktreeInterface := range worktreesInterface .([]interface {}) {
260
+ worktreeMap , ok := worktreeInterface .(map [string ]interface {})
261
+ if ! ok {
262
+ return fmt .Errorf ("worktrees config is not valid in .repositories[%v]" , repoMap ["remote" ])
263
+ }
264
+
265
+ allowedWorktreeKeys := getAllowedKeys (mirror.WorktreeConfig {})
266
+ if key := findUnexpectedKey (worktreeMap , allowedWorktreeKeys ); key != "" {
267
+ return fmt .Errorf ("unexpected key: .repositories[%v].worktrees[%v].%v" , repoMap ["remote" ], worktreeMap ["link" ], key )
268
+ }
269
+
270
+ // Check "pathspecs" in each worktree
271
+ if pathspecsInterface , exists := worktreeMap ["pathspecs" ]; exists {
272
+ if _ , ok := pathspecsInterface .([]interface {}); ! ok {
273
+ return fmt .Errorf ("pathspecs config must be an array in .repositories[%v].worktrees[%v]" , repoMap ["remote" ], worktreeMap ["link" ])
274
+ }
275
+ }
257
276
}
258
277
}
259
278
}
0 commit comments