debian-forge-composer/internal/osbuild/skopeo_source.go
Christian Kellner 513ae6d3d0 osbuild: add support for the org.osbuild.skopeo source
Add bindings for the `org.osbuild.skopeo` source that can be used
to fetch container images from container registires.
2022-07-21 13:32:07 +02:00

73 lines
1.6 KiB
Go

package osbuild
import (
"fmt"
"regexp"
)
var skopeoDigestPattern = regexp.MustCompile(`sha256:[0-9a-f]{64}`)
type SkopeoSource struct {
Items map[string]SkopeoSourceItem `json:"items"`
}
func (SkopeoSource) isSource() {}
type SkopeopSourceImage struct {
Name string `json:"name"`
Digest string `json:"digest"`
TLSVerify *bool `json:"tls-verify,omitempty"`
}
type SkopeoSourceItem struct {
Image SkopeopSourceImage `json:"image"`
}
// NewSkopeoSourceItem creates a new source item for name and digest
func NewSkopeoSourceItem(name, digest string, tlsVerify *bool) SkopeoSourceItem {
item := SkopeoSourceItem{
Image: SkopeopSourceImage{
Name: name,
Digest: digest,
TLSVerify: tlsVerify,
},
}
return item
}
func (item SkopeoSourceItem) validate() error {
if item.Image.Name == "" {
return fmt.Errorf("source item has empty name")
}
if !skopeoDigestPattern.MatchString(item.Image.Digest) {
return fmt.Errorf("source item has invalid digest")
}
return nil
}
// NewSkopeoSource creates a new and empty SkopeoSource
func NewSkopeoSource() *SkopeoSource {
return &SkopeoSource{
Items: make(map[string]SkopeoSourceItem),
}
}
// AddItem adds a source item to the source; will panic
// if any of the supplied options are invalid or missing
func (source *SkopeoSource) AddItem(name, digest, image string, tlsVerify *bool) {
item := NewSkopeoSourceItem(name, digest, tlsVerify)
if err := item.validate(); err != nil {
panic(err)
}
if !skopeoDigestPattern.MatchString(image) {
panic("item has invalid image id")
}
source.Items[image] = item
}