woodpecker/vendor/github.com/daixiang0/gci/pkg/io/file.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

59 lines
1.3 KiB
Go

package io
import "io/ioutil"
// FileObj allows mocking the access to files
type FileObj interface {
Load() ([]byte, error)
Path() string
}
// File represents a file that can be loaded from the file system
type File struct {
FilePath string
}
func (f File) Path() string {
return f.FilePath
}
func (f File) Load() ([]byte, error) {
return ioutil.ReadFile(f.FilePath)
}
// FileGeneratorFunc returns a list of files that can be loaded and processed
type FileGeneratorFunc func() ([]FileObj, error)
func (a FileGeneratorFunc) Combine(b FileGeneratorFunc) FileGeneratorFunc {
return func() ([]FileObj, error) {
files, err := a()
if err != nil {
return nil, err
}
additionalFiles, err := b()
if err != nil {
return nil, err
}
files = append(files, additionalFiles...)
return files, err
}
}
func GoFilesInPathsGenerator(paths []string) FileGeneratorFunc {
return FilesInPathsGenerator(paths, isGoFile)
}
func FilesInPathsGenerator(paths []string, fileCheckFun fileCheckFunction) FileGeneratorFunc {
return func() (foundFiles []FileObj, err error) {
for _, path := range paths {
files, err := FindFilesForPath(path, fileCheckFun)
if err != nil {
return nil, err
}
for _, filePath := range files {
foundFiles = append(foundFiles, File{filePath})
}
}
return foundFiles, nil
}
}