Merge pull request #305 from fkautz/pr_out_add_support_for_input_file_in_dagger_compute
Add support for input file in dagger compute
This commit is contained in:
@@ -108,6 +108,11 @@ func (v *Value) Exists() bool {
|
||||
return v.val.Exists()
|
||||
}
|
||||
|
||||
// Proxy function to the underlying cue.Value
|
||||
func (v *Value) Bytes() ([]byte, error) {
|
||||
return v.val.Bytes()
|
||||
}
|
||||
|
||||
// Proxy function to the underlying cue.Value
|
||||
func (v *Value) String() (string, error) {
|
||||
return v.val.String()
|
||||
|
@@ -3,8 +3,11 @@ package dagger
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
|
||||
"dagger.io/go/dagger/compiler"
|
||||
)
|
||||
|
||||
@@ -29,6 +32,7 @@ const (
|
||||
InputTypeText InputType = "text"
|
||||
InputTypeJSON InputType = "json"
|
||||
InputTypeYAML InputType = "yaml"
|
||||
InputTypeFile InputType = "file"
|
||||
InputTypeEmpty InputType = ""
|
||||
)
|
||||
|
||||
@@ -41,6 +45,7 @@ type Input struct {
|
||||
Text *textInput `json:"text,omitempty"`
|
||||
JSON *jsonInput `json:"json,omitempty"`
|
||||
YAML *yamlInput `json:"yaml,omitempty"`
|
||||
File *fileInput `json:"file,omitempty"`
|
||||
}
|
||||
|
||||
func (i Input) Compile() (*compiler.Value, error) {
|
||||
@@ -57,6 +62,8 @@ func (i Input) Compile() (*compiler.Value, error) {
|
||||
return i.JSON.Compile()
|
||||
case InputTypeYAML:
|
||||
return i.YAML.Compile()
|
||||
case InputTypeFile:
|
||||
return i.File.Compile()
|
||||
case "":
|
||||
return nil, fmt.Errorf("input has not been set")
|
||||
default:
|
||||
@@ -211,3 +218,28 @@ type yamlInput struct {
|
||||
func (i yamlInput) Compile() (*compiler.Value, error) {
|
||||
return compiler.DecodeYAML("", []byte(i.Data))
|
||||
}
|
||||
|
||||
func FileInput(data string) Input {
|
||||
return Input{
|
||||
Type: InputTypeFile,
|
||||
File: &fileInput{
|
||||
Path: data,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type fileInput struct {
|
||||
Path string `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func (i fileInput) Compile() (*compiler.Value, error) {
|
||||
data, err := ioutil.ReadFile(i.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value := compiler.NewValue()
|
||||
if err := value.FillPath(cue.MakePath(), data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
@@ -798,7 +798,21 @@ func (p *Pipeline) DockerBuild(ctx context.Context, op *compiler.Value, st llb.S
|
||||
}
|
||||
|
||||
func (p *Pipeline) WriteFile(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
|
||||
content, err := op.Lookup("content").String()
|
||||
var content []byte
|
||||
var err error
|
||||
|
||||
switch kind := op.Lookup("content").Kind(); kind {
|
||||
case cue.BytesKind:
|
||||
content, err = op.Lookup("content").Bytes()
|
||||
case cue.StringKind:
|
||||
var str string
|
||||
str, err = op.Lookup("content").String()
|
||||
if err == nil {
|
||||
content = []byte(str)
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("unhandled data type in WriteFile: %s", kind)
|
||||
}
|
||||
if err != nil {
|
||||
return st, err
|
||||
}
|
||||
@@ -814,7 +828,7 @@ func (p *Pipeline) WriteFile(ctx context.Context, op *compiler.Value, st llb.Sta
|
||||
}
|
||||
|
||||
return st.File(
|
||||
llb.Mkfile(dest, fs.FileMode(mode), []byte(content)),
|
||||
llb.Mkfile(dest, fs.FileMode(mode), content),
|
||||
llb.WithCustomName(p.vertexNamef("WriteFile %s", dest)),
|
||||
), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user