From 343d5e9a5b5514f6b22395ea6ac6c334d6e742b9 Mon Sep 17 00:00:00 2001 From: peefy Date: Mon, 27 Nov 2023 21:22:12 +0800 Subject: [PATCH] feat: enhance vet cli with file pattern and stdin features Signed-off-by: peefy --- cmd/kcl/commands/vet.go | 66 +++++++++++++++++++++++++++++++---------- pkg/fs/pattern.go | 51 ------------------------------- pkg/fs/util.go | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 67 deletions(-) create mode 100644 pkg/fs/util.go diff --git a/cmd/kcl/commands/vet.go b/cmd/kcl/commands/vet.go index 642baf7..9164335 100644 --- a/cmd/kcl/commands/vet.go +++ b/cmd/kcl/commands/vet.go @@ -4,9 +4,11 @@ package cmd import ( "fmt" + "io" "os" "github.com/spf13/cobra" + "kcl-lang.io/cli/pkg/fs" "kcl-lang.io/kcl-go/pkg/tools/validate" ) @@ -16,6 +18,12 @@ This command validates the data file using the kcl code. ` vetExample = ` # Validate the JSON data using the kcl code kcl vet data.json code.k + + # Validate the YAML data using the kcl code + kcl vet data.yaml code.k --format yaml + + # Validate the JSON data using the kcl code with the schema name + kcl vet data.json code.k -s Schema ` ) @@ -28,22 +36,9 @@ func NewVetCmd() *cobra.Command { Long: vetDesc, Example: vetExample, RunE: func(_ *cobra.Command, args []string) error { - data, err := os.ReadFile(args[0]) - if err != nil { - return err - } - code, err := os.ReadFile(args[1]) - if err != nil { - return err - } - ok, err := validate.ValidateCode(string(data), string(code), &o) - if err != nil { - return err - } - if ok { - fmt.Println("Validate success") - } - return nil + dataFile := args[0] + codeFile := args[1] + return doValidate(dataFile, codeFile, &o) }, SilenceUsage: true, } @@ -59,3 +54,42 @@ func NewVetCmd() *cobra.Command { return cmd } + +func doValidate(dataFile, codeFile string, o *validate.ValidateOptions) error { + var ok bool + code, err := os.ReadFile(codeFile) + if err != nil { + return err + } + if dataFile == "-" { + // Read data from stdin + input, err := io.ReadAll(os.Stdin) + if err != nil { + return err + } + ok, err = validate.ValidateCode(string(input), string(code), o) + if err != nil { + return err + } + } else { + // Read data from files + dataFiles, err := fs.ExpandInputFiles([]string{dataFile}, false) + if err != nil { + return err + } + for _, dataFile := range dataFiles { + data, err := os.ReadFile(dataFile) + if err != nil { + return err + } + ok, err = validate.ValidateCode(string(data), string(code), o) + if err != nil { + return err + } + } + } + if ok { + fmt.Println("Validate success") + } + return nil +} diff --git a/pkg/fs/pattern.go b/pkg/fs/pattern.go index c6429ff..80f2d9c 100644 --- a/pkg/fs/pattern.go +++ b/pkg/fs/pattern.go @@ -39,54 +39,3 @@ func ExpandIfFilePattern(pattern string, recursive bool) ([]string, error) { } return []string{pattern}, nil } - -func GetAllFilesInFolder(folderPath string, recursive bool) ([]string, error) { - var fileList []string - - files, err := os.ReadDir(folderPath) - if err != nil { - return nil, err - } - - for _, file := range files { - if file.IsDir() && recursive { - subFolderFiles, err := GetAllFilesInFolder(filepath.Join(folderPath, file.Name()), recursive) - if err != nil { - return fileList, fmt.Errorf("error while reading files from subfolder: %s", err) - } - fileList = append(fileList, subFolderFiles...) - } else if !file.IsDir() { - fileList = append(fileList, filepath.Join(folderPath, file.Name())) - } - } - return fileList, nil -} - -func IgnoreFile(path string, extensions []string) bool { - if len(extensions) == 0 { - return false - } - ext := filepath.Ext(path) - for _, s := range extensions { - if s == ext { - return false - } - } - return true -} - -func IsDir(path string) bool { - fileInfo, err := os.Stat(path) - if err != nil { - return false - } - return fileInfo.IsDir() -} - -func FileExists(path string) bool { - fi, err := os.Lstat(path) - if err != nil || fi.IsDir() { - return false - } - return true -} diff --git a/pkg/fs/util.go b/pkg/fs/util.go new file mode 100644 index 0000000..a96761b --- /dev/null +++ b/pkg/fs/util.go @@ -0,0 +1,58 @@ +package fs + +import ( + "fmt" + "os" + "path/filepath" +) + +func GetAllFilesInFolder(folderPath string, recursive bool) ([]string, error) { + var fileList []string + + files, err := os.ReadDir(folderPath) + if err != nil { + return nil, err + } + + for _, file := range files { + if file.IsDir() && recursive { + subFolderFiles, err := GetAllFilesInFolder(filepath.Join(folderPath, file.Name()), recursive) + if err != nil { + return fileList, fmt.Errorf("error while reading files from subfolder: %s", err) + } + fileList = append(fileList, subFolderFiles...) + } else if !file.IsDir() { + fileList = append(fileList, filepath.Join(folderPath, file.Name())) + } + } + return fileList, nil +} + +func IgnoreFile(path string, extensions []string) bool { + if len(extensions) == 0 { + return false + } + ext := filepath.Ext(path) + for _, s := range extensions { + if s == ext { + return false + } + } + return true +} + +func IsDir(path string) bool { + fileInfo, err := os.Stat(path) + if err != nil { + return false + } + return fileInfo.IsDir() +} + +func FileExists(path string) bool { + fi, err := os.Lstat(path) + if err != nil || fi.IsDir() { + return false + } + return true +}