74 lines
2.5 KiB
Go
74 lines
2.5 KiB
Go
package manifest
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
|
)
|
|
|
|
// SuppliedProductVersion identifies the supported supplied-product collection.
|
|
const SuppliedProductVersion = 1
|
|
|
|
// SuppliedProductCollection records accepted local products independently of execution.
|
|
type SuppliedProductCollection struct {
|
|
Version int `json:"version"`
|
|
Products map[string]SuppliedProduct `json:"products"`
|
|
}
|
|
|
|
// SuppliedProduct identifies the actual local bytes and their interpretation.
|
|
type SuppliedProduct struct {
|
|
Path string `json:"path"`
|
|
Checksum string `json:"checksum"`
|
|
Size int64 `json:"size"`
|
|
Contract *artifactmodel.ContractMetadata `json:"contract,omitempty"`
|
|
}
|
|
|
|
// ValidateSuppliedProducts validates durable record structure, not filesystem content.
|
|
func ValidateSuppliedProducts(collection *SuppliedProductCollection) error {
|
|
if collection == nil {
|
|
return nil
|
|
}
|
|
if collection.Version != SuppliedProductVersion {
|
|
return fmt.Errorf("unsupported supplied product version %d", collection.Version)
|
|
}
|
|
if collection.Products == nil {
|
|
return fmt.Errorf("supplied products are required")
|
|
}
|
|
for id, record := range collection.Products {
|
|
source, err := artifactpolicy.ClassifySource(id)
|
|
if err != nil || source.ID != id || source.Kind == artifactpolicy.SourceKindPreviousArtifact {
|
|
return fmt.Errorf("invalid supplied product source %q", id)
|
|
}
|
|
if err := validateCanonicalRelativePath("path", record.Path); err != nil {
|
|
return fmt.Errorf("supplied product %q: %w", id, err)
|
|
}
|
|
if err := validateSHA256("checksum", record.Checksum); err != nil {
|
|
return fmt.Errorf("supplied product %q: %w", id, err)
|
|
}
|
|
if record.Size <= 0 {
|
|
return fmt.Errorf("supplied product %q: size must be positive", id)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CloneSuppliedProducts isolates mutable product records and contract metadata.
|
|
func CloneSuppliedProducts(collection *SuppliedProductCollection) *SuppliedProductCollection {
|
|
if collection == nil {
|
|
return nil
|
|
}
|
|
clone := *collection
|
|
if collection.Products != nil {
|
|
clone.Products = make(map[string]SuppliedProduct, len(collection.Products))
|
|
for id, record := range collection.Products {
|
|
if record.Contract != nil {
|
|
contract := *record.Contract
|
|
record.Contract = &contract
|
|
}
|
|
clone.Products[id] = record
|
|
}
|
|
}
|
|
return &clone
|
|
}
|