...

Source file src/github.com/redhat-developer/odo/pkg/devfile/location/location.go

Documentation: github.com/redhat-developer/odo/pkg/devfile/location

     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  // possibleDevfileNames contains possible devfile name that should be checked in the context dir.
    12  var possibleDevfileNames = [...]string{"devfile.yaml", ".devfile.yaml", "devfile.yml", ".devfile.yml"}
    13  
    14  // DevfileFilenamesProvider checks if the context dir contains devfile with possible names from possibleDevfileNames variable,
    15  // else it returns "devfile.yaml" as default value.
    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  // DevfileLocation returns path to devfile File with approprite devfile File name from possibleDevfileNames variable,
    26  // if contextDir value is provided as argument then DevfileLocation returns devfile path
    27  // else it assumes current dir as contextDir and returns appropriate value.
    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  // IsDevfileName returns true if name is a supported name for a devfile
    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  // DirectoryContainsDevfile returns true if the given directory contains a devfile with a supported name
    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  		// path to file does exist
    56  		return true, nil
    57  	}
    58  	return false, nil
    59  }
    60  
    61  // DirIsEmpty returns true if the given directory contains no file
    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  // DirContainsOnlyDevfile returns true if the directory contains only one file which is a devfile
    71  // with a supported name
    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