2021-12-17 20:25:20 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/fs"
|
|
|
|
|
|
|
|
"cuelang.org/go/cue"
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/plancontext"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("Mkdir", func() Task { return &mkdirTask{} })
|
|
|
|
}
|
|
|
|
|
|
|
|
type mkdirTask struct {
|
|
|
|
}
|
|
|
|
|
2022-03-23 23:02:17 +01:00
|
|
|
func (t *mkdirTask) Run(ctx context.Context, pctx *plancontext.Context, s *solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
2021-12-17 20:25:20 +01:00
|
|
|
path, err := v.Lookup("path").String()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-22 14:17:52 +01:00
|
|
|
// Permissions (int)
|
|
|
|
permissions, err := v.Lookup("permissions").Int64()
|
2021-12-17 20:25:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve options
|
|
|
|
mkdirOpts := []llb.MkdirOption{}
|
|
|
|
var opts struct {
|
|
|
|
Parents bool
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.Decode(&opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Parents {
|
|
|
|
mkdirOpts = append(mkdirOpts, llb.WithParents(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve input Filesystem
|
|
|
|
input, err := pctx.FS.FromValue(v.Lookup("input"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve input llb state
|
2022-01-03 22:34:49 +01:00
|
|
|
inputState, err := input.State()
|
2021-12-17 20:25:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add Mkdir operation on input llb state
|
|
|
|
outputState := inputState.File(
|
2021-12-22 14:17:52 +01:00
|
|
|
llb.Mkdir(path, fs.FileMode(permissions), mkdirOpts...),
|
2021-12-17 20:25:20 +01:00
|
|
|
withCustomName(v, "Mkdir %s", path),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Compute state
|
|
|
|
result, err := s.Solve(ctx, outputState, pctx.Platform.Get())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve result result filesystem
|
2021-12-17 22:17:06 +01:00
|
|
|
outputFS := pctx.FS.New(result)
|
2021-12-17 20:25:20 +01:00
|
|
|
|
|
|
|
// Init output
|
|
|
|
output := compiler.NewValue()
|
|
|
|
|
2021-12-17 22:17:06 +01:00
|
|
|
if err := output.FillPath(cue.ParsePath("output"), outputFS.MarshalCUE()); err != nil {
|
2021-12-17 20:25:20 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return output, nil
|
|
|
|
}
|