Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.
Open
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
87 changes: 86 additions & 1 deletion internal/package_handler/schema.go
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 {
Copy link
Contributor

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:

  • checksProfileSchema
  • checkManifestSchema

// 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)
}
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 NewPackageHandlerFromURL method. Subsequent actions should be performed using the methods of the PackageHandler type returned by it.

_, 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this function is unnecessary because the Packagehandler already knows the exact location of the profile.yml and manifest.yml files by following the specification, so there is no need to walk through the entire directory tree. Additionally, this function checks for manifest.yml and profile.yml files that may not be necessary for the specification, as they might be located in a different directory than the one declared in the spec.

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
}
3 changes: 3 additions & 0 deletions internal/package_handler/schema/manifest_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ properties:
image:
type: string
additionalProperties: false
required:
- image
profiles:
type: array
minItems: 1
items:
type: string
required:
Expand Down
Loading