-
Notifications
You must be signed in to change notification settings - Fork 180
/
jsonnet.go
176 lines (161 loc) · 3.75 KB
/
jsonnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package jsonnet
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/ghodss/yaml"
"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{
cli.StringFlag{
Name: "source",
Usage: "Source file",
Value: ".drone.jsonnet",
},
cli.StringFlag{
Name: "target",
Usage: "target file",
Value: ".drone.yml",
},
cli.BoolFlag{
Name: "stream",
Usage: "Write output as a YAML stream.",
},
cli.BoolFlag{
Name: "format",
Usage: "Write output as formatted YAML",
},
cli.BoolFlag{
Name: "stdout",
Usage: "Write output to stdout",
},
cli.BoolFlag{
Name: "string",
Usage: "Expect a string, manifest as plain text",
},
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)",
},
},
}
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
}
target := c.String("target")
return ioutil.WriteFile(target, []byte(result), 0644)
}
func convert(source string, stringOutput bool, format bool, stream bool, vars []string, jpath []string) (string, error) {
data, err := ioutil.ReadFile(source)
if err != nil {
return "", err
}
vm := jsonnet.MakeVM()
vm.MaxStack = 500
vm.StringOutput = stringOutput
vm.ErrorFormatter.SetMaxStackTraceSize(20)
vm.ErrorFormatter.SetColorFormatter(
color.New(color.FgRed).Fprintf,
)
// 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
}
buf := new(bytes.Buffer)
if stream {
docs, err := vm.EvaluateSnippetStream(source, string(data))
if err != nil {
return "", err
}
for _, doc := range docs {
formatted, err := formatDoc([]byte(doc))
if err != nil {
return "", err
}
buf.WriteString("---")
buf.WriteString("\n")
buf.Write(formatted)
}
} else {
result, err := vm.EvaluateSnippet(source, string(data))
if err != nil {
return "", err
}
formatted, err := formatDoc([]byte(result))
if err != nil {
return "", err
}
buf.Write(formatted)
}
return buf.String(), nil
}
// 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
}