1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-05 15:37:19 +00:00
kyverno/pkg/utils/git/git.go
Charles-Edouard Brétéché 6893842226
refactor: cli test command (#5550)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2022-12-03 19:56:09 +01:00

52 lines
1.2 KiB
Go

package git
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
)
func Clone(path string, fs billy.Filesystem, branch string) (*git.Repository, error) {
return git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
URL: path,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch)),
Progress: os.Stdout,
SingleBranch: true,
Depth: 1,
})
}
func ListFiles(fs billy.Filesystem, path string, predicate func(fs.FileInfo) bool) ([]string, error) {
path = filepath.Clean(path)
if _, err := fs.Stat(path); err != nil {
return nil, err
}
files, err := fs.ReadDir(path)
if err != nil {
return nil, err
}
var results []string
for _, file := range files {
name := filepath.Join(path, file.Name())
if file.IsDir() {
children, err := ListFiles(fs, name, predicate)
if err != nil {
return nil, err
}
results = append(results, children...)
} else if predicate(file) {
results = append(results, name)
}
}
return results, nil
}
func ListYamls(f billy.Filesystem, path string) ([]string, error) {
return ListFiles(f, path, IsYaml)
}