2021-06-03 13:06:57 +00:00
|
|
|
package jsonnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-03 13:17:39 +00:00
|
|
|
"fmt"
|
2021-06-03 13:06:57 +00:00
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
|
|
|
|
|
|
|
"github.com/google/go-jsonnet"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Parse(req *core.ConvertArgs, template *core.Template, templateData map[string]interface{}) (string, error) {
|
|
|
|
// create the jsonnet vm
|
|
|
|
vm := jsonnet.MakeVM()
|
|
|
|
vm.MaxStack = 500
|
|
|
|
vm.StringOutput = false
|
|
|
|
vm.ErrorFormatter.SetMaxStackTraceSize(20)
|
|
|
|
|
|
|
|
var jsonnetFile string
|
|
|
|
var jsonentFileName string
|
|
|
|
if template != nil {
|
|
|
|
jsonnetFile = template.Data
|
|
|
|
jsonentFileName = template.Name
|
|
|
|
} else {
|
|
|
|
jsonnetFile = req.Config.Data
|
|
|
|
jsonentFileName = req.Repo.Config
|
|
|
|
}
|
|
|
|
// map external inputs
|
|
|
|
if len(templateData) != 0 {
|
|
|
|
for k, v := range templateData {
|
2021-06-03 13:17:39 +00:00
|
|
|
key := fmt.Sprintf("input." + k)
|
|
|
|
val := fmt.Sprint(v)
|
|
|
|
vm.ExtVar(key, val)
|
2021-06-03 13:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// convert the jsonnet file to yaml
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
docs, err := vm.EvaluateSnippetStream(jsonentFileName, jsonnetFile)
|
|
|
|
if err != nil {
|
|
|
|
doc, err2 := vm.EvaluateSnippet(jsonentFileName, jsonnetFile)
|
|
|
|
if err2 != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
docs = append(docs, doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the jsonnet vm returns a stream of yaml documents
|
|
|
|
// that need to be combined into a single yaml file.
|
|
|
|
for _, doc := range docs {
|
|
|
|
buf.WriteString("---")
|
|
|
|
buf.WriteString("\n")
|
|
|
|
buf.WriteString(doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|