engine.#ReadFile

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-12-13 17:48:32 +01:00
parent b25984dad9
commit dd2fef8a2d
7 changed files with 96 additions and 0 deletions

View File

@ -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.

46
plan/task/readfile.go Normal file
View File

@ -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
}

View File

@ -0,0 +1,10 @@
package engine
#ReadFile: {
_type: "ReadFile"
input: #FS
path: string
contents: string
output: #FS
}

View File

@ -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
}

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,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"
}
}
}