woodpecker/vendor/github.com/daixiang0/gci/pkg/io/search.go
6543 56a854fe14
Update deps (#789)
* update github.com/docker/cli

* update github.com/docker/distribution

* update github.com/docker/docker

* update github.com/gin-gonic/gin

* update github.com/golang-jwt/jwt/v4

* update github.com/golangci/golangci-lint

* update github.com/gorilla/securecookie

* update github.com/mattn/go-sqlite3

* update github.com/moby/moby

* update github.com/prometheus/client_golang

* update github.com/xanzy/go-gitlab
2022-02-24 17:33:24 +01:00

47 lines
1 KiB
Go

package io
import (
"io/fs"
"os"
"path/filepath"
)
type fileCheckFunction func(file os.FileInfo) bool
func FindFilesForPath(path string, fileCheckFun fileCheckFunction) ([]string, error) {
switch entry, err := os.Stat(path); {
case err != nil:
return nil, err
case entry.IsDir():
return findFilesForDirectory(path, fileCheckFun)
case fileCheckFun(entry):
return []string{filepath.Clean(path)}, nil
default:
return []string{}, nil
}
}
func findFilesForDirectory(dirPath string, fileCheckFun fileCheckFunction) ([]string, error) {
var filePaths []string
err := filepath.WalkDir(dirPath, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
file, err := entry.Info()
if err != nil {
return err
}
if !entry.IsDir() && fileCheckFun(file) {
filePaths = append(filePaths, filepath.Clean(path))
}
return nil
})
if err != nil {
return nil, err
}
return filePaths, nil
}
func isGoFile(file os.FileInfo) bool {
return !file.IsDir() && filepath.Ext(file.Name()) == ".go"
}