Skip to content

Commit

Permalink
feat: get all files in --cr path provided
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-ibra committed Sep 6, 2024
1 parent 6d9420f commit ad10303
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 6 deletions.
38 changes: 32 additions & 6 deletions pkg/cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/validator-labs/validatorctl/pkg/utils/embed"
"github.com/validator-labs/validatorctl/pkg/utils/exec"
exec_utils "github.com/validator-labs/validatorctl/pkg/utils/exec"
file_utils "github.com/validator-labs/validatorctl/pkg/utils/file"
"github.com/validator-labs/validatorctl/pkg/utils/kind"
"github.com/validator-labs/validatorctl/pkg/utils/kube"
string_utils "github.com/validator-labs/validatorctl/pkg/utils/string"
Expand Down Expand Up @@ -283,13 +284,38 @@ func ConfigureOrCheckCommand(c *cfg.Config, tc *cfg.TaskConfig) error {
}

func readPluginSpecs(path string) ([]plugins.PluginSpec, error) {
log.InfoCLI("Reading plugin specs from %s", path)
fi, err := os.Stat(path)
if err != nil {
return nil, err
}

files := make([]string, 0)
if !fi.IsDir() {
files = append(files, path)
} else {
files, err = file_utils.GetFilesInDir(path)
if err != nil {
return nil, err
}
}

if len(files) == 0 {
return nil, fmt.Errorf("no files found in %s", path)
}

ps := make([]plugins.PluginSpec, 0)
for _, f := range files {
pSpecs := readPluginSpecsFromFile(f)
ps = append(ps, pSpecs...)
}

return ps, nil
}

// TODO:
// get a list of files to check. if its a directory the list is all the files in the dir, if its a file the list is the single file
// read all files in this list and return all the plugin specs in each of the files
// return the list of plugin specs
return []plugins.PluginSpec{}, nil
func readPluginSpecsFromFile(path string) []plugins.PluginSpec {
// read the file and return the plugin specs
log.InfoCLI("Reading plugin specs from %s", path)
return []plugins.PluginSpec{}
}

func toPluginSpecs(vc *components.ValidatorConfig) []plugins.PluginSpec {
Expand Down
27 changes: 27 additions & 0 deletions pkg/utils/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package file

import (
"io/fs"
"path/filepath"
)

// GetFilesInDir walks the file tree from dir and returns a list of all files found in lexical order.
func GetFilesInDir(dir string) ([]string, error) {
files := make([]string, 0)
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if !d.IsDir() {
files = append(files, path)
}

return nil
})
if err != nil {
return nil, err
}

return files, nil
}
101 changes: 101 additions & 0 deletions pkg/utils/file/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package file

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

var content = []byte("test content")

func cleanup(dir string) error {
return os.RemoveAll(dir)
}

func TestGetFilesInDir(t *testing.T) {
tests := []struct {
name string
setup func() (string, error)
expected []string
}{
{
name: "empty directory",
setup: func() (string, error) {
dir, err := os.MkdirTemp("", "test-empty-dir")
return dir, err
},
expected: []string{},
},
{
name: "directory with files",
setup: func() (string, error) {
dir, err := os.MkdirTemp("", "test-dir-with-files")
if err != nil {
return "", err
}

files := []string{"file1.txt", "file2.txt"}
for _, file := range files {
err = os.WriteFile(filepath.Join(dir, file), content, 0644)
if err != nil {
return "", err
}
}
return dir, nil
},
expected: []string{"file1.txt", "file2.txt"},
},
{
name: "nested directories with files",
setup: func() (string, error) {
dir, err := os.MkdirTemp("", "test-nested-dir")
if err != nil {
return "", err
}

subDir := filepath.Join(dir, "subdir")
err = os.Mkdir(subDir, 0755)
if err != nil {
return "", err
}
err = os.WriteFile(filepath.Join(dir, "file1.txt"), content, 0644)
if err != nil {
return "", err
}
err = os.WriteFile(filepath.Join(subDir, "file2.txt"), content, 0644)
if err != nil {
return "", err
}
err = os.WriteFile(filepath.Join(subDir, "file3.txt"), content, 0644)
if err != nil {
return "", err
}
return dir, nil
},
expected: []string{"file1.txt", "subdir/file2.txt", "subdir/file3.txt"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup test directory and files
dir, err := tt.setup()
if err != nil {
t.Fatalf("Setup failed: %v", err)
}
defer cleanup(dir)

files, err := GetFilesInDir(dir)
if err != nil {
t.Fatalf("GetFilesInDir() error = %v", err)
}

// Validate the result
for i, file := range files {
if file != filepath.Join(dir, tt.expected[i]) {
t.Errorf("Expected file %v, got %v", tt.expected[i], file)
}
}
})
}
}

0 comments on commit ad10303

Please sign in to comment.