Skip to content

Commit 8e538b6

Browse files
Fix --stream combined with --format for jsonnet (#195)
* Fix `--stream` combined with `--format` for `jsonnet` If both are defined and jsonnet defines a list, it currently only generates one of the resources Also added a test for it * Minimize diffs by keeping all in one file
1 parent f25f6e5 commit 8e538b6

File tree

6 files changed

+144
-23
lines changed

6 files changed

+144
-23
lines changed

.drone.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ type: docker
33
name: default
44

55
steps:
6+
- name: test
7+
image: golang:1.16
8+
commands:
9+
- go test ./...
10+
611
- name: build
712
image: golang:1.16
813
commands:

drone/jsonnet/jsonnet.go

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,30 @@ var Command = cli.Command{
6060
}
6161

6262
func generate(c *cli.Context) error {
63-
source := c.String("source")
63+
result, err := convert(c.String("source"), c.Bool("string"), c.Bool("format"), c.Bool("stream"), c.StringSlice("extVar"))
64+
if err != nil {
65+
return err
66+
}
67+
68+
// the user can optionally write the yaml to stdout. This is useful for debugging purposes without mutating an existing file.
69+
if c.Bool("stdout") {
70+
io.WriteString(os.Stdout, result)
71+
return nil
72+
}
73+
6474
target := c.String("target")
75+
return ioutil.WriteFile(target, []byte(result), 0644)
76+
}
6577

78+
func convert(source string, stringOutput bool, format bool, stream bool, vars []string) (string, error) {
6679
data, err := ioutil.ReadFile(source)
6780
if err != nil {
68-
return err
81+
return "", err
6982
}
7083

7184
vm := jsonnet.MakeVM()
7285
vm.MaxStack = 500
73-
vm.StringOutput = c.Bool("string")
86+
vm.StringOutput = stringOutput
7487
vm.ErrorFormatter.SetMaxStackTraceSize(20)
7588
vm.ErrorFormatter.SetColorFormatter(
7689
color.New(color.FgRed).Fprintf,
@@ -80,49 +93,55 @@ func generate(c *cli.Context) error {
8093
RegisterNativeFuncs(vm)
8194

8295
// extVars
83-
vars := c.StringSlice("extVar")
8496
for _, v := range vars {
8597
name, value, err := getVarVal(v)
8698
if err != nil {
87-
return err
99+
return "", err
88100
}
89101
vm.ExtVar(name, value)
90102
}
91103

104+
formatDoc := func(doc []byte) ([]byte, error) {
105+
// enable yaml output
106+
if format {
107+
formatted, yErr := yaml.JSONToYAML(doc)
108+
if yErr != nil {
109+
return nil, fmt.Errorf("failed to convert to YAML: %v", yErr)
110+
}
111+
return formatted, nil
112+
}
113+
return doc, nil
114+
}
115+
92116
buf := new(bytes.Buffer)
93-
if c.Bool("stream") {
117+
if stream {
94118
docs, err := vm.EvaluateSnippetStream(source, string(data))
95119
if err != nil {
96-
return err
120+
return "", err
97121
}
98122
for _, doc := range docs {
123+
formatted, err := formatDoc([]byte(doc))
124+
if err != nil {
125+
return "", err
126+
}
127+
99128
buf.WriteString("---")
100129
buf.WriteString("\n")
101-
buf.WriteString(doc)
130+
buf.Write(formatted)
102131
}
103132
} else {
104133
result, err := vm.EvaluateSnippet(source, string(data))
105134
if err != nil {
106-
return err
135+
return "", err
107136
}
108-
buf.WriteString(result)
109-
}
110-
// enable yaml output
111-
if c.Bool("format") {
112-
formatted, yErr := yaml.JSONToYAML(buf.Bytes())
113-
if yErr != nil {
114-
return fmt.Errorf("failed to convert to YAML: %v", yErr)
137+
formatted, err := formatDoc([]byte(result))
138+
if err != nil {
139+
return "", err
115140
}
116-
buf.Reset()
117141
buf.Write(formatted)
118142
}
119-
// the user can optionally write the yaml to stdout. This is useful for debugging purposes without mutating an existing file.
120-
if c.Bool("stdout") {
121-
io.Copy(os.Stdout, buf)
122-
return nil
123-
}
124143

125-
return ioutil.WriteFile(target, buf.Bytes(), 0644)
144+
return buf.String(), nil
126145
}
127146

128147
// https://github.com/google/go-jsonnet/blob/master/cmd/jsonnet/cmd.go#L149

drone/jsonnet/jsonnet_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package jsonnet
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestConvert(t *testing.T) {
12+
testcases := []struct {
13+
name string
14+
jsonnetFile, yamlFile string
15+
stringOutput, format, stream bool
16+
extVars []string
17+
}{
18+
{
19+
name: "Stream + Format",
20+
jsonnetFile: "stream_format.jsonnet",
21+
yamlFile: "stream_format.yaml",
22+
format: true, stream: true,
23+
},
24+
}
25+
26+
for _, tc := range testcases {
27+
tc := tc
28+
t.Run(tc.name, func(t *testing.T) {
29+
expected, err := os.ReadFile(filepath.Join("./testdata", tc.yamlFile))
30+
assert.NoError(t, err)
31+
32+
result, err := convert(filepath.Join("./testdata", tc.jsonnetFile), tc.stringOutput, tc.format, tc.stream, tc.extVars)
33+
assert.NoError(t, err)
34+
assert.Equal(t, string(expected), result)
35+
})
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local pipeline(name) =
2+
{
3+
kind: 'pipeline',
4+
type: 'docker',
5+
name: name,
6+
platform: {
7+
os: 'linux',
8+
arch: 'amd64',
9+
},
10+
steps: [
11+
{
12+
name: 'test',
13+
image: 'golang:1.16',
14+
commands: ['go test ./...'],
15+
},
16+
{
17+
name: 'build',
18+
image: 'golang:1.16',
19+
commands: ['go build ./...'],
20+
},
21+
],
22+
};
23+
24+
[
25+
pipeline('first'),
26+
pipeline('second'),
27+
]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
kind: pipeline
3+
name: first
4+
platform:
5+
arch: amd64
6+
os: linux
7+
steps:
8+
- commands:
9+
- go test ./...
10+
image: golang:1.16
11+
name: test
12+
- commands:
13+
- go build ./...
14+
image: golang:1.16
15+
name: build
16+
type: docker
17+
---
18+
kind: pipeline
19+
name: second
20+
platform:
21+
arch: amd64
22+
os: linux
23+
steps:
24+
- commands:
25+
- go test ./...
26+
image: golang:1.16
27+
name: test
28+
- commands:
29+
- go build ./...
30+
image: golang:1.16
31+
name: build
32+
type: docker

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/mattn/go-colorable v0.1.4
2121
github.com/mattn/go-isatty v0.0.11
2222
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
23+
github.com/stretchr/testify v1.4.0
2324
github.com/urfave/cli v1.20.0
2425
go.starlark.net v0.0.0-20201118183435-e55f603d8c79
2526
golang.org/x/net v0.0.0-20190603091049-60506f45cf65

0 commit comments

Comments
 (0)