dagger.#FS support

- Implement dagger.#FS support
- Migrate `context.imports` to dagger.#FS
- Backward compat: dagger.#FS can be passed in lieu of a
  dagger.#Artifact
- For instance, an import (`dagger.#FS`) can be passed to the current
  `yarn.#Package` implementation

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-11-30 12:48:09 -08:00
parent 6bedfb7c63
commit 0aea10d23e
23 changed files with 359 additions and 135 deletions

View File

@@ -15,10 +15,11 @@ type ContextKey string
// id := ctx.Secrets.Register("mysecret")
// secret := ctx.Secrets.Get(id)
type Context struct {
Platform *platformContext
Directories *directoryContext
Secrets *secretContext
Services *serviceContext
Platform *platformContext
FS *fsContext
LocalDirs *localDirContext
Secrets *secretContext
Services *serviceContext
}
func New() *Context {
@@ -26,8 +27,11 @@ func New() *Context {
Platform: &platformContext{
platform: defaultPlatform,
},
Directories: &directoryContext{
store: make(map[ContextKey]*Directory),
FS: &fsContext{
store: make(map[ContextKey]*FS),
},
LocalDirs: &localDirContext{
store: make(map[ContextKey]*LocalDir),
},
Secrets: &secretContext{
store: make(map[ContextKey]*Secret),

View File

@@ -1,54 +0,0 @@
package plancontext
import "sync"
type Directory struct {
Path string
Include []string
Exclude []string
}
type directoryContext struct {
l sync.RWMutex
store map[ContextKey]*Directory
}
func (c *directoryContext) Register(directory *Directory) ContextKey {
c.l.Lock()
defer c.l.Unlock()
id := hashID(directory)
c.store[id] = directory
return id
}
func (c *directoryContext) Get(id ContextKey) *Directory {
c.l.RLock()
defer c.l.RUnlock()
return c.store[id]
}
func (c *directoryContext) List() []*Directory {
c.l.RLock()
defer c.l.RUnlock()
directories := make([]*Directory, 0, len(c.store))
for _, d := range c.store {
directories = append(directories, d)
}
return directories
}
func (c *directoryContext) Paths() map[string]string {
c.l.RLock()
defer c.l.RUnlock()
directories := make(map[string]string)
for _, d := range c.store {
directories[d.Path] = d.Path
}
return directories
}

32
plancontext/fs.go Normal file
View File

@@ -0,0 +1,32 @@
package plancontext
import (
"sync"
bkgw "github.com/moby/buildkit/frontend/gateway/client"
)
type FS struct {
Result bkgw.Reference
}
type fsContext struct {
l sync.RWMutex
store map[ContextKey]*FS
}
func (c *fsContext) Register(fs *FS) ContextKey {
c.l.Lock()
defer c.l.Unlock()
id := hashID(fs)
c.store[id] = fs
return id
}
func (c *fsContext) Get(id ContextKey) *FS {
c.l.RLock()
defer c.l.RUnlock()
return c.store[id]
}

60
plancontext/localdir.go Normal file
View File

@@ -0,0 +1,60 @@
package plancontext
import (
"path/filepath"
"sync"
)
type LocalDir struct {
Path string
}
type localDirContext struct {
l sync.RWMutex
store map[ContextKey]*LocalDir
}
func (c *localDirContext) Register(directory *LocalDir) ContextKey {
c.l.Lock()
defer c.l.Unlock()
id := hashID(directory)
c.store[id] = directory
return id
}
func (c *localDirContext) Get(id ContextKey) *LocalDir {
c.l.RLock()
defer c.l.RUnlock()
return c.store[id]
}
func (c *localDirContext) List() []*LocalDir {
c.l.RLock()
defer c.l.RUnlock()
directories := make([]*LocalDir, 0, len(c.store))
for _, d := range c.store {
directories = append(directories, d)
}
return directories
}
func (c *localDirContext) Paths() (map[string]string, error) {
c.l.RLock()
defer c.l.RUnlock()
directories := make(map[string]string)
for _, d := range c.store {
abs, err := filepath.Abs(d.Path)
if err != nil {
return nil, err
}
directories[d.Path] = abs
}
return directories, nil
}