Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance vet cli with file pattern and stdin features #18

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}