github.com/arunkumar7540/cli@v6.45.0+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  	if info.IsDir() {
    30  		return loc.handleDir(filepathOrDirectory)
    31  	}
    32  
    33  	return loc.handleFilepath(filepathOrDirectory)
    34  }
    35  
    36  func (loc Locator) handleDir(dir string) (string, bool, error) {
    37  	for _, filename := range loc.FilesToCheckFor {
    38  		fullPath := filepath.Join(dir, filename)
    39  		if _, err := os.Stat(fullPath); err == nil {
    40  			return fullPath, true, nil
    41  		} else if !os.IsNotExist(err) {
    42  			return "", false, err
    43  		}
    44  	}
    45  
    46  	return "", false, nil
    47  }
    48  
    49  func (Locator) handleFilepath(filepath string) (string, bool, error) {
    50  	return filepath, true, nil
    51  }