2018-05-14 20:03:05 +02:00
|
|
|
package jsonnet
|
|
|
|
|
|
|
|
import (
|
2018-09-15 02:25:54 +02:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2018-05-14 20:03:05 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2018-08-27 04:40:06 +02:00
|
|
|
"os"
|
2018-05-14 20:03:05 +02:00
|
|
|
|
2018-09-15 02:25:54 +02:00
|
|
|
"github.com/drone/drone-yaml/yaml"
|
|
|
|
"github.com/drone/drone-yaml/yaml/pretty"
|
2018-05-14 20:03:05 +02:00
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/google/go-jsonnet"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Command exports the jsonnet command.
|
|
|
|
var Command = cli.Command{
|
|
|
|
Name: "jsonnet",
|
|
|
|
Usage: "generate .drone.yml from jsonnet",
|
|
|
|
ArgsUsage: "[path/to/.drone.jsonnet]",
|
|
|
|
Action: func(c *cli.Context) {
|
|
|
|
if err := generate(c); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
2018-09-15 02:25:54 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "source",
|
|
|
|
Usage: "Source file",
|
|
|
|
Value: ".drone.jsonnet",
|
2018-08-27 04:40:06 +02:00
|
|
|
},
|
2018-09-15 02:25:54 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "target",
|
|
|
|
Usage: "target file",
|
|
|
|
Value: ".drone.yml",
|
2018-05-14 20:03:05 +02:00
|
|
|
},
|
2018-09-15 02:25:54 +02:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "stream",
|
|
|
|
Usage: "Write output as a YAML stream.",
|
2018-05-14 20:03:05 +02:00
|
|
|
},
|
2018-09-15 02:25:54 +02:00
|
|
|
cli.BoolTFlag{
|
|
|
|
Name: "format",
|
|
|
|
Usage: "Write output as formatted YAML",
|
2018-05-14 20:03:05 +02:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "stdout",
|
2018-09-15 02:25:54 +02:00
|
|
|
Usage: "Write output to stdout",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "string",
|
|
|
|
Usage: "Expect a string, manifest as plain text",
|
2018-05-14 20:03:05 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func generate(c *cli.Context) error {
|
2018-09-15 02:25:54 +02:00
|
|
|
source := c.String("source")
|
|
|
|
target := c.String("target")
|
2018-05-14 20:03:05 +02:00
|
|
|
|
2018-09-15 02:25:54 +02:00
|
|
|
data, err := ioutil.ReadFile(source)
|
2018-05-14 20:03:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vm := jsonnet.MakeVM()
|
2018-09-15 02:25:54 +02:00
|
|
|
vm.MaxStack = 500
|
|
|
|
vm.StringOutput = c.Bool("string")
|
|
|
|
vm.ErrorFormatter.SetMaxStackTraceSize(20)
|
2018-05-14 20:03:05 +02:00
|
|
|
vm.ErrorFormatter.SetColorFormatter(
|
|
|
|
color.New(color.FgRed).Fprintf,
|
|
|
|
)
|
|
|
|
|
2018-11-26 22:32:34 +01:00
|
|
|
// register native functions
|
|
|
|
RegisterNativeFuncs(vm)
|
|
|
|
|
2018-09-15 02:25:54 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2018-08-27 04:40:06 +02:00
|
|
|
if c.Bool("stream") {
|
2018-09-15 02:25:54 +02:00
|
|
|
docs, err := vm.EvaluateSnippetStream(source, string(data))
|
2018-08-27 04:40:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-15 02:25:54 +02:00
|
|
|
for _, doc := range docs {
|
|
|
|
buf.WriteString("---")
|
|
|
|
buf.WriteString("\n")
|
|
|
|
buf.WriteString(doc)
|
2018-08-27 04:40:06 +02:00
|
|
|
}
|
2018-09-15 02:25:54 +02:00
|
|
|
} else {
|
|
|
|
result, err := vm.EvaluateSnippet(source, string(data))
|
2018-08-27 04:40:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-15 02:25:54 +02:00
|
|
|
buf.WriteString(result)
|
2018-08-27 04:40:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 02:25:54 +02:00
|
|
|
// the yaml file is parsed and formatted by default. This
|
|
|
|
// can be disabled for --format=false.
|
|
|
|
if c.BoolT("format") {
|
|
|
|
manifest, err := yaml.Parse(buf)
|
2018-08-27 04:40:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-15 02:25:54 +02:00
|
|
|
buf.Reset()
|
|
|
|
pretty.Print(buf, manifest)
|
2018-08-27 04:40:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 02:25:54 +02:00
|
|
|
// the user can optionally write the yaml to stdout. This
|
|
|
|
// is useful for debugging purposes without mutating an
|
|
|
|
// existing file.
|
|
|
|
if c.Bool("stdout") {
|
|
|
|
io.Copy(os.Stdout, buf)
|
2018-05-14 20:03:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-27 04:40:06 +02:00
|
|
|
|
2018-09-15 02:25:54 +02:00
|
|
|
return ioutil.WriteFile(target, buf.Bytes(), 0644)
|
2018-05-14 20:03:05 +02:00
|
|
|
}
|