diff --git a/dagger/compiler/value.go b/dagger/compiler/value.go index f5c8ee85..e04db9a3 100644 --- a/dagger/compiler/value.go +++ b/dagger/compiler/value.go @@ -92,6 +92,11 @@ func (v *Value) String() (string, error) { return v.val.String() } +// Proxy function to the underlying cue.Value +func (v *Value) Int64() (int64, error) { + return v.val.Int64() +} + func (v *Value) SourceUnsafe() string { s, _ := v.SourceString() return s diff --git a/dagger/pipeline.go b/dagger/pipeline.go index 6b8784ab..10c5bdd0 100644 --- a/dagger/pipeline.go +++ b/dagger/pipeline.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "io/fs" "strings" "github.com/docker/distribution/reference" @@ -167,6 +168,10 @@ func (p *Pipeline) doOp(ctx context.Context, op *compiler.Value) error { return p.Subdir(ctx, op) case "docker-build": return p.DockerBuild(ctx, op) + case "write-file": + return p.WriteFile(ctx, op) + case "mkdir": + return p.Mkdir(ctx, op) default: return fmt.Errorf("invalid operation: %s", op.JSON()) } @@ -679,3 +684,55 @@ func (p *Pipeline) DockerBuild(ctx context.Context, op *compiler.Value) error { return nil } + +func (p *Pipeline) WriteFile(ctx context.Context, op *compiler.Value) error { + content, err := op.Get("content").String() + if err != nil { + return err + } + + dest, err := op.Get("dest").String() + if err != nil { + return err + } + + mode, err := op.Get("mode").Int64() + if err != nil { + return err + } + + p.fs = p.fs.Change(func(st llb.State) llb.State { + return st.File( + llb.Mkfile(dest, fs.FileMode(mode), []byte(content)), + llb.WithCustomName(p.vertexNamef("WriteFile %s", dest)), + ) + }) + + return nil +} + +func (p *Pipeline) Mkdir(ctx context.Context, op *compiler.Value) error { + path, err := op.Get("path").String() + if err != nil { + return err + } + + dir, err := op.Get("dir").String() + if err != nil { + return err + } + + mode, err := op.Get("mode").Int64() + if err != nil { + return err + } + + p.fs = p.fs.Change(func(st llb.State) llb.State { + return st.Dir(dir).File( + llb.Mkdir(path, fs.FileMode(mode)), + llb.WithCustomName(p.vertexNamef("Mkdir %s", path)), + ) + }) + + return nil +} diff --git a/stdlib/dagger/dagger.cue b/stdlib/dagger/dagger.cue index ecdc79c5..3fbcc721 100644 --- a/stdlib/dagger/dagger.cue +++ b/stdlib/dagger/dagger.cue @@ -71,3 +71,17 @@ package dagger buildArg?: [string]: string label?: [string]: string } + +#WriteFile: { + do: "write-file" + content: string + dest: string + mode: int | *0o644 +} + +#Mkdir: { + do: "mkdir" + dir: *"/" | string + path: string + mode: int | *0o755 +}