Skip to content

Commit

Permalink
Merge pull request #9 from Peefy/file-expand-for-import-tool
Browse files Browse the repository at this point in the history
feat: support file expand for the import tool
  • Loading branch information
Peefy authored Nov 7, 2023
2 parents 6b7d860 + 103be1b commit 3271e5f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 12 deletions.
11 changes: 2 additions & 9 deletions cmd/kcl/commands/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/spf13/cobra"
"kcl-lang.io/cli/pkg/fs"
"kcl-lang.io/kcl-go/pkg/utils"
)

Expand Down Expand Up @@ -43,7 +44,7 @@ func NewCleanCmd() *cobra.Command {
filepath.Join(args[0], "__main__/.kclvm/cache"),
}
for _, cachePath := range cachePaths {
if isDir(cachePath) {
if fs.IsDir(cachePath) {
if err := os.RemoveAll(cachePath); err == nil {
fmt.Printf("%s removed\n", cachePath)
} else {
Expand All @@ -59,11 +60,3 @@ func NewCleanCmd() *cobra.Command {

return cmd
}

func isDir(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.IsDir()
}
84 changes: 84 additions & 0 deletions pkg/fs/pattern.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package fs

import (
"fmt"
"os"
"path/filepath"
)

// ExpandInputFiles returns all the filenames that match the input filename,
// filepath or patterns.
func ExpandInputFiles(files []string, recursive bool) ([]string, error) {
var result []string
for _, f := range files {
expandFiles, err := ExpandIfFilePattern(f, recursive)
if err != nil {
return result, err
}
result = append(result, expandFiles...)
}
return result, nil
}

// ExpandIfFilePattern returns all the filenames that match the input pattern
// or the filename if it is a specific filename and not a pattern.
// If the input is a pattern and it yields no result it will result in an error.
func ExpandIfFilePattern(pattern string, recursive bool) ([]string, error) {
if _, err := os.Stat(pattern); os.IsNotExist(err) {
matches, err := filepath.Glob(pattern)
if err == nil && len(matches) == 0 {
return nil, fmt.Errorf("the path %q does not exist", pattern)
}
if err == filepath.ErrBadPattern {
return nil, fmt.Errorf("pattern %q is not valid: %v", pattern, err)
}
return matches, err
}
if IsDir(pattern) {
return GetAllFilesInFolder(pattern, recursive)
}
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()
}
12 changes: 9 additions & 3 deletions pkg/options/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"

"kcl-lang.io/cli/pkg/fs"
"kcl-lang.io/kcl-go/pkg/tools/gen"
crdGen "kcl-lang.io/kcl-openapi/pkg/kube_resource/generator"
"kcl-lang.io/kcl-openapi/pkg/swagger/generator"
Expand All @@ -20,6 +21,7 @@ type ImportOptions struct {
Force bool
SkipValidation bool
ModelPackage string
Recursive bool
}

// NewImportOptions returns a new instance of ImportOptions with default values.
Expand All @@ -33,6 +35,10 @@ func NewImportOptions() *ImportOptions {
func (o *ImportOptions) Run() error {
opts := &gen.GenKclOptions{}
mode := strings.ToLower(o.Mode)
files, err := fs.ExpandInputFiles(o.Files, o.Recursive)
if err != nil {
return err
}
switch mode {
case Json:
opts.Mode = gen.ModeJson
Expand All @@ -47,7 +53,7 @@ func (o *ImportOptions) Run() error {
case Auto:
opts.Mode = gen.ModeAuto
case Crd, OpenAPI:
for _, p := range o.Files {
for _, p := range files {
opts := new(generator.GenOpts)
// cli opts to generator.GenOpts
opts.Spec = p
Expand Down Expand Up @@ -85,14 +91,14 @@ func (o *ImportOptions) Run() error {
}

if o.Output == "-" {
for _, p := range o.Files {
for _, p := range files {
err := gen.GenKcl(os.Stdout, p, nil, opts)
if err != nil {
return err
}
}
} else {
for _, p := range o.Files {
for _, p := range files {
outputFile := o.Output
if outputFile == "" {
filenameWithExtension := filepath.Base(p)
Expand Down

0 comments on commit 3271e5f

Please sign in to comment.