Skip to content

Commit

Permalink
Merge pull request #18 from Peefy/enhance-vet-cli-input
Browse files Browse the repository at this point in the history
feat: enhance vet cli with file pattern and stdin features
  • Loading branch information
Peefy authored Nov 28, 2023
2 parents e90ae43 + 343d5e9 commit a85e70e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 67 deletions.
66 changes: 50 additions & 16 deletions cmd/kcl/commands/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
`
)

Expand All @@ -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,
}
Expand All @@ -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
}
51 changes: 0 additions & 51 deletions pkg/fs/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
58 changes: 58 additions & 0 deletions pkg/fs/util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a85e70e

Please sign in to comment.