This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
dagger/dagger/fs.go
Andrea Luzzardi c923e5042b cleanup: solver/fs
- Solver: Encapsulates all access to Buildkit. Can solve plain LLB, invoke external frontends (for DockerBuild) and export (for ContainerPush)
- FS (now BuildkitFS) implements the standard Go 1.16 io/fs.FS interface and provides a read-only filesystem on top of a buildkit result. It can be used with built-ins such as fs.WalkDir (no need to have our own Walk functions anymore)
- Moved CueBuild into compiler.Build since it no longer depends on Buildkit. Instead it relies on the io/fs.FS interface, which is used both for the base config and the stdlib (go:embed also uses io/fs.FS). Overlaying base and the stdlib is now done by the same code.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
2021-03-12 13:38:49 -08:00

105 lines
2.0 KiB
Go

package dagger
import (
"context"
"errors"
"io/fs"
"time"
bkgw "github.com/moby/buildkit/frontend/gateway/client"
fstypes "github.com/tonistiigi/fsutil/types"
)
// BuildkitFS is a io/fs.FS adapter for Buildkit
// BuildkitFS implements the ReadFileFS, StatFS and ReadDirFS interfaces.
type BuildkitFS struct {
ref bkgw.Reference
}
func NewBuildkitFS(ref bkgw.Reference) *BuildkitFS {
return &BuildkitFS{
ref: ref,
}
}
// Open is not supported.
func (f *BuildkitFS) Open(name string) (fs.File, error) {
return nil, errors.New("not implemented")
}
func (f *BuildkitFS) Stat(name string) (fs.FileInfo, error) {
st, err := f.ref.StatFile(context.TODO(), bkgw.StatRequest{
Path: name,
})
if err != nil {
return nil, err
}
return bkFileInfo{st}, nil
}
func (f *BuildkitFS) ReadDir(name string) ([]fs.DirEntry, error) {
entries, err := f.ref.ReadDir(context.TODO(), bkgw.ReadDirRequest{
Path: name,
})
if err != nil {
return nil, err
}
res := make([]fs.DirEntry, 0, len(entries))
for _, st := range entries {
res = append(res, bkDirEntry{
bkFileInfo: bkFileInfo{
st: st,
},
})
}
return res, nil
}
func (f *BuildkitFS) ReadFile(name string) ([]byte, error) {
return f.ref.ReadFile(context.TODO(), bkgw.ReadRequest{
Filename: name,
})
}
// bkFileInfo is a fs.FileInfo adapter for fstypes.Stat
type bkFileInfo struct {
st *fstypes.Stat
}
func (s bkFileInfo) Name() string {
return s.st.GetPath()
}
func (s bkFileInfo) Size() int64 {
return s.st.GetSize_()
}
func (s bkFileInfo) IsDir() bool {
return s.st.IsDir()
}
func (s bkFileInfo) ModTime() time.Time {
return time.Unix(s.st.GetModTime(), 0)
}
func (s bkFileInfo) Mode() fs.FileMode {
return fs.FileMode(s.st.Mode)
}
func (s bkFileInfo) Sys() interface{} {
return s.st
}
// bkDirEntry is a fs.DirEntry adapter for fstypes.Stat
type bkDirEntry struct {
bkFileInfo
}
func (s bkDirEntry) Info() (fs.FileInfo, error) {
return s.bkFileInfo, nil
}
func (s bkDirEntry) Type() fs.FileMode {
return s.Mode()
}