From b3f04d145209df7049de20cb0f8d600cedd03a1b Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Wed, 17 May 2023 14:19:59 -0400 Subject: [PATCH] Upgrading to a walkfunc --- cmd/obp/cmd/validate.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/obp/cmd/validate.go b/cmd/obp/cmd/validate.go index 3ca0cf8..f3c5ff1 100644 --- a/cmd/obp/cmd/validate.go +++ b/cmd/obp/cmd/validate.go @@ -5,6 +5,9 @@ package cmd import ( "fmt" + "io/fs" + "log" + "os" _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" "github.com/spf13/cobra" @@ -29,12 +32,30 @@ var validateCmd = &cobra.Command{ if len(target) == 0 { return fmt.Errorf("Please profide a target filename") } + root := os.DirFS(target) + _, err := root.Open(".") + if err != nil { + return fmt.Errorf("cannot open target directory %q: %w", target, err) + } return nil }, RunE: func(cmd *cobra.Command, args []string) error { schema := cmd.Flag("schema").Value.String() target := cmd.Flag("target").Value.String() - return obp.Validate(schema, target) + root := os.DirFS(target) + return fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if d.IsDir() { + return nil + } + + log.Printf("scanning %q\n", path) + return obp.Validate(schema, target) + }) + // return obp.Validate(schema, target) }, } @@ -43,6 +64,6 @@ func init() { // when this action is called directly. // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") validateCmd.Flags().StringP("schema", "s", "base.schema", "path to protobuf file") - validateCmd.Flags().StringP("target", "t", "", "list of filenames to validate") + validateCmd.Flags().StringP("target", "t", "", "directory containing validation targets") rootCmd.AddCommand(validateCmd) }