@@ -181,7 +181,7 @@ func (p *Package) Zip(path string) error {
181
181
fPaths := []string {descriptorPath }
182
182
for _ , r := range p .resources {
183
183
for _ , p := range r .path {
184
- c , err := read (filepath .Join (r .basePath , p ))
184
+ _ , c , err := read (filepath .Join (r .basePath , p ))
185
185
if err != nil {
186
186
return err
187
187
}
@@ -293,7 +293,7 @@ func FromString(in string, basePath string, loaders ...validator.RegistryLoader)
293
293
// Load the data package descriptor from the specified URL or file path.
294
294
// If path has the ".zip" extension, it will be saved in local filesystem and decompressed before loading.
295
295
func Load (path string , loaders ... validator.RegistryLoader ) (* Package , error ) {
296
- contents , err := read (path )
296
+ localPath , contents , err := read (path )
297
297
if err != nil {
298
298
return nil , fmt .Errorf ("error reading path contents (%s): %w" , path , err )
299
299
}
@@ -305,34 +305,47 @@ func Load(path string, loaders ...validator.RegistryLoader) (*Package, error) {
305
305
if err != nil {
306
306
return nil , fmt .Errorf ("error creating temporary directory: %w" , err )
307
307
}
308
- fNames , err := unzip (path , dir )
308
+ fNames , err := unzip (localPath , dir )
309
309
if err != nil {
310
- return nil , fmt .Errorf ("error unzipping path contents (%s): %w" , path , err )
310
+ return nil , fmt .Errorf ("error unzipping path contents (%s): %w" , localPath , err )
311
311
}
312
312
if _ , ok := fNames [descriptorFileNameWithinZip ]; ok {
313
313
return Load (filepath .Join (dir , descriptorFileNameWithinZip ), loaders ... )
314
314
}
315
- return nil , fmt .Errorf ("zip file %s does not contain a file called %s" , path , descriptorFileNameWithinZip )
315
+ return nil , fmt .Errorf ("zip file %s does not contain a file called %s" , localPath , descriptorFileNameWithinZip )
316
316
}
317
317
318
- func read (path string ) ([]byte , error ) {
318
+ func read (path string ) (string , []byte , error ) {
319
319
if strings .HasPrefix (path , "http" ) {
320
320
resp , err := http .Get (path )
321
321
if err != nil {
322
- return nil , fmt .Errorf ("error performing HTTP GET(%s): %w" , path , err )
322
+ return "" , nil , fmt .Errorf ("error performing HTTP GET(%s): %w" , path , err )
323
323
}
324
324
defer resp .Body .Close ()
325
325
buf , err := ioutil .ReadAll (resp .Body )
326
326
if err != nil {
327
- return nil , fmt .Errorf ("error reading response body contents (%s): %w" , path , err )
327
+ return "" , nil , fmt .Errorf ("error reading response body contents (%s): %w" , path , err )
328
328
}
329
- return buf , nil
329
+ // Making sure zip file is materialized.
330
+ // This makes debugging easier.
331
+ localPath , err := func () (string , error ) {
332
+ f , err := ioutil .TempFile ("" , "*.zip" )
333
+ if err != nil {
334
+ return "" , fmt .Errorf ("error creating temp file to save zip (dir:%s): %w" , os .TempDir (), err )
335
+ }
336
+ defer f .Close ()
337
+ if _ , err := f .Write (buf ); err != nil {
338
+ return f .Name (), fmt .Errorf ("error writing temp file to save zip (%s): %w" , f .Name (), err )
339
+ }
340
+ return f .Name (), nil
341
+ }()
342
+ return localPath , buf , err
330
343
}
331
344
buf , err := ioutil .ReadFile (path )
332
345
if err != nil {
333
- return nil , fmt .Errorf ("error reading local file contents (%s): %w" , path , err )
346
+ return "" , nil , fmt .Errorf ("error reading local file contents (%s): %w" , path , err )
334
347
}
335
- return buf , nil
348
+ return path , buf , nil
336
349
}
337
350
338
351
func unzip (archive , basePath string ) (map [string ]struct {}, error ) {
0 commit comments