@@ -2,7 +2,6 @@ package server
2
2
3
3
import (
4
4
"archive/zip"
5
- "context"
6
5
"encoding/json"
7
6
"fmt"
8
7
"io"
@@ -20,10 +19,10 @@ import (
20
19
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
21
20
"github.com/devfile/library/pkg/devfile/parser"
22
21
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
22
+ "github.com/devfile/registry-support/index/generator/schema"
23
23
indexSchema "github.com/devfile/registry-support/index/generator/schema"
24
24
"github.com/devfile/registry-support/index/server/pkg/util"
25
25
"github.com/gin-gonic/gin"
26
- "github.com/hashicorp/go-getter"
27
26
"github.com/prometheus/client_golang/prometheus"
28
27
"gopkg.in/segmentio/analytics-go.v3"
29
28
)
@@ -157,11 +156,11 @@ func serveDevfileStarterProject(c *gin.Context) {
157
156
return
158
157
} else {
159
158
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
+ }
164
162
var starterProjects []v1alpha2.StarterProject
163
+ var downloadBytes []byte
165
164
166
165
if err != nil {
167
166
log .Print (err .Error ())
@@ -180,140 +179,132 @@ func serveDevfileStarterProject(c *gin.Context) {
180
179
"status" : fmt .Sprintf ("problem in reading starter project %s of devfile %s" , starterProjectName , devfileName ),
181
180
})
182
181
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
183
187
}
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
- }
214
188
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
+ }
261
213
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 )
263
234
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
278
236
}
237
+ defer srcFile .Close ()
279
238
280
- resp , err := client . Get ( starterProject . Zip . Location )
239
+ dstFile , err := zipWriter . Create ( path . Join ( "." , strings . Split ( currPath , downloadTmpLoc )[ 1 ]) )
281
240
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
289
242
}
290
- defer resp .Body .Close ()
291
243
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
300
246
}
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
306
247
}
307
248
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
+ })
309
258
return
310
259
}
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
311
305
}
312
- // *****************
313
306
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 )
317
308
}
318
309
}
319
310
0 commit comments