Migrate dagger.#Secret and dagger.#Stream to new format
- Refactored to keep every transformation of built-in types (e.g. FS, Secret, etc) to/from CUE in the same place (plancontext) - dagger.#Service and dagger.#Secret are now following the new FS-like format (e.g. `_service: id: string`) - Backward compatibility - dagger.#Stream is now an alias for dagger.#Service Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
@@ -1,32 +1,71 @@
|
||||
package plancontext
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"github.com/google/uuid"
|
||||
bkgw "github.com/moby/buildkit/frontend/gateway/client"
|
||||
"go.dagger.io/dagger/compiler"
|
||||
)
|
||||
|
||||
var (
|
||||
fsIDPath = cue.MakePath(
|
||||
cue.Hid("_fs", "alpha.dagger.io/dagger"),
|
||||
cue.Str("id"),
|
||||
)
|
||||
)
|
||||
|
||||
type FS struct {
|
||||
Result bkgw.Reference
|
||||
id string
|
||||
result bkgw.Reference
|
||||
}
|
||||
|
||||
func (fs *FS) Result() bkgw.Reference {
|
||||
return fs.result
|
||||
}
|
||||
|
||||
func (fs *FS) Value() *compiler.Value {
|
||||
v := compiler.NewValue()
|
||||
if err := v.FillPath(fsIDPath, fs.id); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
type fsContext struct {
|
||||
l sync.RWMutex
|
||||
store map[ContextKey]*FS
|
||||
store map[string]*FS
|
||||
}
|
||||
|
||||
func (c *fsContext) Register(fs *FS) ContextKey {
|
||||
func (c *fsContext) New(result bkgw.Reference) *FS {
|
||||
c.l.Lock()
|
||||
defer c.l.Unlock()
|
||||
|
||||
id := hashID(fs)
|
||||
c.store[id] = fs
|
||||
return id
|
||||
fs := &FS{
|
||||
// FIXME: get a hash from result instead
|
||||
id: uuid.New().String(),
|
||||
result: result,
|
||||
}
|
||||
|
||||
c.store[fs.id] = fs
|
||||
return fs
|
||||
}
|
||||
|
||||
func (c *fsContext) Get(id ContextKey) *FS {
|
||||
func (c *fsContext) FromValue(v *compiler.Value) (*FS, error) {
|
||||
c.l.RLock()
|
||||
defer c.l.RUnlock()
|
||||
|
||||
return c.store[id]
|
||||
id, err := v.LookupPath(fsIDPath).String()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid FS %q: %w", v.Path(), err)
|
||||
}
|
||||
|
||||
fs, ok := c.store[id]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("fs %q not found", id)
|
||||
}
|
||||
|
||||
return fs, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user