1
0
Fork 0
mirror of https://github.com/drone/drone-cli.git synced 2024-05-19 13:46:02 +02:00
drone-cli/drone/jsonnet/jsonnet.go

177 lines
3.7 KiB
Go
Raw Normal View History

2018-05-14 20:03:05 +02:00
package jsonnet
import (
2018-09-15 02:25:54 +02:00
"bytes"
"fmt"
2018-09-15 02:25:54 +02:00
"io"
2018-05-14 20:03:05 +02:00
"io/ioutil"
"log"
2018-08-27 04:40:06 +02:00
"os"
"path/filepath"
"strings"
2018-05-14 20:03:05 +02:00
"github.com/fatih/color"
"github.com/ghodss/yaml"
2018-05-14 20:03:05 +02:00
"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
},
2021-05-27 23:47:51 +02:00
cli.BoolFlag{
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
},
cli.StringSliceFlag{
Name: "extVar, V",
Usage: "Pass extVars to Jsonnet (can be specified multiple times)",
},
cli.StringSliceFlag{
Name: "jpath, J",
Usage: "Specify an additional library search dir (right-most wins)",
},
2018-05-14 20:03:05 +02:00
},
}
func generate(c *cli.Context) error {
result, err := convert(
c.String("source"),
c.Bool("string"),
c.Bool("format"),
c.Bool("stream"),
c.StringSlice("extVar"),
c.StringSlice("jpath"),
)
if err != nil {
return err
}
// 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.WriteString(os.Stdout, result)
return nil
}
2018-09-15 02:25:54 +02:00
target := c.String("target")
return ioutil.WriteFile(target, []byte(result), 0644)
}
2018-05-14 20:03:05 +02:00
func convert(source string, stringOutput bool, format bool, stream bool, vars []string, jpath []string) (string, error) {
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
2018-05-14 20:03:05 +02:00
}
vm := jsonnet.MakeVM()
2018-09-15 02:25:54 +02:00
vm.MaxStack = 500
vm.StringOutput = stringOutput
2018-09-15 02:25:54 +02:00
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)
jsonnetPath := filepath.SplitList(os.Getenv("JSONNET_PATH"))
jsonnetPath = append(jsonnetPath, jpath...)
vm.Importer(&jsonnet.FileImporter{
JPaths: jsonnetPath,
})
// extVars
for _, v := range vars {
name, value, err := getVarVal(v)
if err != nil {
return "", err
}
vm.ExtVar(name, value)
}
formatDoc := func(doc []byte) ([]byte, error) {
// enable yaml output
if format {
formatted, yErr := yaml.JSONToYAML(doc)
if yErr != nil {
return nil, fmt.Errorf("failed to convert to YAML: %v", yErr)
}
return formatted, nil
}
return doc, nil
}
2018-09-15 02:25:54 +02:00
buf := new(bytes.Buffer)
if 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-08-27 04:40:06 +02:00
}
2018-09-15 02:25:54 +02:00
for _, doc := range docs {
formatted, err := formatDoc([]byte(doc))
if err != nil {
return "", err
}
2018-09-15 02:25:54 +02:00
buf.WriteString("---")
buf.WriteString("\n")
buf.Write(formatted)
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-08-27 04:40:06 +02:00
}
formatted, err := formatDoc([]byte(result))
if err != nil {
return "", err
2018-08-27 04:40:06 +02:00
}
buf.Write(formatted)
2018-08-27 04:40:06 +02:00
}
return buf.String(), nil
2018-05-14 20:03:05 +02:00
}
// https://github.com/google/go-jsonnet/blob/master/cmd/jsonnet/cmd.go#L149
func getVarVal(s string) (string, string, error) {
parts := strings.SplitN(s, "=", 2)
name := parts[0]
if len(parts) == 1 {
content, exists := os.LookupEnv(name)
if exists {
return name, content, nil
}
return "", "", fmt.Errorf("environment variable %v was undefined", name)
}
return name, parts[1], nil
}