55 "encoding/json"
66 "fmt"
77 "github.com/devfile/api/v2/pkg/attributes"
8+ registryLibrary "github.com/devfile/registry-support/registry-library/library"
89 "io/ioutil"
910 "net/url"
1011 "os"
@@ -23,8 +24,6 @@ import (
2324
2425 "reflect"
2526
26- registryLibrary "github.com/devfile/registry-support/registry-library/library"
27-
2827 v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2928 apiOverride "github.com/devfile/api/v2/pkg/utils/overriding"
3029 "github.com/devfile/api/v2/pkg/validation"
@@ -402,14 +401,17 @@ func parseFromRegistry(importReference v1.ImportReference, resolveCtx *resolutio
402401 id := importReference .Id
403402 registryURL := importReference .RegistryUrl
404403 destDir := "."
405- tempDir , err := ioutil .TempDir ("" , "" )
404+
405+ // contains the downloaded stack from the registry
406+ tempDir , _ := ioutil .TempDir ("" , "" )
406407 defer os .RemoveAll (tempDir )
407408
408409 if registryURL != "" {
409- err = registryLibrary .PullStackFromRegistry (registryURL , id , tempDir , registryLibrary.RegistryOptions {})
410- err = util .CopyAllDirFiles (tempDir , destDir )
411-
412- devfileContent , err := ioutil .ReadFile (path .Join (tempDir , "devfile.yaml" ))
410+ err = getStackFromRegistry (id , registryURL , tempDir , destDir )
411+ if err != nil {
412+ return DevfileObj {}, err
413+ }
414+ devfileContent , err := getDevfileFromDir (tempDir )
413415 if err != nil {
414416 devfileContent , err = getDevfileFromRegistry (id , registryURL , importReference .Version )
415417 if err != nil {
@@ -426,10 +428,11 @@ func parseFromRegistry(importReference v1.ImportReference, resolveCtx *resolutio
426428
427429 } else if tool .registryURLs != nil {
428430 for _ , registryURL := range tool .registryURLs {
429- err = registryLibrary .PullStackFromRegistry (registryURL , id , tempDir , registryLibrary.RegistryOptions {})
430- err = util .CopyAllDirFiles (tempDir , destDir )
431-
432- devfileContent , err := ioutil .ReadFile (path .Join (tempDir , "devfile.yaml" ))
431+ err = getStackFromRegistry (id , registryURL , tempDir , destDir )
432+ if err != nil {
433+ return DevfileObj {}, err
434+ }
435+ devfileContent , err := getDevfileFromDir (tempDir )
433436 if err != nil {
434437 devfileContent , err = getDevfileFromRegistry (id , registryURL , importReference .Version )
435438 }
@@ -451,6 +454,24 @@ func parseFromRegistry(importReference v1.ImportReference, resolveCtx *resolutio
451454 return DevfileObj {}, fmt .Errorf ("failed to get id: %s from registry URLs provided" , id )
452455}
453456
457+ func getStackFromRegistry (id , registryURL , stackDir , destDir string ) error {
458+ if ! strings .HasPrefix (registryURL , "http://" ) && ! strings .HasPrefix (registryURL , "https://" ) {
459+ return fmt .Errorf ("the provided registryURL: %s is not a valid URL" , registryURL )
460+ }
461+ registryLibrary .PullStackFromRegistry (registryURL , id , stackDir , registryLibrary.RegistryOptions {})
462+ util .CopyAllDirFiles (stackDir , destDir )
463+
464+ return nil
465+ }
466+
467+ func getDevfileFromDir (dir string ) ([]byte , error ) {
468+ devfileContent , err := ioutil .ReadFile (path .Join (dir , "devfile.yaml" ))
469+ if err != nil {
470+ return nil , fmt .Errorf ("failed to get devfile from stack registry" )
471+ }
472+ return devfileContent , err
473+ }
474+
454475func getDevfileFromRegistry (id , registryURL , version string ) ([]byte , error ) {
455476 if ! strings .HasPrefix (registryURL , "http://" ) && ! strings .HasPrefix (registryURL , "https://" ) {
456477 return nil , fmt .Errorf ("the provided registryURL: %s is not a valid URL" , registryURL )
0 commit comments