From 86fc64e9e2918392cb85a9ee3a1720fed774834e Mon Sep 17 00:00:00 2001 From: Yoofi Quansah Date: Sat, 9 Mar 2024 12:45:06 -0600 Subject: [PATCH] chore: add billyFS implementation --- validation/validate.go | 61 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/validation/validate.go b/validation/validate.go index cbfdd6ee1a..44a69477b4 100644 --- a/validation/validate.go +++ b/validation/validate.go @@ -13,6 +13,8 @@ import ( "cuelang.org/go/cue/cuecontext" cueerrors "cuelang.org/go/cue/errors" "cuelang.org/go/encoding/yaml" + "github.com/go-git/go-billy/v5" + "github.com/go-git/go-billy/v5/util" "github.com/gobwas/glob" goyaml "gopkg.in/yaml.v3" ) @@ -212,8 +214,63 @@ func (v FeaturesValidator) Validate(file string, reader io.Reader) error { return nil } +// ValidateFilesFromBillyFS will walk an billy.Filesystem and find relevant files to +// validate. +func (v FeaturesValidator) ValidateFilesFromBillyFS(src billy.Filesystem) error { + idx, err := newFliptIndex(defaultFliptIndex()) + if err != nil { + return err + } + + fi, err := src.Open(IndexFileName) + if err != nil { + if !errors.Is(err, fs.ErrNotExist) { + return err + } + } else { + parsed, err := parseFliptIndex(fi) + if err != nil { + return err + } + + idx, err = newFliptIndex(parsed) + if err != nil { + return err + } + defer fi.Close() + } + + if err := util.Walk(src, ".", func(path string, fi fs.FileInfo, err error) error { + if err != nil { + return err + } + if fi.IsDir() { + return nil + } + + if idx.match(path) { + fi, err := src.Open(path) + if err != nil { + return err + } + + err = v.Validate(path, fi) + if err != nil { + return err + } + } + + return nil + + }); err != nil { + return err + } + + return nil +} + // ValidateFilesFromDir will walk an FS and find relevant files to -// validate from the cue validation standpoint. +// validate. func (v FeaturesValidator) ValidateFilesFromDir(src fs.FS) error { idx, err := openFliptIndex(src) if err != nil { @@ -308,7 +365,7 @@ func parseFliptIndex(r io.Reader) (fliptIndexSource, error) { return idx, nil } -// Match returns true if the path should be kept because it matches +// match returns true if the path should be kept because it matches // the underlying index filter func (i *fliptIndex) match(path string) bool { for _, include := range i.includes {