53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package template
|
|
|
|
import "fmt"
|
|
|
|
type registryKey struct {
|
|
id string
|
|
version int
|
|
}
|
|
|
|
// Registry holds the templates of the labd build by ID and version.
|
|
type Registry struct {
|
|
templates map[registryKey]Template
|
|
}
|
|
|
|
// NewRegistry validates the templates and indexes them. A duplicate ID and
|
|
// version is a build error.
|
|
func NewRegistry(templates ...Template) (*Registry, error) {
|
|
registry := &Registry{templates: make(map[registryKey]Template, len(templates))}
|
|
for _, template := range templates {
|
|
if err := template.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
key := registryKey{id: template.ID, version: template.Version}
|
|
if _, duplicate := registry.templates[key]; duplicate {
|
|
return nil, fmt.Errorf("template.NewRegistry - duplicate template %s version %d", template.ID, template.Version)
|
|
}
|
|
registry.templates[key] = template
|
|
}
|
|
return registry, nil
|
|
}
|
|
|
|
// Lookup returns the template with the given ID and version.
|
|
func (r *Registry) Lookup(id string, version int) (Template, bool) {
|
|
template, ok := r.templates[registryKey{id: id, version: version}]
|
|
return template, ok
|
|
}
|
|
|
|
// All returns every template in no particular order.
|
|
func (r *Registry) All() []Template {
|
|
all := make([]Template, 0, len(r.templates))
|
|
for _, template := range r.templates {
|
|
all = append(all, template)
|
|
}
|
|
return all
|
|
}
|
|
|
|
// Builtin returns the templates of this build. Real templates and their
|
|
// prepared images arrive with the MikroTik and Ubuntu lab images; until then
|
|
// the registry is empty and every CreateLab answers validation_failed.
|
|
func Builtin() []Template {
|
|
return nil
|
|
}
|