Skip to content

Commit 3f4fb80

Browse files
starter project download git source changed to use index/generator functions.
1 parent 7170750 commit 3f4fb80

File tree

1 file changed

+115
-124
lines changed

1 file changed

+115
-124
lines changed

index/server/pkg/server/endpoint.go

Lines changed: 115 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package server
22

33
import (
44
"archive/zip"
5-
"context"
65
"encoding/json"
76
"fmt"
87
"io"
@@ -20,10 +19,10 @@ import (
2019
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2120
"github.com/devfile/library/pkg/devfile/parser"
2221
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
22+
"github.com/devfile/registry-support/index/generator/schema"
2323
indexSchema "github.com/devfile/registry-support/index/generator/schema"
2424
"github.com/devfile/registry-support/index/server/pkg/util"
2525
"github.com/gin-gonic/gin"
26-
"github.com/hashicorp/go-getter"
2726
"github.com/prometheus/client_golang/prometheus"
2827
"gopkg.in/segmentio/analytics-go.v3"
2928
)
@@ -157,11 +156,11 @@ func serveDevfileStarterProject(c *gin.Context) {
157156
return
158157
} else {
159158
content, err := parser.ParseFromData(devfileBytes)
160-
filterOptions := common.DevfileOptions{}
161-
// filterOptions := common.DevfileOptions{
162-
// FilterByName: starterProjectName,
163-
// }
159+
filterOptions := common.DevfileOptions{
160+
FilterByName: starterProjectName,
161+
}
164162
var starterProjects []v1alpha2.StarterProject
163+
var downloadBytes []byte
165164

166165
if err != nil {
167166
log.Print(err.Error())
@@ -180,140 +179,132 @@ func serveDevfileStarterProject(c *gin.Context) {
180179
"status": fmt.Sprintf("problem in reading starter project %s of devfile %s", starterProjectName, devfileName),
181180
})
182181
return
182+
} else if len(starterProjects) == 0 {
183+
c.JSON(http.StatusNotFound, gin.H{
184+
"status": fmt.Sprintf("the starter project named %s does not exist in the devfile of %s", starterProjectName, devfileName),
185+
})
186+
return
183187
}
184-
// ** Temp Filter **
185-
for _, starterProject := range starterProjects {
186-
if starterProject.Name == starterProjectName {
187-
var downloadBytes []byte
188-
189-
if starterProject.Git != nil {
190-
downloadTmpLoc := path.Join("/tmp", starterProjectName)
191-
// TODO: Setup go-getter to download subDir and from other remotes
192-
client := &getter.Client{
193-
Ctx: context.Background(),
194-
Dst: downloadTmpLoc,
195-
Dir: true,
196-
Src: strings.Split(starterProject.Git.Remotes["origin"], "https://")[1],
197-
Mode: getter.ClientModeDir,
198-
Detectors: []getter.Detector{
199-
&getter.GitHubDetector{},
200-
},
201-
Getters: map[string]getter.Getter{
202-
"git": &getter.GitGetter{},
203-
},
204-
}
205-
if err := client.Get(); err != nil {
206-
log.Print(err.Error())
207-
c.JSON(http.StatusInternalServerError, gin.H{
208-
"error": err.Error(),
209-
"status": fmt.Sprintf("Problem with downloading starter project %s from location: %s",
210-
starterProjectName, client.Src),
211-
})
212-
return
213-
}
214188

215-
zipFile, err := os.Create(fmt.Sprintf("%s.zip", downloadTmpLoc))
216-
if err != nil {
217-
log.Print(err.Error())
218-
c.JSON(http.StatusInternalServerError, gin.H{
219-
"error": err.Error(),
220-
"status": fmt.Sprintf("Problem with creating starter project %s zip archive for download",
221-
starterProjectName),
222-
})
223-
return
224-
}
225-
defer zipFile.Close()
226-
227-
zipWriter := zip.NewWriter(zipFile)
228-
defer zipWriter.Close()
229-
230-
err = filepath.Walk(downloadTmpLoc, func(currPath string, info fs.FileInfo, err error) error {
231-
if err != nil {
232-
return err
233-
} else if !info.IsDir() {
234-
srcFile, err := os.Open(currPath)
235-
if err != nil {
236-
return err
237-
}
238-
defer srcFile.Close()
239-
240-
dstFile, err := zipWriter.Create(path.Join(".", strings.Split(currPath, downloadTmpLoc)[1]))
241-
if err != nil {
242-
return err
243-
}
244-
245-
if _, err := io.Copy(dstFile, srcFile); err != nil {
246-
return err
247-
}
248-
}
249-
250-
return nil
251-
})
252-
if err != nil {
253-
log.Print(err.Error())
254-
c.JSON(http.StatusInternalServerError, gin.H{
255-
"error": err.Error(),
256-
"status": fmt.Sprintf("Problem with populating starter project %s zip archive for download, see error for details",
257-
starterProjectName),
258-
})
259-
return
260-
}
189+
if starterProject := starterProjects[0]; starterProject.Git != nil {
190+
downloadTmpLoc := path.Join("/tmp", starterProjectName)
191+
gitScheme := schema.Git{
192+
Remotes: starterProject.Git.Remotes,
193+
RemoteName: "origin",
194+
SubDir: starterProject.SubDir,
195+
}
196+
197+
if starterProject.Git.CheckoutFrom != nil {
198+
gitScheme.RemoteName = starterProject.Git.CheckoutFrom.Remote
199+
gitScheme.Revision = starterProject.Git.CheckoutFrom.Revision
200+
}
201+
202+
gitScheme.Url = gitScheme.Remotes[gitScheme.RemoteName]
203+
204+
if err := library.downloadRemoteStack(&gitScheme, downloadTmpLoc, false); err != nil {
205+
log.Print(err.Error())
206+
c.JSON(http.StatusInternalServerError, gin.H{
207+
"error": err.Error(),
208+
"status": fmt.Sprintf("Problem with downloading starter project %s from location: %s",
209+
starterProjectName, gitScheme.Url),
210+
})
211+
return
212+
}
261213

262-
_, err = zipFile.Read(downloadBytes)
214+
zipFile, err := os.Create(fmt.Sprintf("%s.zip", downloadTmpLoc))
215+
if err != nil {
216+
log.Print(err.Error())
217+
c.JSON(http.StatusInternalServerError, gin.H{
218+
"error": err.Error(),
219+
"status": fmt.Sprintf("Problem with creating starter project %s zip archive for download",
220+
starterProjectName),
221+
})
222+
return
223+
}
224+
defer zipFile.Close()
225+
226+
zipWriter := zip.NewWriter(zipFile)
227+
defer zipWriter.Close()
228+
229+
err = filepath.Walk(downloadTmpLoc, func(currPath string, info fs.FileInfo, err error) error {
230+
if err != nil {
231+
return err
232+
} else if !info.IsDir() {
233+
srcFile, err := os.Open(currPath)
263234
if err != nil {
264-
log.Print(err.Error())
265-
c.JSON(http.StatusInternalServerError, gin.H{
266-
"error": err.Error(),
267-
"status": fmt.Sprintf("Problem with reading starter project %s zip archive for download",
268-
starterProjectName),
269-
})
270-
return
271-
}
272-
} else if starterProject.Zip != nil {
273-
client := http.Client{
274-
CheckRedirect: func(req *http.Request, via []*http.Request) error {
275-
req.URL.Opaque = req.URL.Path
276-
return nil
277-
},
235+
return err
278236
}
237+
defer srcFile.Close()
279238

280-
resp, err := client.Get(starterProject.Zip.Location)
239+
dstFile, err := zipWriter.Create(path.Join(".", strings.Split(currPath, downloadTmpLoc)[1]))
281240
if err != nil {
282-
log.Print(err.Error())
283-
c.JSON(http.StatusInternalServerError, gin.H{
284-
"error": err.Error(),
285-
"status": fmt.Sprintf("Problem with downloading starter project %s from location: %s",
286-
starterProjectName, starterProject.Zip.Location),
287-
})
288-
return
241+
return err
289242
}
290-
defer resp.Body.Close()
291243

292-
downloadBytes, err = ioutil.ReadAll(resp.Body)
293-
if err != nil {
294-
log.Print(err.Error())
295-
c.JSON(http.StatusInternalServerError, gin.H{
296-
"error": err.Error(),
297-
"status": fmt.Sprintf("Problem with reading downloaded starter %s", starterProjectName),
298-
})
299-
return
244+
if _, err := io.Copy(dstFile, srcFile); err != nil {
245+
return err
300246
}
301-
} else {
302-
c.JSON(http.StatusBadRequest, gin.H{
303-
"status": fmt.Sprintf("Starter project %s has no source to download from", starterProjectName),
304-
})
305-
return
306247
}
307248

308-
c.Data(http.StatusAccepted, "application/zip", downloadBytes)
249+
return nil
250+
})
251+
if err != nil {
252+
log.Print(err.Error())
253+
c.JSON(http.StatusInternalServerError, gin.H{
254+
"error": err.Error(),
255+
"status": fmt.Sprintf("Problem with populating starter project %s zip archive for download, see error for details",
256+
starterProjectName),
257+
})
309258
return
310259
}
260+
261+
_, err = zipFile.Read(downloadBytes)
262+
if err != nil {
263+
log.Print(err.Error())
264+
c.JSON(http.StatusInternalServerError, gin.H{
265+
"error": err.Error(),
266+
"status": fmt.Sprintf("Problem with reading starter project %s zip archive for download",
267+
starterProjectName),
268+
})
269+
return
270+
}
271+
} else if starterProject.Zip != nil {
272+
client := http.Client{
273+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
274+
req.URL.Opaque = req.URL.Path
275+
return nil
276+
},
277+
}
278+
279+
resp, err := client.Get(starterProject.Zip.Location)
280+
if err != nil {
281+
log.Print(err.Error())
282+
c.JSON(http.StatusInternalServerError, gin.H{
283+
"error": err.Error(),
284+
"status": fmt.Sprintf("Problem with downloading starter project %s from location: %s",
285+
starterProjectName, starterProject.Zip.Location),
286+
})
287+
return
288+
}
289+
defer resp.Body.Close()
290+
291+
downloadBytes, err = ioutil.ReadAll(resp.Body)
292+
if err != nil {
293+
log.Print(err.Error())
294+
c.JSON(http.StatusInternalServerError, gin.H{
295+
"error": err.Error(),
296+
"status": fmt.Sprintf("Problem with reading downloaded starter %s", starterProjectName),
297+
})
298+
return
299+
}
300+
} else {
301+
c.JSON(http.StatusBadRequest, gin.H{
302+
"status": fmt.Sprintf("Starter project %s has no source to download from", starterProjectName),
303+
})
304+
return
311305
}
312-
// *****************
313306

314-
c.JSON(http.StatusNotFound, gin.H{
315-
"status": fmt.Sprintf("the starter project named %s does not exist in the devfile of %s", starterProjectName, devfileName),
316-
})
307+
c.Data(http.StatusAccepted, "application/zip", downloadBytes)
317308
}
318309
}
319310

0 commit comments

Comments
 (0)