github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/manifestparser/locator.go (about)

     1  package manifestparser
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  )
     7  
     8  type Locator struct {
     9  	FilesToCheckFor []string
    10  }
    11  
    12  func NewLocator() *Locator {
    13  	return &Locator{
    14  		FilesToCheckFor: []string{
    15  			"manifest.yml",
    16  			"manifest.yaml",
    17  		},
    18  	}
    19  }
    20  
    21  func (loc Locator) Path(filepathOrDirectory string) (string, bool, error) {
    22  	info, err := os.Stat(filepathOrDirectory)
    23  	if os.IsNotExist(err) {
    24  		return "", false, nil
    25  	} else if err != nil {
    26  		return "", false, err
    27  	}
    28  
    29  	resolvedFilepathOrDirectory, err := filepath.EvalSymlinks(filepathOrDirectory)
    30  	if err != nil {
    31  		return "", false, err
    32  	}
    33  
    34  	if info.IsDir() {
    35  		return loc.handleDir(resolvedFilepathOrDirectory)
    36  	}
    37  
    38  	return loc.handleFilepath(resolvedFilepathOrDirectory)
    39  }
    40  
    41  func (loc Locator) handleDir(dir string) (string, bool, error) {
    42  	for _, filename := range loc.FilesToCheckFor {
    43  		fullPath := filepath.Join(dir, filename)
    44  		if _, err := os.Stat(fullPath); err == nil {
    45  			return fullPath, true, nil
    46  		} else if !os.IsNotExist(err) {
    47  			return "", false, err
    48  		}
    49  	}
    50  
    51  	return "", false, nil
    52  }
    53  
    54  func (Locator) handleFilepath(filepath string) (string, bool, error) {
    55  	return filepath, true, nil
    56  }