Skip to content

Commit 57734db

Browse files
committed
feat(convert): support json output
1 parent 9cf04c7 commit 57734db

File tree

5 files changed

+45
-12
lines changed

5 files changed

+45
-12
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
- Added a new command `file render` to render a final decK file. This will result in a file representing
9393
the state as it would be synced online.
9494
[#963](https://github.com/Kong/deck/pull/963)
95+
- Added a new flag `--format` to `file convert` to enable JSON output.
96+
[#963](https://github.com/Kong/deck/pull/963)
9597

9698
### Fixes
9799

cmd/file_convert.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ import (
44
"fmt"
55
"log"
66
"os"
7+
"strings"
78

89
"github.com/kong/deck/convert"
910
"github.com/kong/deck/cprint"
11+
"github.com/kong/deck/file"
1012
"github.com/kong/deck/utils"
1113
"github.com/spf13/cobra"
1214
)
1315

1416
var (
1517
convertCmdSourceFormat string
16-
convertCmdDestinationFormat string
18+
convertCmdDestinationFormat string // konnect/kong-gateway-3.x/etc
1719
convertCmdInputFile string
1820
convertCmdOutputFile string
1921
convertCmdAssumeYes bool
22+
convertCmdStateFormat string // yaml/json output
2023
)
2124

2225
func executeConvert(_ *cobra.Command, _ []string) error {
@@ -38,7 +41,13 @@ func executeConvert(_ *cobra.Command, _ []string) error {
3841
return nil
3942
}
4043

41-
err = convert.Convert([]string{convertCmdInputFile}, convertCmdOutputFile, sourceFormat, destinationFormat, false)
44+
err = convert.Convert(
45+
[]string{convertCmdInputFile},
46+
convertCmdOutputFile,
47+
file.Format(strings.ToUpper(convertCmdStateFormat)),
48+
sourceFormat,
49+
destinationFormat,
50+
false)
4251
if err != nil {
4352
return fmt.Errorf("converting file: %w", err)
4453
}
@@ -52,7 +61,13 @@ func executeConvert(_ *cobra.Command, _ []string) error {
5261
return fmt.Errorf("getting files from directory: %w", err)
5362
}
5463
for _, filename := range files {
55-
err = convert.Convert([]string{filename}, filename, sourceFormat, destinationFormat, false)
64+
err = convert.Convert(
65+
[]string{filename},
66+
filename,
67+
file.Format(strings.ToUpper(convertCmdStateFormat)),
68+
sourceFormat,
69+
destinationFormat,
70+
false)
5671
if err != nil {
5772
return fmt.Errorf("converting '%s' file: %w", filename, err)
5873
}
@@ -100,6 +115,9 @@ can be converted into a 'kong-gateway-3.x' configuration file.`,
100115
"file to write configuration to after conversion. Use `-` to write to stdout.")
101116
convertCmd.Flags().BoolVar(&convertCmdAssumeYes, "yes",
102117
false, "assume `yes` to prompts and run non-interactively.")
118+
convertCmd.Flags().StringVar(&convertCmdStateFormat, "format",
119+
"yaml", "output file format: json or yaml.")
120+
103121
return convertCmd
104122
}
105123

cmd/file_render.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
package cmd
22

33
import (
4+
"strings"
5+
46
"github.com/kong/deck/convert"
7+
"github.com/kong/deck/file"
58
"github.com/spf13/cobra"
69
)
710

811
var (
912
fileRenderCmdKongStateFile []string
1013
fileRenderCmdKongFileOutput string
14+
fileRenderCmdStateFormat string
1115
)
1216

1317
func executeFileRenderCmd(_ *cobra.Command, _ []string) error {
14-
return convert.Convert(fileRenderCmdKongStateFile, fileRenderCmdKongFileOutput,
15-
convert.FormatDistributed, convert.FormatKongGateway3x, true)
18+
return convert.Convert(
19+
fileRenderCmdKongStateFile,
20+
fileRenderCmdKongFileOutput,
21+
file.Format(strings.ToUpper(fileRenderCmdStateFormat)),
22+
convert.FormatDistributed,
23+
convert.FormatKongGateway3x,
24+
true)
1625
}
1726

1827
func newFileRenderCmd() *cobra.Command {
@@ -34,10 +43,8 @@ func newFileRenderCmd() *cobra.Command {
3443
renderCmd.Flags().StringVarP(&fileRenderCmdKongFileOutput, "output-file", "o",
3544
"-", "file to which to write Kong's configuration."+
3645
"Use `-` to write to stdout.")
37-
38-
// TODO: support json output
39-
// renderCmd.Flags().StringVar(&fileRenderCmdStateFormat, "format",
40-
// "yaml", "output file format: json or yaml.")
46+
renderCmd.Flags().StringVar(&fileRenderCmdStateFormat, "format",
47+
"yaml", "output file format: json or yaml.")
4148

4249
return renderCmd
4350
}

convert/convert.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ func ParseFormat(key string) (Format, error) {
5050
}
5151
}
5252

53-
func Convert(inputFilenames []string, outputFilename string, from, to Format, mockEnvVars bool) error {
54-
const outputFormat = file.YAML
53+
func Convert(
54+
inputFilenames []string,
55+
outputFilename string,
56+
outputFormat file.Format,
57+
from Format,
58+
to Format,
59+
mockEnvVars bool,
60+
) error {
5561
var (
5662
outputContent *file.Content
5763
err error

convert/convert_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func Test_Convert(t *testing.T) {
323323
for k, v := range tt.args.envVars {
324324
t.Setenv(k, v)
325325
}
326-
err := Convert(inputFiles, tt.args.outputFilename, tt.args.fromFormat,
326+
err := Convert(inputFiles, tt.args.outputFilename, file.YAML, tt.args.fromFormat,
327327
tt.args.toFormat, !tt.args.disableMocks)
328328
if (err != nil) != tt.wantErr {
329329
t.Errorf("Convert() error = %v, wantErr %v", err, tt.wantErr)

0 commit comments

Comments
 (0)