github.com/andresbott/yamlfmt@v0.1.0/internal/filematch/filematch.go (about)

     1  package filematch
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/yargevad/filepathx"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  func aggregate(glob ...string) ([]string, error) {
    11  	ret := []string{}
    12  	for _, g := range glob {
    13  		got, err := find(g)
    14  		if err != nil {
    15  			return []string{}, err
    16  		}
    17  		ret = append(ret, got...)
    18  	}
    19  	return ret, nil
    20  }
    21  
    22  func find(glob string) ([]string, error) {
    23  	matches, err := filepathx.Glob(glob)
    24  	if err != nil {
    25  		return []string{}, fmt.Errorf("unable to glob files: %v", err)
    26  	}
    27  	return matches, nil
    28  }
    29  
    30  func FindFiles(glob string) ([]string, error) {
    31  
    32  	switch glob {
    33  
    34  	case "./", "":
    35  		return aggregate("./*.yaml", "./*.yml")
    36  	case "./..":
    37  		return aggregate("./**/*.yaml", "./**/*.yml")
    38  	default:
    39  		if finfo, err := os.Stat(glob); err == nil {
    40  			if finfo.IsDir() {
    41  				glob = strings.TrimSuffix(glob, "/")
    42  				if !strings.HasPrefix(glob, "./") {
    43  					glob = "./" + glob
    44  				}
    45  				return aggregate(glob+"/*.yaml", glob+"/*.yml")
    46  			}
    47  		}
    48  		return find(glob)
    49  	}
    50  
    51  }