diff --git a/CHANGELOG.md b/CHANGELOG.md index 771d31dea..77e681d25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/cmd/convert.go b/cmd/convert.go index 0476ab2e2..3b25f42fb 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "log" "os" "github.com/kong/deck/convert" @@ -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 .\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 .\n" + - "Please see https://docs.konghq.com/konnect/getting-started/import/.\n") - } - return nil - }, + RunE: execute, } sourceFormats := []convert.Format{convert.FormatKongGateway, convert.FormatKongGateway2x} diff --git a/cmd/root.go b/cmd/root.go index 2e19fd2da..e1810e628 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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()) @@ -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 }