From 7b8e5ac5e18184765e289aeca7d0ee5576ba9512 Mon Sep 17 00:00:00 2001 From: Michael Valdron Date: Wed, 26 Jan 2022 19:21:38 -0500 Subject: [PATCH] draft proposal function source added. --- index/server/pkg/server/endpoint.go | 99 +++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/index/server/pkg/server/endpoint.go b/index/server/pkg/server/endpoint.go index db57d846..bfe25046 100644 --- a/index/server/pkg/server/endpoint.go +++ b/index/server/pkg/server/endpoint.go @@ -140,6 +140,105 @@ func serveDevfile(c *gin.Context) { func serveDevfileStarterProject(c *gin.Context) { devfileName := c.Param("devfileName") starterProjectName := c.Param("starterProjectName") + + /** source from serveDevfile **/ + devfileBytes, err := func(devfileName string) ([]byte, error) { + var index []indexSchema.Schema + bytes, err := ioutil.ReadFile(indexPath) + if err != nil { + return nil, err + } + err = json.Unmarshal(bytes, &index) + if err != nil { + return nil, err + } + + for _, devfileIndex := range index { + if devfileIndex.Name == devfileName { + var bytes []byte + if devfileIndex.Type == indexSchema.StackDevfileType { + bytes, err = pullStackFromRegistry(devfileIndex) + } else { + // Retrieve the sample devfile stored under /registry/samples/ + sampleDevfilePath := path.Join(samplesPath, devfileIndex.Name, devfileName) + if _, err = os.Stat(sampleDevfilePath); err == nil { + bytes, err = ioutil.ReadFile(sampleDevfilePath) + } + } + + return bytes, err + } + } + + return nil, nil + }(devfileName) + if err != nil { + log.Print(err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err.Error(), + "status": fmt.Sprintf("failed to pull the devfile of %s", devfileName), + }) + return + } else if err == nil && devfileBytes == nil { + c.JSON(http.StatusNotFound, gin.H{ + "status": fmt.Sprintf("the devfile of %s didn't exist", devfileName), + }) + return + } + + // devfileBytes := func(c *gin.Context, devfileName string) []byte { + // var index []indexSchema.Schema + // bytes, err := ioutil.ReadFile(indexPath) + // if err != nil { + // log.Print(err.Error()) + // c.JSON(http.StatusInternalServerError, gin.H{ + // "error": err.Error(), + // "status": fmt.Sprintf("failed to pull the devfile of %s", devfileName), + // }) + // return nil + // } + // err = json.Unmarshal(bytes, &index) + // if err != nil { + // log.Print(err.Error()) + // c.JSON(http.StatusInternalServerError, gin.H{ + // "error": err.Error(), + // "status": fmt.Sprintf("failed to pull the devfile of %s", devfileName), + // }) + // return nil + // } + // for _, devfileIndex := range index { + // if devfileIndex.Name == devfileName { + // var bytes []byte + // if devfileIndex.Type == indexSchema.StackDevfileType { + // bytes, err = pullStackFromRegistry(devfileIndex) + // } else { + // // Retrieve the sample devfile stored under /registry/samples/ + // sampleDevfilePath := path.Join(samplesPath, devfileIndex.Name, devfileName) + // if _, err = os.Stat(sampleDevfilePath); err == nil { + // bytes, err = ioutil.ReadFile(sampleDevfilePath) + // } + // } + // if err != nil { + // log.Print(err.Error()) + // c.JSON(http.StatusInternalServerError, gin.H{ + // "error": err.Error(), + // "status": fmt.Sprintf("failed to pull the devfile of %s", devfileName), + // }) + // return nil + // } + + // return bytes + // } + // } + + // c.JSON(http.StatusNotFound, gin.H{ + // "status": fmt.Sprintf("the devfile of %s didn't exist", devfileName), + // }) + + // return nil + // }(c, devfileName) + + /****/ } func serveUI(c *gin.Context) {