2021-11-25 01:24:04 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-23 20:23:52 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-11-25 01:24:04 +01:00
|
|
|
|
2021-11-30 21:48:09 +01:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
2021-12-16 22:39:34 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-11-25 01:24:04 +01:00
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/plancontext"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-12-22 16:19:40 +01:00
|
|
|
Register("InputDirectory", func() Task { return &inputDirectoryTask{} })
|
2021-11-25 01:24:04 +01:00
|
|
|
}
|
|
|
|
|
2021-12-22 16:19:40 +01:00
|
|
|
type inputDirectoryTask struct {
|
2021-11-25 01:24:04 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:23:52 +01:00
|
|
|
func (c *inputDirectoryTask) PreRun(ctx context.Context, pctx *plancontext.Context, v *compiler.Value) error {
|
|
|
|
path, err := v.Lookup("path").AbsPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
|
|
return fmt.Errorf("path %q does not exist", path)
|
|
|
|
}
|
|
|
|
pctx.LocalDirs.Add(path)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-22 16:19:40 +01:00
|
|
|
func (c *inputDirectoryTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
2021-12-23 19:09:26 +01:00
|
|
|
path, err := v.Lookup("path").AbsPath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:48:09 +01:00
|
|
|
var dir struct {
|
|
|
|
Include []string
|
|
|
|
Exclude []string
|
|
|
|
}
|
2021-11-25 01:24:04 +01:00
|
|
|
|
|
|
|
if err := v.Decode(&dir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:39:34 +01:00
|
|
|
lg := log.Ctx(ctx)
|
2021-12-23 19:09:26 +01:00
|
|
|
lg.Debug().Str("path", path).Msg("loading local directory")
|
2021-11-30 21:48:09 +01:00
|
|
|
opts := []llb.LocalOption{
|
2021-12-23 19:09:26 +01:00
|
|
|
withCustomName(v, "Local %s", path),
|
2021-11-30 21:48:09 +01:00
|
|
|
// Without hint, multiple `llb.Local` operations on the
|
|
|
|
// same path get a different digest.
|
|
|
|
llb.SessionID(s.SessionID()),
|
2021-12-23 19:09:26 +01:00
|
|
|
llb.SharedKeyHint(path),
|
2021-11-30 21:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(dir.Include) > 0 {
|
|
|
|
opts = append(opts, llb.IncludePatterns(dir.Include))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Excludes .dagger directory by default
|
|
|
|
excludePatterns := []string{"**/.dagger/"}
|
|
|
|
if len(dir.Exclude) > 0 {
|
|
|
|
excludePatterns = dir.Exclude
|
2021-11-25 01:24:04 +01:00
|
|
|
}
|
2021-11-30 21:48:09 +01:00
|
|
|
opts = append(opts, llb.ExcludePatterns(excludePatterns))
|
|
|
|
|
|
|
|
// FIXME: Remove the `Copy` and use `Local` directly.
|
|
|
|
//
|
|
|
|
// Copy'ing is a costly operation which should be unnecessary.
|
|
|
|
// However, using llb.Local directly breaks caching sometimes for unknown reasons.
|
|
|
|
st := llb.Scratch().File(
|
|
|
|
llb.Copy(
|
|
|
|
llb.Local(
|
2021-12-23 19:09:26 +01:00
|
|
|
path,
|
2021-11-30 21:48:09 +01:00
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
),
|
2021-12-23 19:09:26 +01:00
|
|
|
withCustomName(v, "Local %s [copy]", path),
|
2021-11-30 21:48:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
result, err := s.Solve(ctx, st, pctx.Platform.Get())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-01 02:51:28 +01:00
|
|
|
fs := pctx.FS.New(result)
|
2021-12-09 21:20:56 +01:00
|
|
|
return compiler.NewValue().FillFields(map[string]interface{}{
|
2021-12-16 20:22:36 +01:00
|
|
|
"contents": fs.MarshalCUE(),
|
2021-12-09 21:20:56 +01:00
|
|
|
})
|
2021-11-25 01:24:04 +01:00
|
|
|
}
|