Refactor op/script/component loading and spec validation

Signed-off-by: Solomon Hykes <sh.github.6811@hykes.org>
This commit is contained in:
Solomon Hykes
2021-01-24 12:54:55 -08:00
parent bbe16283ab
commit e10025d688
19 changed files with 269 additions and 177 deletions

View File

@@ -3,21 +3,44 @@ package dagger
import (
"context"
"os"
"github.com/pkg/errors"
)
type Component struct {
// Source value for the component, without spec merged
// eg. `{ string, #dagger: compute: [{do:"fetch-container", ...}]}`
v *Value
// Annotation value for the component , with spec merged.
// -> the contents of #dagger.compute
// eg. `compute: [{do:"fetch-container", ...}]`
//
// The spec is merged at this level because the Cue API
// does not support merging embedded scalar with nested definition.
config *Value
}
func NewComponent(v *Value) (*Component, error) {
config := v.Get("#dagger")
if !config.Exists() {
return nil, os.ErrNotExist
}
spec := v.cc.Spec()
config, err := spec.Get("#ComponentConfig").Merge(v.Get("#dagger"))
if err != nil {
return nil, errors.Wrap(err, "invalid component config")
}
return &Component{
v: v,
config: config,
}, nil
}
func (c *Component) Value() *Value {
return c.v
}
func (c *Component) Exists() bool {
// Does #dagger exist?
return c.Config().Exists()
}
// Return the contents of the "#dagger" annotation.
func (c *Component) Config() *Value {
return c.Value().Get("#dagger")
@@ -38,11 +61,7 @@ func (c *Component) Validate() error {
// Return this component's compute script.
func (c *Component) ComputeScript() (*Script, error) {
v := c.Value().Get("#dagger.compute")
if !v.Exists() {
return nil, os.ErrNotExist
}
return v.Script()
return newScript(c.Config().Get("compute"))
}
// Compute the configuration for this component.