2021-12-17 23:58:52 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/plancontext"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("Copy", func() Task { return ©Task{} })
|
|
|
|
}
|
|
|
|
|
|
|
|
type copyTask struct {
|
|
|
|
}
|
|
|
|
|
2022-03-23 23:02:17 +01:00
|
|
|
func (t *copyTask) Run(ctx context.Context, pctx *plancontext.Context, s *solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
2021-12-17 23:58:52 +01:00
|
|
|
var err error
|
|
|
|
|
|
|
|
input, err := pctx.FS.FromValue(v.Lookup("input"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-21 18:42:05 +01:00
|
|
|
inputState, err := input.State()
|
2021-12-17 23:58:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-28 04:09:12 +01:00
|
|
|
contents, err := pctx.FS.FromValue(v.Lookup("contents"))
|
2021-12-17 23:58:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-28 04:09:12 +01:00
|
|
|
contentsState, err := contents.State()
|
2021-12-17 23:58:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-28 04:09:12 +01:00
|
|
|
sourcePath, err := v.Lookup("source").String()
|
2021-12-17 23:58:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
destPath, err := v.Lookup("dest").String()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-01 03:13:49 +02:00
|
|
|
var filters struct {
|
|
|
|
Include []string
|
|
|
|
Exclude []string
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.Decode(&filters); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: allow more configurable llb options
|
|
|
|
// For now we define the following convenience presets.
|
|
|
|
opts := &llb.CopyInfo{
|
|
|
|
CopyDirContentsOnly: true,
|
|
|
|
CreateDestPath: true,
|
|
|
|
AllowWildcard: true,
|
|
|
|
IncludePatterns: filters.Include,
|
|
|
|
ExcludePatterns: filters.Exclude,
|
|
|
|
}
|
|
|
|
|
2021-12-17 23:58:52 +01:00
|
|
|
outputState := inputState.File(
|
|
|
|
llb.Copy(
|
2022-01-28 04:09:12 +01:00
|
|
|
contentsState,
|
2021-12-17 23:58:52 +01:00
|
|
|
sourcePath,
|
|
|
|
destPath,
|
2022-04-01 03:13:49 +02:00
|
|
|
opts,
|
2021-12-17 23:58:52 +01:00
|
|
|
),
|
|
|
|
withCustomName(v, "Copy %s %s", sourcePath, destPath),
|
|
|
|
)
|
|
|
|
|
|
|
|
result, err := s.Solve(ctx, outputState, pctx.Platform.Get())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := pctx.FS.New(result)
|
|
|
|
|
2021-12-20 18:41:52 +01:00
|
|
|
return compiler.NewValue().FillFields(map[string]interface{}{
|
|
|
|
"output": fs.MarshalCUE(),
|
|
|
|
})
|
2021-12-17 23:58:52 +01:00
|
|
|
}
|