...
1 package location
2
3 import (
4 "os"
5 "path/filepath"
6
7 "github.com/redhat-developer/odo/pkg/testingutil/filesystem"
8 "github.com/redhat-developer/odo/pkg/util"
9 )
10
11
12 var possibleDevfileNames = [...]string{"devfile.yaml", ".devfile.yaml", "devfile.yml", ".devfile.yml"}
13
14
15
16 func DevfileFilenamesProvider(fsys filesystem.Filesystem, contextDir string) string {
17 for _, devFile := range possibleDevfileNames {
18 if util.CheckPathExists(fsys, filepath.Join(contextDir, devFile)) {
19 return devFile
20 }
21 }
22 return "devfile.yaml"
23 }
24
25
26
27
28 func DevfileLocation(fsys filesystem.Filesystem, contextDir string) string {
29 if contextDir == "" {
30 contextDir = "./"
31 }
32 devFile := DevfileFilenamesProvider(fsys, contextDir)
33 return filepath.Join(contextDir, devFile)
34 }
35
36
37 func IsDevfileName(name string) bool {
38 for _, devFile := range possibleDevfileNames {
39 if devFile == name {
40 return true
41 }
42 }
43 return false
44 }
45
46
47 func DirectoryContainsDevfile(fsys filesystem.Filesystem, dir string) (bool, error) {
48 for _, devFile := range possibleDevfileNames {
49 _, err := fsys.Stat(filepath.Join(dir, devFile))
50 if os.IsNotExist(err) {
51 continue
52 } else if err != nil {
53 return false, err
54 }
55
56 return true, nil
57 }
58 return false, nil
59 }
60
61
62 func DirIsEmpty(fsys filesystem.Filesystem, path string) (bool, error) {
63 files, err := fsys.ReadDir(path)
64 if err != nil {
65 return false, err
66 }
67 return len(files) == 0, nil
68 }
69
70
71
72 func DirContainsOnlyDevfile(fsys filesystem.Filesystem, path string) (bool, error) {
73 files, err := fsys.ReadDir(path)
74 if err != nil {
75 return false, err
76 }
77 return len(files) == 1 && IsDevfileName(files[0].Name()), nil
78 }
79
View as plain text