Add mkdir task
Signed-off-by: Vasek - Tom C <tom.chauveau@epitech.eu>
This commit is contained in:
parent
15fa9b0aac
commit
09c427f1cf
81
plan/task/mkdir.go
Normal file
81
plan/task/mkdir.go
Normal file
@ -0,0 +1,81 @@
|
||||
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 {
|
||||
}
|
||||
|
||||
func (t *mkdirTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
||||
path, err := v.Lookup("path").String()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Mode (int)
|
||||
mode, err := v.Lookup("mode").Int64()
|
||||
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
|
||||
inputState, err := input.Result().ToState()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add Mkdir operation on input llb state
|
||||
outputState := inputState.File(
|
||||
llb.Mkdir(path, fs.FileMode(mode), mkdirOpts...),
|
||||
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
|
||||
outputFs := pctx.FS.New(result)
|
||||
|
||||
// Init output
|
||||
output := compiler.NewValue()
|
||||
|
||||
if err := output.FillPath(cue.ParsePath("output"), outputFs.MarshalCUE()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return output, nil
|
||||
}
|
Reference in New Issue
Block a user