Merge pull request #1208 from aluzzardi/engine-readfile

engine.#ReadFile
This commit is contained in:
Andrea Luzzardi 2021-12-15 16:07:16 +01:00 committed by GitHub
commit ba984c4d03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -8,3 +8,8 @@ setup() {
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"
}
}
}