Skip to content

Commit

Permalink
feat(convert): support json output
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jul 17, 2023
1 parent 9cf04c7 commit 57734db
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
- Added a new command `file render` to render a final decK file. This will result in a file representing
the state as it would be synced online.
[#963](https://github.com/Kong/deck/pull/963)
- Added a new flag `--format` to `file convert` to enable JSON output.
[#963](https://github.com/Kong/deck/pull/963)

### Fixes

Expand Down
24 changes: 21 additions & 3 deletions cmd/file_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import (
"fmt"
"log"
"os"
"strings"

"github.com/kong/deck/convert"
"github.com/kong/deck/cprint"
"github.com/kong/deck/file"
"github.com/kong/deck/utils"
"github.com/spf13/cobra"
)

var (
convertCmdSourceFormat string
convertCmdDestinationFormat string
convertCmdDestinationFormat string // konnect/kong-gateway-3.x/etc
convertCmdInputFile string
convertCmdOutputFile string
convertCmdAssumeYes bool
convertCmdStateFormat string // yaml/json output
)

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

err = convert.Convert([]string{convertCmdInputFile}, convertCmdOutputFile, sourceFormat, destinationFormat, false)
err = convert.Convert(
[]string{convertCmdInputFile},
convertCmdOutputFile,
file.Format(strings.ToUpper(convertCmdStateFormat)),
sourceFormat,
destinationFormat,
false)
if err != nil {
return fmt.Errorf("converting file: %w", err)
}
Expand All @@ -52,7 +61,13 @@ func executeConvert(_ *cobra.Command, _ []string) error {
return fmt.Errorf("getting files from directory: %w", err)
}
for _, filename := range files {
err = convert.Convert([]string{filename}, filename, sourceFormat, destinationFormat, false)
err = convert.Convert(
[]string{filename},
filename,
file.Format(strings.ToUpper(convertCmdStateFormat)),
sourceFormat,
destinationFormat,
false)
if err != nil {
return fmt.Errorf("converting '%s' file: %w", filename, err)
}
Expand Down Expand Up @@ -100,6 +115,9 @@ can be converted into a 'kong-gateway-3.x' configuration file.`,
"file to write configuration to after conversion. Use `-` to write to stdout.")
convertCmd.Flags().BoolVar(&convertCmdAssumeYes, "yes",
false, "assume `yes` to prompts and run non-interactively.")
convertCmd.Flags().StringVar(&convertCmdStateFormat, "format",
"yaml", "output file format: json or yaml.")

return convertCmd
}

Expand Down
19 changes: 13 additions & 6 deletions cmd/file_render.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package cmd

import (
"strings"

"github.com/kong/deck/convert"
"github.com/kong/deck/file"
"github.com/spf13/cobra"
)

var (
fileRenderCmdKongStateFile []string
fileRenderCmdKongFileOutput string
fileRenderCmdStateFormat string
)

func executeFileRenderCmd(_ *cobra.Command, _ []string) error {
return convert.Convert(fileRenderCmdKongStateFile, fileRenderCmdKongFileOutput,
convert.FormatDistributed, convert.FormatKongGateway3x, true)
return convert.Convert(
fileRenderCmdKongStateFile,
fileRenderCmdKongFileOutput,
file.Format(strings.ToUpper(fileRenderCmdStateFormat)),
convert.FormatDistributed,
convert.FormatKongGateway3x,
true)
}

func newFileRenderCmd() *cobra.Command {
Expand All @@ -34,10 +43,8 @@ func newFileRenderCmd() *cobra.Command {
renderCmd.Flags().StringVarP(&fileRenderCmdKongFileOutput, "output-file", "o",
"-", "file to which to write Kong's configuration."+
"Use `-` to write to stdout.")

// TODO: support json output
// renderCmd.Flags().StringVar(&fileRenderCmdStateFormat, "format",
// "yaml", "output file format: json or yaml.")
renderCmd.Flags().StringVar(&fileRenderCmdStateFormat, "format",
"yaml", "output file format: json or yaml.")

return renderCmd
}
10 changes: 8 additions & 2 deletions convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ func ParseFormat(key string) (Format, error) {
}
}

func Convert(inputFilenames []string, outputFilename string, from, to Format, mockEnvVars bool) error {
const outputFormat = file.YAML
func Convert(
inputFilenames []string,
outputFilename string,
outputFormat file.Format,
from Format,
to Format,
mockEnvVars bool,
) error {
var (
outputContent *file.Content
err error
Expand Down
2 changes: 1 addition & 1 deletion convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func Test_Convert(t *testing.T) {
for k, v := range tt.args.envVars {
t.Setenv(k, v)
}
err := Convert(inputFiles, tt.args.outputFilename, tt.args.fromFormat,
err := Convert(inputFiles, tt.args.outputFilename, file.YAML, tt.args.fromFormat,
tt.args.toFormat, !tt.args.disableMocks)
if (err != nil) != tt.wantErr {
t.Errorf("Convert() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit 57734db

Please sign in to comment.