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,27 +1,84 @@
|
||||
package plancontext
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"go.dagger.io/dagger/compiler"
|
||||
)
|
||||
|
||||
var (
|
||||
serviceIDPath = cue.MakePath(
|
||||
cue.Hid("_service", "alpha.dagger.io/dagger"),
|
||||
cue.Str("id"),
|
||||
)
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
id string
|
||||
|
||||
unix string
|
||||
npipe string
|
||||
}
|
||||
|
||||
func (s *Service) ID() string {
|
||||
return s.id
|
||||
}
|
||||
|
||||
func (s *Service) Unix() string {
|
||||
return s.unix
|
||||
}
|
||||
|
||||
func (s *Service) NPipe() string {
|
||||
return s.npipe
|
||||
}
|
||||
|
||||
func (s *Service) Value() *compiler.Value {
|
||||
v := compiler.NewValue()
|
||||
if err := v.FillPath(serviceIDPath, s.id); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
type serviceContext struct {
|
||||
l sync.RWMutex
|
||||
store map[ContextKey]*Service
|
||||
store map[string]*Service
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Unix string
|
||||
Npipe string
|
||||
}
|
||||
|
||||
func (c *serviceContext) Register(service *Service) ContextKey {
|
||||
func (c *serviceContext) New(unix, npipe string) *Service {
|
||||
c.l.Lock()
|
||||
defer c.l.Unlock()
|
||||
|
||||
id := hashID(service)
|
||||
c.store[id] = service
|
||||
return id
|
||||
s := &Service{
|
||||
id: hashID(unix, npipe),
|
||||
unix: unix,
|
||||
npipe: npipe,
|
||||
}
|
||||
|
||||
c.store[s.id] = s
|
||||
return s
|
||||
}
|
||||
|
||||
func (c *serviceContext) Get(id ContextKey) *Service {
|
||||
func (c *serviceContext) FromValue(v *compiler.Value) (*Service, error) {
|
||||
c.l.RLock()
|
||||
defer c.l.RUnlock()
|
||||
|
||||
id, err := v.LookupPath(serviceIDPath).String()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid service %q: %w", v.Path(), err)
|
||||
}
|
||||
|
||||
s, ok := c.store[id]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("service %q not found", id)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (c *serviceContext) Get(id string) *Service {
|
||||
c.l.RLock()
|
||||
defer c.l.RUnlock()
|
||||
|
||||
|
Reference in New Issue
Block a user