github.com/blend/go-sdk@v1.20220411.3/sourceutil/find_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  )
    15  
    16  // FindFiles finds all files in a given path that matches a given glob
    17  // but does not traverse recursively.
    18  func FindFiles(ctx context.Context, targetPath string, matchGlob string) (output []string, err error) {
    19  	err = filepath.Walk(targetPath, func(path string, info os.FileInfo, walkErr error) error {
    20  		if walkErr != nil {
    21  			return walkErr
    22  		}
    23  		if info.IsDir() {
    24  			if path == targetPath {
    25  				return nil
    26  			}
    27  			return filepath.SkipDir
    28  		}
    29  		matched, err := filepath.Match(matchGlob, info.Name())
    30  		if err != nil {
    31  			return err
    32  		}
    33  		if matched {
    34  			output = append(output, path)
    35  		}
    36  		return nil
    37  	})
    38  	return
    39  }