Merge pull request #1259 from TomChv/engine/mkdir

Europa: engine.#Mkdir
This commit is contained in:
Andrea Luzzardi 2021-12-21 17:53:34 +01:00 committed by GitHub
commit 9b2746b2cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 237 additions and 23 deletions

View File

@ -152,7 +152,7 @@ _No output._
## engine.#Mkdir ## engine.#Mkdir
Create a directory Create one or multiple directory in a container
### engine.#Mkdir Inputs ### engine.#Mkdir Inputs
@ -212,8 +212,6 @@ _No output._
## engine.#ReadFile ## engine.#ReadFile
Read a file from a filesystem tree
### engine.#ReadFile Inputs ### engine.#ReadFile Inputs
_No input._ _No input._

View File

@ -56,6 +56,18 @@ _No input._
_No output._ _No output._
## engine.#Mkdir
Create one or multiple directory in a container
### engine.#Mkdir Inputs
_No input._
### engine.#Mkdir Outputs
_No output._
## engine.#Mount ## engine.#Mount
A transient filesystem mount. A transient filesystem mount.

View File

@ -150,7 +150,7 @@ _No output._
## engine.#Mkdir ## engine.#Mkdir
Create a directory Create one or multiple directory in a container Create a directory
### engine.#Mkdir Inputs ### engine.#Mkdir Inputs

81
plan/task/mkdir.go Normal file
View 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
}

View File

@ -1,6 +1,26 @@
package engine package engine
// Read a file from a filesystem tree // Create one or multiple directory in a container
#Mkdir: {
$dagger: task: _name: "Mkdir"
// Container filesystem
input: #FS
// Path of the directory to create
// It can be nested (e.g : "/foo" or "/foo/bar")
path: string
// Permissions to set
mode: *0o755 | int
// If set, it creates parents' directory if they do not exist
parents: *true | false
// Modified filesystem
output: #FS
}
#ReadFile: { #ReadFile: {
$dagger: task: _name: "ReadFile" $dagger: task: _name: "ReadFile"
@ -40,23 +60,6 @@ package engine
output: #FS output: #FS
} }
// Create a directory
#Mkdir: {
@dagger(notimplemented)
$dagger: task: _name: "Mkdir"
input: #FS
// Path of the directory
path: string
// FIXME: permissions?
mode: int
// Create parent directories as needed?
parents: *true | false
output: #FS
}
// Copy files from one FS tree to another // Copy files from one FS tree to another
#Copy: { #Copy: {
$dagger: task: _name: "Copy" $dagger: task: _name: "Copy"

View File

@ -56,7 +56,23 @@ setup() {
cd "$TESTDIR"/tasks/copy cd "$TESTDIR"/tasks/copy
"$DAGGER" --europa up ./copy_exec.cue "$DAGGER" --europa up ./copy_exec.cue
"$DAGGER" --europa up ./copy_file.cue "$DAGGER" --europa up ./copy_file.cue
run "$DAGGER" --europa up ./copy_exec_invalid.cue run "$DAGGER" --europa up ./copy_exec_invalid.cue
assert_failure assert_failure
} }
@test "task: #Mkdir" {
# Make directory
cd "$TESTDIR"/tasks/mkdir
"$DAGGER" --europa up ./mkdir.cue
# Create parents
cd "$TESTDIR"/tasks/mkdir
"$DAGGER" --europa up ./mkdir_parents.cue
# Disable parents creation
cd "$TESTDIR"/tasks/mkdir
run "$DAGGER" --europa up ./mkdir_failure_disable_parents.cue
assert_failure
}

View File

@ -0,0 +1 @@
module: ""

View File

@ -0,0 +1,3 @@
# generated by dagger
alpha.dagger.io
dagger.lock

View File

@ -0,0 +1,33 @@
package main
import (
"alpha.dagger.io/europa/dagger/engine"
)
engine.#Plan & {
actions: {
image: engine.#Pull & {
source: "alpine:3.15.0@sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3"
}
mkdir: engine.#Mkdir & {
input: image.output
path: "/test"
}
writeChecker: engine.#WriteFile & {
input: mkdir.output
path: "/test/foo"
contents: "bar"
mode: 700
}
readChecker: engine.#ReadFile & {
input: writeChecker.output
path: "/test/foo"
} & {
// assert result
contents: "bar"
}
}
}

View File

@ -0,0 +1,34 @@
package main
import (
"alpha.dagger.io/europa/dagger/engine"
)
engine.#Plan & {
actions: {
image: engine.#Pull & {
source: "alpine:3.15.0@sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3"
}
mkdir: engine.#Mkdir & {
input: image.output
path: "/test/baz"
parents: false
}
writeChecker: engine.#WriteFile & {
input: mkdir.output
path: "/test/baz/foo"
contents: "bar"
mode: 700
}
readChecker: engine.#ReadFile & {
input: writeChecker.output
path: "/test/baz/foo"
} & {
// assert result
contents: "bar"
}
}
}

View File

@ -0,0 +1,33 @@
package main
import (
"alpha.dagger.io/europa/dagger/engine"
)
engine.#Plan & {
actions: {
image: engine.#Pull & {
source: "alpine:3.15.0@sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3"
}
mkdir: engine.#Mkdir & {
input: image.output
path: "/test/baz"
}
writeChecker: engine.#WriteFile & {
input: mkdir.output
path: "/test/baz/foo"
contents: "bar"
mode: 700
}
readChecker: engine.#ReadFile & {
input: writeChecker.output
path: "/test/baz/foo"
} & {
// assert result
contents: "bar"
}
}
}