From dd2fef8a2dd0de967a61832fcb383ee0434d67e8 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Mon, 13 Dec 2021 17:48:32 +0100 Subject: [PATCH] engine.#ReadFile Signed-off-by: Andrea Luzzardi --- docs/reference/europa/dagger/engine/README.md | 10 ++++ plan/task/readfile.go | 46 +++++++++++++++++++ stdlib/europa/dagger/engine/fs.cue | 10 ++++ tests/tasks.bats | 5 ++ tests/tasks/readfile/cue.mod/module.cue | 1 + tests/tasks/readfile/cue.mod/pkg/.gitignore | 3 ++ tests/tasks/readfile/readfile.cue | 21 +++++++++ 7 files changed, 96 insertions(+) create mode 100644 plan/task/readfile.go create mode 100644 stdlib/europa/dagger/engine/fs.cue create mode 100644 tests/tasks/readfile/cue.mod/module.cue create mode 100644 tests/tasks/readfile/cue.mod/pkg/.gitignore create mode 100644 tests/tasks/readfile/readfile.cue diff --git a/docs/reference/europa/dagger/engine/README.md b/docs/reference/europa/dagger/engine/README.md index 6bb38f9c..f2ecc560 100644 --- a/docs/reference/europa/dagger/engine/README.md +++ b/docs/reference/europa/dagger/engine/README.md @@ -66,6 +66,16 @@ _No input._ _No output._ +## engine.#ReadFile + +### engine.#ReadFile Inputs + +_No input._ + +### engine.#ReadFile Outputs + +_No output._ + ## engine.#Secret A reference to an external secret, for example: - A password - A SSH private key - An API token Secrets are never merged in the Cue tree. They can only be used by a special filesystem mount designed to minimize leak risk. diff --git a/plan/task/readfile.go b/plan/task/readfile.go new file mode 100644 index 00000000..cb6a1f25 --- /dev/null +++ b/plan/task/readfile.go @@ -0,0 +1,46 @@ +package task + +import ( + "context" + "fmt" + "io/fs" + + "cuelang.org/go/cue" + "go.dagger.io/dagger/compiler" + "go.dagger.io/dagger/plancontext" + "go.dagger.io/dagger/solver" +) + +func init() { + Register("ReadFile", func() Task { return &readFileTask{} }) +} + +type readFileTask struct { +} + +func (t *readFileTask) 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 + } + + input, err := pctx.FS.FromValue(v.Lookup("input")) + if err != nil { + return nil, err + } + inputFS := solver.NewBuildkitFS(input.Result()) + + // FIXME: we should create an intermediate image containing only `path`. + // That way, on cache misses, we'll only download the layer with the file contents rather than the entire FS. + contents, err := fs.ReadFile(inputFS, path) + if err != nil { + return nil, fmt.Errorf("ReadFile %s: %w", path, err) + } + + output := compiler.NewValue() + if err := output.FillPath(cue.ParsePath("contents"), string(contents)); err != nil { + return nil, err + } + + return output, nil +} diff --git a/stdlib/europa/dagger/engine/fs.cue b/stdlib/europa/dagger/engine/fs.cue new file mode 100644 index 00000000..7632d6be --- /dev/null +++ b/stdlib/europa/dagger/engine/fs.cue @@ -0,0 +1,10 @@ +package engine + +#ReadFile: { + _type: "ReadFile" + + input: #FS + path: string + contents: string + output: #FS +} diff --git a/tests/tasks.bats b/tests/tasks.bats index 2eed09a8..8036dffd 100644 --- a/tests/tasks.bats +++ b/tests/tasks.bats @@ -7,4 +7,9 @@ setup() { @test "task: #Pull" { cd "$TESTDIR"/tasks/pull dagger --europa up +} + +@test "task: #ReadFile" { + cd "$TESTDIR"/tasks/readfile + dagger --europa up } \ No newline at end of file diff --git a/tests/tasks/readfile/cue.mod/module.cue b/tests/tasks/readfile/cue.mod/module.cue new file mode 100644 index 00000000..f8af9cef --- /dev/null +++ b/tests/tasks/readfile/cue.mod/module.cue @@ -0,0 +1 @@ +module: "" diff --git a/tests/tasks/readfile/cue.mod/pkg/.gitignore b/tests/tasks/readfile/cue.mod/pkg/.gitignore new file mode 100644 index 00000000..2d4dc1ae --- /dev/null +++ b/tests/tasks/readfile/cue.mod/pkg/.gitignore @@ -0,0 +1,3 @@ +# generated by dagger +alpha.dagger.io +dagger.lock diff --git a/tests/tasks/readfile/readfile.cue b/tests/tasks/readfile/readfile.cue new file mode 100644 index 00000000..65603d6d --- /dev/null +++ b/tests/tasks/readfile/readfile.cue @@ -0,0 +1,21 @@ +package main + +import ( + "alpha.dagger.io/europa/dagger/engine" +) + +engine.#Plan & { + actions: { + image: engine.#Pull & { + source: "alpine:3.15.0@sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3" + } + + readfile: engine.#ReadFile & { + input: image.output + path: "/etc/alpine-release" + } & { + // assert result + contents: "3.15.0\n" + } + } +}