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

Added yaml file extension validation for manifest retrieval code #269

Merged
merged 3 commits into from
Apr 25, 2024
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
31 changes: 25 additions & 6 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,36 @@ func isDirectory(path string) (bool, error) {
return fileInfo.IsDir(), nil
}

// getManifestFiles uses filepath.Walk to retrieve a list of the manifest files within the given manifest path
// isYAML determines if a file is of the YAML extension or not
func isYAML(path string) bool {
return filepath.Ext(path) == ".yaml" || filepath.Ext(path) == ".yml"
}

// getManifests uses filepath.Walk to retrieve a list of the manifest files within the given manifest path
func getManifestFiles(p string) ([]safeguards.ManifestFile, error) {
var manifestFiles []safeguards.ManifestFile

err := filepath.Walk(p, func(filepath string, info fs.FileInfo, err error) error {
noYamlFiles := true
err := filepath.Walk(p, func(walkPath string, info fs.FileInfo, err error) error {
manifest := safeguards.ManifestFile{}
// skip when walkPath is just given path and also a directory
if p == walkPath && info.IsDir() {
return nil
}

if err != nil {
return fmt.Errorf("error walking path %s with error: %w", filepath, err)
return fmt.Errorf("error walking path %s with error: %w", walkPath, err)
}

if !info.IsDir() && info.Name() != "" {
if !info.IsDir() && info.Name() != "" && isYAML(walkPath) {
log.Debugf("%s is not a directory, appending to manifestFiles", info.Name())
noYamlFiles = false

manifest.Name = info.Name()
manifest.Path = filepath
manifest.Path = walkPath
manifestFiles = append(manifestFiles, manifest)
} else if !isYAML(p) {
log.Debugf("%s is not a manifest file, skipping...", info.Name())
} else {
log.Debugf("%s is a directory, skipping...", info.Name())
}
Expand All @@ -79,6 +93,9 @@ func getManifestFiles(p string) ([]safeguards.ManifestFile, error) {
if err != nil {
return nil, fmt.Errorf("could not walk directory: %w", err)
}
if noYamlFiles {
return nil, fmt.Errorf("no manifest files found within given path")
}

return manifestFiles, nil
}
Expand All @@ -101,11 +118,13 @@ func (vc *validateCmd) run(c *cobra.Command) error {
if err != nil {
return err
}
} else {
} else if isYAML(vc.manifestPath) {
manifestFiles = append(manifestFiles, safeguards.ManifestFile{
Name: path.Base(vc.manifestPath),
Path: vc.manifestPath,
})
} else {
return fmt.Errorf("expected at least one .yaml or .yml file within given path")
}

if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ func TestIsDirectory(t *testing.T) {
assert.NotNil(t, err)
}

// TestIsYAML tests the isYAML function for proper returns
func TestIsYAML(t *testing.T) {
dirNotYaml, _ := filepath.Abs("../pkg/safeguards/tests/not-yaml")
dirYaml, _ := filepath.Abs("../pkg/safeguards/tests/all/success")
fileNotYaml, _ := filepath.Abs("../pkg/safeguards/tests/not-yaml/readme.md")
fileYaml, _ := filepath.Abs("../pkg/safeguards/tests/all/success/all-success-manifest-1.yaml")

assert.False(t, isYAML(fileNotYaml))
assert.True(t, isYAML(fileYaml))

manifestFiles, err := getManifestFiles(dirNotYaml)
assert.Nil(t, manifestFiles)
assert.NotNil(t, err)

manifestFiles, err = getManifestFiles(dirYaml)
assert.NotNil(t, manifestFiles)
assert.Nil(t, err)
}

// TestRunValidate tests the run command for `draft validate` for proper returns
func TestRunValidate(t *testing.T) {
ctx := context.TODO()
Expand Down
1 change: 1 addition & 0 deletions pkg/safeguards/tests/not-yaml/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a test file
Loading