Skip to content

Commit

Permalink
chore(cli): move 'convert' command under 'file' sub-command
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jul 14, 2023
1 parent f2b24b6 commit acd54b0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@

### Misc

- Moved the `convert` command under the `file` sub-command, to be used as `deck file convert ...`. The
top level command `deck convert ...` is marked as deprecated and will be removed in a future version.
[#939](https://github.com/Kong/deck/pull/939)


## [v1.23.0]

Expand Down
109 changes: 61 additions & 48 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/kong/deck/convert"
Expand All @@ -18,61 +19,73 @@ var (
convertCmdAssumeYes bool
)

func executeConvert(_ *cobra.Command, _ []string) error {
sourceFormat, err := convert.ParseFormat(convertCmdSourceFormat)
if err != nil {
return err
}
destinationFormat, err := convert.ParseFormat(convertCmdDestinationFormat)
if err != nil {
return err
}

if convertCmdInputFile != "" {
if yes, err := utils.ConfirmFileOverwrite(
convertCmdOutputFile, "", convertCmdAssumeYes,
); err != nil {
return err
} else if !yes {
return nil
}

err = convert.Convert(convertCmdInputFile, convertCmdOutputFile, sourceFormat, destinationFormat)
if err != nil {
return fmt.Errorf("converting file: %w", err)
}
} else if is2xTo3xConversion() {
path, err := os.Getwd()
if err != nil {
return fmt.Errorf("getting current working directory: %w", err)
}
files, err := utils.ConfigFilesInDir(path)
if err != nil {
return fmt.Errorf("getting files from directory: %w", err)
}
for _, filename := range files {
err = convert.Convert(filename, filename, sourceFormat, destinationFormat)
if err != nil {
return fmt.Errorf("converting '%s' file: %w", filename, err)
}
}
}
if convertCmdDestinationFormat == "konnect" {
cprint.UpdatePrintf("Warning: konnect format type was deprecated in v1.12 and it will be removed\n" +
"in a future version. Please use your Kong configuration files with deck <cmd>.\n" +
"Please see https://docs.konghq.com/konnect/getting-started/import/.\n")
}
return nil
}

// newConvertCmd represents the convert command
func newConvertCmd() *cobra.Command {
func newConvertCmd(deprecated bool) *cobra.Command {
short := "Convert files from one format into another format"
execute := executeConvert
if deprecated {
short = "[deprecated] use 'file convert' instead"
execute = func(cmd *cobra.Command, args []string) error {
log.Println("Warning: the 'deck convert' command was deprecated and moved to 'deck file convert'")
return executeConvert(cmd, args)
}
}

convertCmd := &cobra.Command{
Use: "convert",
Short: "Convert files from one format into another format",
Short: short,
Long: `The convert command changes configuration files from one format
into another compatible format. For example, a configuration for 'kong-gateway-2.x'
can be converted into a 'kong-gateway-3.x' configuration file.`,
Args: validateNoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
sourceFormat, err := convert.ParseFormat(convertCmdSourceFormat)
if err != nil {
return err
}
destinationFormat, err := convert.ParseFormat(convertCmdDestinationFormat)
if err != nil {
return err
}

if convertCmdInputFile != "" {
if yes, err := utils.ConfirmFileOverwrite(
convertCmdOutputFile, "", convertCmdAssumeYes,
); err != nil {
return err
} else if !yes {
return nil
}

err = convert.Convert(convertCmdInputFile, convertCmdOutputFile, sourceFormat, destinationFormat)
if err != nil {
return fmt.Errorf("converting file: %w", err)
}
} else if is2xTo3xConversion() {
path, err := os.Getwd()
if err != nil {
return fmt.Errorf("getting current working directory: %w", err)
}
files, err := utils.ConfigFilesInDir(path)
if err != nil {
return fmt.Errorf("getting files from directory: %w", err)
}
for _, filename := range files {
err = convert.Convert(filename, filename, sourceFormat, destinationFormat)
if err != nil {
return fmt.Errorf("converting '%s' file: %w", filename, err)
}
}
}
if convertCmdDestinationFormat == "konnect" {
cprint.UpdatePrintf("Warning: konnect format type was deprecated in v1.12 and it will be removed\n" +
"in a future version. Please use your Kong configuration files with deck <cmd>.\n" +
"Please see https://docs.konghq.com/konnect/getting-started/import/.\n")
}
return nil
},
RunE: execute,
}

sourceFormats := []convert.Format{convert.FormatKongGateway, convert.FormatKongGateway2x}
Expand Down
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,11 @@ It can be used to export, import, or sync entities to Kong.`,
rootCmd.AddCommand(newPingCmd())
rootCmd.AddCommand(newDumpCmd())
rootCmd.AddCommand(newDiffCmd())
rootCmd.AddCommand(newConvertCmd())
rootCmd.AddCommand(newConvertCmd(true)) // deprecated, to exist under the `file` subcommand only
rootCmd.AddCommand(newCompletionCmd())
rootCmd.AddCommand(newKonnectCmd())
// commands from go-apiops library:
fileCmd := newAddFileCmd()
{
fileCmd := newAddFileCmd()
rootCmd.AddCommand(fileCmd)
fileCmd.AddCommand(newAddPluginsCmd())
fileCmd.AddCommand(newAddTagsCmd())
Expand All @@ -225,6 +224,8 @@ It can be used to export, import, or sync entities to Kong.`,
fileCmd.AddCommand(newMergeCmd())
fileCmd.AddCommand(newPatchCmd())
fileCmd.AddCommand(newOpenapi2KongCmd())
fileCmd.AddCommand(newConvertCmd(false))
fileCmd.AddCommand(newValidateCmd()) // alias; since this does both file+online
}
return rootCmd
}
Expand Down

0 comments on commit acd54b0

Please sign in to comment.