From 029bd9ef960aa1f3072fac63d11d1fe88e41ad40 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Wed, 1 Nov 2023 11:53:59 +0100 Subject: [PATCH] fix(cli): deprecated commands default to kong.yaml 2 Cobra commands were generated; deprecated, and current. Both would point to the same golbal variable. The bug is that Cobra sets the defaults when CREATING the command not when parsing. This means that the last command created would set the global defaults, and hence overwrite the ones from the other command. Introduced in #962 Jira: DECK-156 --- .vscode/launch.json | 18 ++++++++++++++++++ cmd/file_convert.go | 31 +++++++++++++++++++++---------- cmd/gateway_dump.go | 24 ++++++++++++++++-------- 3 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..a3711939d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "main.go", + "args": [ + "dump", + ] + } + ] +} diff --git a/cmd/file_convert.go b/cmd/file_convert.go index 4c2ccf50c..28096d50b 100644 --- a/cmd/file_convert.go +++ b/cmd/file_convert.go @@ -13,12 +13,14 @@ import ( ) var ( - convertCmdSourceFormat string - convertCmdDestinationFormat string // konnect/kong-gateway-3.x/etc - convertCmdInputFile string - convertCmdOutputFile string - convertCmdAssumeYes bool - convertCmdStateFormat string // yaml/json output + convertCmdSourceFormat string + convertCmdDestinationFormat string // konnect/kong-gateway-3.x/etc + convertCmdInputFile string + convertCmdOutputFile string + convertCmdInputFileDeprecated string + convertCmdOutputFileDeprecated string + convertCmdAssumeYes bool + convertCmdStateFormat string // yaml/json output ) func executeConvert(_ *cobra.Command, _ []string) error { @@ -91,6 +93,8 @@ func newConvertCmd(deprecated bool) *cobra.Command { if deprecated { short = "[deprecated] use 'file convert' instead" execute = func(cmd *cobra.Command, args []string) error { + convertCmdInputFile = convertCmdInputFileDeprecated + convertCmdOutputFile = convertCmdOutputFileDeprecated cprint.UpdatePrintf("Warning: 'deck convert' is DEPRECATED and will be removed in a future version. " + "Use 'deck file convert' instead.\n") return executeConvert(cmd, args) @@ -115,10 +119,17 @@ can be converted into a 'kong-gateway-3.x' configuration file.`, fmt.Sprintf("format of the source file, allowed formats: %v", sourceFormats)) convertCmd.Flags().StringVar(&convertCmdDestinationFormat, "to", "", fmt.Sprintf("desired format of the output, allowed formats: %v", destinationFormats)) - convertCmd.Flags().StringVar(&convertCmdInputFile, "input-file", fileInDefault, - "configuration file to be converted. Use `-` to read from stdin.") - convertCmd.Flags().StringVarP(&convertCmdOutputFile, "output-file", "o", fileOutDefault, - "file to write configuration to after conversion. Use `-` to write to stdout.") + if deprecated { + convertCmd.Flags().StringVar(&convertCmdInputFileDeprecated, "input-file", fileInDefault, + "configuration file to be converted. Use `-` to read from stdin.") + convertCmd.Flags().StringVarP(&convertCmdOutputFileDeprecated, "output-file", "o", fileOutDefault, + "file to write configuration to after conversion. Use `-` to write to stdout.") + } else { + convertCmd.Flags().StringVar(&convertCmdInputFile, "input-file", fileInDefault, + "configuration file to be converted. Use `-` to read from stdin.") + convertCmd.Flags().StringVarP(&convertCmdOutputFile, "output-file", "o", fileOutDefault, + "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", diff --git a/cmd/gateway_dump.go b/cmd/gateway_dump.go index 95637646a..ebdf7f32d 100644 --- a/cmd/gateway_dump.go +++ b/cmd/gateway_dump.go @@ -17,11 +17,12 @@ import ( const defaultFileOutName = "kong" var ( - dumpCmdKongStateFile string - dumpCmdStateFormat string - dumpWorkspace string - dumpAllWorkspaces bool - dumpWithID bool + dumpCmdKongStateFileDeprecated string + dumpCmdKongStateFile string + dumpCmdStateFormat string + dumpWorkspace string + dumpAllWorkspaces bool + dumpWithID bool ) func listWorkspaces(ctx context.Context, client *kong.Client) ([]string, error) { @@ -149,6 +150,7 @@ func newDumpCmd(deprecated bool) *cobra.Command { if deprecated { short = "[deprecated] use 'gateway dump' instead" execute = func(cmd *cobra.Command, args []string) error { + dumpCmdKongStateFile = dumpCmdKongStateFileDeprecated cprint.UpdatePrintf("Warning: 'deck dump' is DEPRECATED and will be removed in a future version. " + "Use 'deck gateway dump' instead.\n") return executeDump(cmd, args) @@ -168,9 +170,15 @@ configure Kong.`, RunE: execute, } - dumpCmd.Flags().StringVarP(&dumpCmdKongStateFile, "output-file", "o", - fileOutDefault, "file to which to write Kong's configuration."+ - "Use `-` to write to stdout.") + if deprecated { + dumpCmd.Flags().StringVarP(&dumpCmdKongStateFileDeprecated, "output-file", "o", + fileOutDefault, "file to which to write Kong's configuration."+ + "Use `-` to write to stdout.") + } else { + dumpCmd.Flags().StringVarP(&dumpCmdKongStateFile, "output-file", "o", + fileOutDefault, "file to which to write Kong's configuration."+ + "Use `-` to write to stdout.") + } dumpCmd.Flags().StringVar(&dumpCmdStateFormat, "format", "yaml", "output file format: json or yaml.") dumpCmd.Flags().BoolVar(&dumpWithID, "with-id",