-
Notifications
You must be signed in to change notification settings - Fork 20
Update YAML Schema Validation #70
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,30 @@ | ||
| package package_handler | ||
|
|
||
| import ( | ||
| "embed" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/go-git/go-git/v5" | ||
| "github.com/xeipuuv/gojsonschema" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| const ( | ||
| manifestSchema = "schema/manifest_schema.yml" | ||
| profileSchema = "schema/profile_schema.yml" | ||
| ) | ||
|
|
||
| //go:embed schema/* | ||
| var fs embed.FS | ||
|
|
||
| func validateYAMLSchema(schemaFile, documentFile string) error { | ||
| // Read the YAML schema | ||
| schemaData, err := os.ReadFile(schemaFile) | ||
|
|
||
| schemaData, err := fs.ReadFile(schemaFile) | ||
| if err != nil { | ||
| return fmt.Errorf("error reading the schema file: %v", err) | ||
| } | ||
|
|
@@ -57,3 +70,75 @@ func validateYAMLSchema(schemaFile, documentFile string) error { | |
| } | ||
| return nil | ||
| } | ||
|
|
||
| func ValidateFromRepository(repoURL, repoPath string) (err error) { | ||
| err = cloneRepository(repoURL, repoPath) | ||
| if err != nil { | ||
| log.Fatal("Failed to clone repository:", err) | ||
| } | ||
| // Walk through the directories and validate YAML files | ||
| err = walkDirectories(repoPath) | ||
| if err != nil { | ||
| log.Fatal("Failed to walk directories:", err) | ||
| } | ||
|
|
||
| // Remove repoPath | ||
| err = os.RemoveAll(repoPath) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func cloneRepository(url string, path string) error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that this function is unnecessary. The responsibility of cloning the repository should be on the |
||
| _, err := git.PlainClone(path, false, &git.CloneOptions{ | ||
| URL: url, | ||
| }) | ||
| if err != nil { | ||
| log.Println("Failed to clone repository:", err) | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func walkDirectories(root string) error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this function is unnecessary because the |
||
| err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| log.Println("Failed to walk directory:", err) | ||
| return err | ||
| } | ||
|
|
||
| // Check if the file is a 'profile.yml' or 'manifest.yml' | ||
| if info.IsDir() { | ||
| return nil | ||
| } | ||
|
|
||
| if info.Name() == "profile.yml" { | ||
| // Validate the YAML file against the schema | ||
| err := validateYAMLSchema(profileSchema, path) | ||
| if err != nil { | ||
| log.Printf("Failed to validate %s: %s\n", path, err) | ||
| } else { | ||
| log.Printf("%s is valid\n", path) | ||
| } | ||
| } | ||
| if info.Name() == "manifest.yml" { | ||
| // Validate the YAML file against the schema | ||
| err := validateYAMLSchema(manifestSchema, path) | ||
| if err != nil { | ||
| log.Printf("Failed to validate %s: %s\n", path, err) | ||
| } else { | ||
| log.Printf("%s is valid\n", path) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
| if err != nil { | ||
| log.Println("Failed to walk directories:", err) | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest creating two private methods in
PackageHandler:checksProfileSchemacheckManifestSchema