github.com/blend/go-sdk@v1.20220411.3/sourceutil/find_all_files.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package sourceutil
     9  
    10  import (
    11  	"context"
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  )
    16  
    17  // FindAllFiles finds all the files that match a given glob recursively.
    18  func FindAllFiles(ctx context.Context, startPath, matchGlob string) (output []string, err error) {
    19  	err = filepath.Walk(startPath, func(path string, info os.FileInfo, walkErr error) error {
    20  		if walkErr != nil {
    21  			return walkErr
    22  		}
    23  		if info.IsDir() {
    24  			if path == startPath {
    25  				return nil
    26  			}
    27  			if strings.HasPrefix(info.Name(), "_") {
    28  				return filepath.SkipDir
    29  			}
    30  			if strings.HasPrefix(info.Name(), ".") {
    31  				return filepath.SkipDir
    32  			}
    33  			if info.Name() == "node_modules" {
    34  				return filepath.SkipDir
    35  			}
    36  			if strings.HasPrefix(path, "vendor/") {
    37  				return filepath.SkipDir
    38  			}
    39  			return nil
    40  		}
    41  
    42  		matched, err := filepath.Match(matchGlob, info.Name())
    43  		if err != nil {
    44  			return err
    45  		}
    46  		if matched {
    47  			if !strings.HasPrefix(path, "./") {
    48  				path = "./" + path
    49  			}
    50  			output = append(output, path)
    51  		}
    52  		return nil
    53  	})
    54  	return
    55  }