Skip to content

Commit f836b98

Browse files
committed
Merge branch 'main' into API-46
2 parents 0a68ca4 + 0eccb06 commit f836b98

File tree

4 files changed

+1131
-95
lines changed

4 files changed

+1131
-95
lines changed

pkg/devfile/parser/parse.go

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -400,24 +400,14 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
400400
func parseFromRegistry(importReference v1.ImportReference, resolveCtx *resolutionContextTree, tool resolverTools) (d DevfileObj, err error) {
401401
id := importReference.Id
402402
registryURL := importReference.RegistryUrl
403-
destDir := "."
404-
405-
// contains the downloaded stack from the registry
406-
tempDir, _ := ioutil.TempDir("", "")
407-
defer os.RemoveAll(tempDir)
403+
destDir := d.Ctx.GetAbsPath()
408404

409405
if registryURL != "" {
410-
err = getStackFromRegistry(id, registryURL, tempDir, destDir)
406+
devfileContent, err := getDevfileFromRegistry(id, registryURL, importReference.Version)
411407
if err != nil {
412408
return DevfileObj{}, err
413409
}
414-
devfileContent, err := getDevfileFromDir(tempDir)
415-
if err != nil {
416-
devfileContent, err = getDevfileFromRegistry(id, registryURL, importReference.Version)
417-
if err != nil {
418-
return DevfileObj{}, err
419-
}
420-
}
410+
getStackFromRegistry(id, registryURL, destDir)
421411
d.Ctx, err = devfileCtx.NewByteContentDevfileCtx(devfileContent)
422412
if err != nil {
423413
return d, errors.Wrap(err, "failed to set devfile content from bytes")
@@ -428,14 +418,8 @@ func parseFromRegistry(importReference v1.ImportReference, resolveCtx *resolutio
428418

429419
} else if tool.registryURLs != nil {
430420
for _, registryURL := range tool.registryURLs {
431-
err = getStackFromRegistry(id, registryURL, tempDir, destDir)
432-
if err != nil {
433-
return DevfileObj{}, err
434-
}
435-
devfileContent, err := getDevfileFromDir(tempDir)
436-
if err != nil {
437-
devfileContent, err = getDevfileFromRegistry(id, registryURL, importReference.Version)
438-
}
421+
devfileContent, err := getDevfileFromRegistry(id, registryURL, importReference.Version)
422+
getStackFromRegistry(id, registryURL, destDir)
439423
if devfileContent != nil && err == nil {
440424
d.Ctx, err = devfileCtx.NewByteContentDevfileCtx(devfileContent)
441425
if err != nil {
@@ -454,24 +438,6 @@ func parseFromRegistry(importReference v1.ImportReference, resolveCtx *resolutio
454438
return DevfileObj{}, fmt.Errorf("failed to get id: %s from registry URLs provided", id)
455439
}
456440

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-
475441
func getDevfileFromRegistry(id, registryURL, version string) ([]byte, error) {
476442
if !strings.HasPrefix(registryURL, "http://") && !strings.HasPrefix(registryURL, "https://") {
477443
return nil, fmt.Errorf("the provided registryURL: %s is not a valid URL", registryURL)
@@ -482,6 +448,15 @@ func getDevfileFromRegistry(id, registryURL, version string) ([]byte, error) {
482448
return util.HTTPGetRequest(param, 0)
483449
}
484450

451+
func getStackFromRegistry(id, registryURL, destDir string) {
452+
stackDir, _ := ioutil.TempDir(os.TempDir(), fmt.Sprintf("stack-%s", id))
453+
defer os.RemoveAll(stackDir)
454+
455+
registryLibrary.PullStackFromRegistry(registryURL, id, stackDir, registryLibrary.RegistryOptions{})
456+
util.CopyAllDirFiles(stackDir, destDir)
457+
return
458+
}
459+
485460
func parseFromKubeCRD(importReference v1.ImportReference, resolveCtx *resolutionContextTree, tool resolverTools) (d DevfileObj, err error) {
486461

487462
if tool.k8sClient == nil || tool.context == nil {

pkg/devfile/parser/parse_test.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"net/http/httptest"
2121
"os"
2222
"path"
23-
"path/filepath"
2423
"reflect"
2524
"sigs.k8s.io/yaml"
2625
"strings"
@@ -4318,52 +4317,6 @@ func Test_parseFromRegistry(t *testing.T) {
43184317
}
43194318
}
43204319

4321-
func Test_getDevfileFromDir(t *testing.T) {
4322-
tempDir, err := ioutil.TempDir("", "")
4323-
if err != nil {
4324-
t.Errorf("Failed to create temp dir: %s, error: %v", tempDir, err)
4325-
}
4326-
defer os.RemoveAll(tempDir)
4327-
4328-
devfile := filepath.Join(tempDir, "devfile.yaml")
4329-
if err := ioutil.WriteFile(devfile, []byte(""), 0666); err != nil {
4330-
t.Errorf("Failed to create temp devfile, error: %v", err)
4331-
}
4332-
4333-
missingDevfileDir := "no/devfile/here"
4334-
4335-
tests := []struct {
4336-
name string
4337-
dir string
4338-
wantErr bool
4339-
}{
4340-
{
4341-
name: "should be able to get devfile from dir",
4342-
dir: tempDir,
4343-
wantErr: false,
4344-
},
4345-
{
4346-
name: "should fail if directory doesn't have devfile",
4347-
dir: missingDevfileDir,
4348-
wantErr: true,
4349-
},
4350-
}
4351-
4352-
for _, tt := range tests {
4353-
t.Run(tt.name, func(t *testing.T) {
4354-
gotErr := false
4355-
_, err = getDevfileFromDir(tt.dir)
4356-
if err != nil {
4357-
gotErr = true
4358-
}
4359-
4360-
if !reflect.DeepEqual(gotErr, tt.wantErr) {
4361-
t.Errorf("Got error: %t, want error: %t", gotErr, tt.wantErr)
4362-
}
4363-
})
4364-
}
4365-
}
4366-
43674320
func Test_parseFromKubeCRD(t *testing.T) {
43684321
const (
43694322
namespace = "default"

pkg/util/util.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ func FetchResourceQuantity(resourceType corev1.ResourceName, min string, max str
421421

422422
// CheckPathExists checks if a path exists or not
423423
func CheckPathExists(path string) bool {
424-
if _, err := os.Stat(path); !os.IsNotExist(err) {
424+
return checkPathExistsOnFS(path, filesystem.DefaultFs{})
425+
}
426+
427+
func checkPathExistsOnFS(path string, fs filesystem.Filesystem) bool {
428+
if _, err := fs.Stat(path); !os.IsNotExist(err) {
425429
// path to file does exist
426430
return true
427431
}
@@ -1156,10 +1160,14 @@ func CopyFile(srcPath string, dstPath string, info os.FileInfo) error {
11561160
}
11571161

11581162
// CopyAllDirFiles recursively copies a source directory to a destination directory
1159-
func CopyAllDirFiles(srcDir string, destDir string) error {
1163+
func CopyAllDirFiles(srcDir, destDir string) error {
1164+
return copyAllDirFilesOnFS(srcDir, destDir, filesystem.DefaultFs{})
1165+
}
1166+
1167+
func copyAllDirFilesOnFS(srcDir, destDir string, fs filesystem.Filesystem) error {
11601168
var info os.FileInfo
11611169

1162-
files, err := ioutil.ReadDir(srcDir)
1170+
files, err := fs.ReadDir(srcDir)
11631171
if err != nil {
11641172
return errors.Wrapf(err, "failed reading dir %v", srcDir)
11651173
}
@@ -1169,22 +1177,22 @@ func CopyAllDirFiles(srcDir string, destDir string) error {
11691177
destPath := path.Join(destDir, file.Name())
11701178

11711179
if file.IsDir() {
1172-
if info, err = os.Stat(srcPath); err != nil {
1180+
if info, err = fs.Stat(srcPath); err != nil {
11731181
return err
11741182
}
1175-
if err = os.MkdirAll(destPath, info.Mode()); err != nil {
1183+
if err = fs.MkdirAll(destPath, info.Mode()); err != nil {
11761184
return err
11771185
}
1178-
if err = CopyAllDirFiles(srcPath, destPath); err != nil {
1186+
if err = copyAllDirFilesOnFS(srcPath, destPath, fs); err != nil {
11791187
return err
11801188
}
11811189
} else {
11821190
if file.Name() == "devfile.yaml" {
11831191
continue
11841192
}
11851193
// Only copy files that do not exist in the destination directory
1186-
if _, err := os.Stat(destPath); errors.Is(err, os.ErrNotExist) {
1187-
if err := CopyFile(srcPath, destPath, file); err != nil {
1194+
if !checkPathExistsOnFS(destPath, fs) {
1195+
if err := copyFileOnFs(srcPath, destPath, fs); err != nil {
11881196
return errors.Wrapf(err, "failed to copy %s to %s", srcPath, destPath)
11891197
}
11901198
}
@@ -1193,6 +1201,40 @@ func CopyAllDirFiles(srcDir string, destDir string) error {
11931201
return nil
11941202
}
11951203

1204+
// copied from: https://github.com/devfile/registry-support/blob/main/index/generator/library/util.go
1205+
func copyFileOnFs(src, dst string, fs filesystem.Filesystem) error {
1206+
var err error
1207+
var srcinfo os.FileInfo
1208+
1209+
srcfd, err := fs.Open(src)
1210+
if err != nil {
1211+
return err
1212+
}
1213+
defer func() {
1214+
if e := srcfd.Close(); e != nil {
1215+
fmt.Printf("err occurred while closing file: %v", e)
1216+
}
1217+
}()
1218+
1219+
dstfd, err := fs.Create(dst)
1220+
if err != nil {
1221+
return err
1222+
}
1223+
defer func() {
1224+
if e := dstfd.Close(); e != nil {
1225+
fmt.Printf("err occurred while closing file: %v", e)
1226+
}
1227+
}()
1228+
1229+
if _, err = io.Copy(dstfd, srcfd); err != nil {
1230+
return err
1231+
}
1232+
if srcinfo, err = fs.Stat(src); err != nil {
1233+
return err
1234+
}
1235+
return fs.Chmod(dst, srcinfo.Mode())
1236+
}
1237+
11961238
// PathEqual compare the paths to determine if they are equal
11971239
func PathEqual(firstPath string, secondPath string) bool {
11981240
firstAbsPath, _ := GetAbsPath(firstPath)

0 commit comments

Comments
 (0)