github.com/jenkins-x/draft-repo@v0.9.0/pkg/draft/pack/repo/repo.go (about)

     1  package repo
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  )
     8  
     9  const PackDirName = "packs"
    10  
    11  // Repository represents a pack repository.
    12  type Repository struct {
    13  	Name string
    14  	Dir  string
    15  }
    16  
    17  // FindRepositories takes a given path and returns a list of repositories.
    18  //
    19  // Repositories are defined as directories with a "packs" directory present.
    20  func FindRepositories(path string) []Repository {
    21  	var repos []Repository
    22  	// fail fast if directory does not exist
    23  	if _, err := os.Stat(path); os.IsNotExist(err) {
    24  		return repos
    25  	}
    26  	filepath.Walk(path, func(walkPath string, f os.FileInfo, err error) error {
    27  		// find all directories in walkPath that have a child directory called "packs"
    28  		fileInfo, err := os.Stat(filepath.Join(walkPath, PackDirName))
    29  		if err != nil {
    30  			return nil
    31  		}
    32  		if fileInfo.IsDir() {
    33  			repos = append(repos, Repository{
    34  				Name: strings.TrimPrefix(walkPath, path+"/"),
    35  				Dir:  walkPath,
    36  			})
    37  		}
    38  		return nil
    39  	})
    40  	return repos
    41  }