implemented "write-file" and "mkdir" ops

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-03-03 17:24:21 -08:00
parent f76e43d512
commit ee7bb3c3f5
3 changed files with 76 additions and 0 deletions

View File

@ -92,6 +92,11 @@ func (v *Value) String() (string, error) {
return v.val.String() 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 { func (v *Value) SourceUnsafe() string {
s, _ := v.SourceString() s, _ := v.SourceString()
return s return s

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/fs"
"strings" "strings"
"github.com/docker/distribution/reference" "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) return p.Subdir(ctx, op)
case "docker-build": case "docker-build":
return p.DockerBuild(ctx, op) return p.DockerBuild(ctx, op)
case "write-file":
return p.WriteFile(ctx, op)
case "mkdir":
return p.Mkdir(ctx, op)
default: default:
return fmt.Errorf("invalid operation: %s", op.JSON()) 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 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
}

View File

@ -71,3 +71,17 @@ package dagger
buildArg?: [string]: string buildArg?: [string]: string
label?: [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
}