Skip to content

Commit ec9f9b7

Browse files
committed
chore(cli): move 'convert' command under 'file' sub-command
1 parent 74bf5c5 commit ec9f9b7

File tree

3 files changed

+69
-51
lines changed

3 files changed

+69
-51
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595

9696
### Misc
9797

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

99103
## [v1.23.0]
100104

cmd/convert.go

+61-48
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67

78
"github.com/kong/deck/convert"
@@ -18,61 +19,73 @@ var (
1819
convertCmdAssumeYes bool
1920
)
2021

22+
func executeConvert(_ *cobra.Command, _ []string) error {
23+
sourceFormat, err := convert.ParseFormat(convertCmdSourceFormat)
24+
if err != nil {
25+
return err
26+
}
27+
destinationFormat, err := convert.ParseFormat(convertCmdDestinationFormat)
28+
if err != nil {
29+
return err
30+
}
31+
32+
if convertCmdInputFile != "" {
33+
if yes, err := utils.ConfirmFileOverwrite(
34+
convertCmdOutputFile, "", convertCmdAssumeYes,
35+
); err != nil {
36+
return err
37+
} else if !yes {
38+
return nil
39+
}
40+
41+
err = convert.Convert(convertCmdInputFile, convertCmdOutputFile, sourceFormat, destinationFormat)
42+
if err != nil {
43+
return fmt.Errorf("converting file: %w", err)
44+
}
45+
} else if is2xTo3xConversion() {
46+
path, err := os.Getwd()
47+
if err != nil {
48+
return fmt.Errorf("getting current working directory: %w", err)
49+
}
50+
files, err := utils.ConfigFilesInDir(path)
51+
if err != nil {
52+
return fmt.Errorf("getting files from directory: %w", err)
53+
}
54+
for _, filename := range files {
55+
err = convert.Convert(filename, filename, sourceFormat, destinationFormat)
56+
if err != nil {
57+
return fmt.Errorf("converting '%s' file: %w", filename, err)
58+
}
59+
}
60+
}
61+
if convertCmdDestinationFormat == "konnect" {
62+
cprint.UpdatePrintf("Warning: konnect format type was deprecated in v1.12 and it will be removed\n" +
63+
"in a future version. Please use your Kong configuration files with deck <cmd>.\n" +
64+
"Please see https://docs.konghq.com/konnect/getting-started/import/.\n")
65+
}
66+
return nil
67+
}
68+
2169
// newConvertCmd represents the convert command
22-
func newConvertCmd() *cobra.Command {
70+
func newConvertCmd(deprecated bool) *cobra.Command {
71+
short := "Convert files from one format into another format"
72+
execute := executeConvert
73+
if deprecated {
74+
short = "[deprecated] use 'file convert' instead"
75+
execute = func(cmd *cobra.Command, args []string) error {
76+
log.Println("Warning: the 'deck convert' command was deprecated and moved to 'deck file convert'")
77+
return executeConvert(cmd, args)
78+
}
79+
}
80+
2381
convertCmd := &cobra.Command{
2482
Use: "convert",
25-
Short: "Convert files from one format into another format",
83+
Short: short,
2684
Long: `The convert command changes configuration files from one format
2785
into another compatible format. For example, a configuration for 'kong-gateway-2.x'
2886
can be converted into a 'kong-gateway-3.x' configuration file.`,
2987
Args: validateNoArgs,
30-
RunE: func(cmd *cobra.Command, args []string) error {
31-
sourceFormat, err := convert.ParseFormat(convertCmdSourceFormat)
32-
if err != nil {
33-
return err
34-
}
35-
destinationFormat, err := convert.ParseFormat(convertCmdDestinationFormat)
36-
if err != nil {
37-
return err
38-
}
39-
40-
if convertCmdInputFile != "" {
41-
if yes, err := utils.ConfirmFileOverwrite(
42-
convertCmdOutputFile, "", convertCmdAssumeYes,
43-
); err != nil {
44-
return err
45-
} else if !yes {
46-
return nil
47-
}
48-
49-
err = convert.Convert(convertCmdInputFile, convertCmdOutputFile, sourceFormat, destinationFormat)
50-
if err != nil {
51-
return fmt.Errorf("converting file: %w", err)
52-
}
53-
} else if is2xTo3xConversion() {
54-
path, err := os.Getwd()
55-
if err != nil {
56-
return fmt.Errorf("getting current working directory: %w", err)
57-
}
58-
files, err := utils.ConfigFilesInDir(path)
59-
if err != nil {
60-
return fmt.Errorf("getting files from directory: %w", err)
61-
}
62-
for _, filename := range files {
63-
err = convert.Convert(filename, filename, sourceFormat, destinationFormat)
64-
if err != nil {
65-
return fmt.Errorf("converting '%s' file: %w", filename, err)
66-
}
67-
}
68-
}
69-
if convertCmdDestinationFormat == "konnect" {
70-
cprint.UpdatePrintf("Warning: konnect format type was deprecated in v1.12 and it will be removed\n" +
71-
"in a future version. Please use your Kong configuration files with deck <cmd>.\n" +
72-
"Please see https://docs.konghq.com/konnect/getting-started/import/.\n")
73-
}
74-
return nil
75-
},
88+
RunE: execute,
7689
}
7790

7891
sourceFormats := []convert.Format{convert.FormatKongGateway, convert.FormatKongGateway2x}

cmd/root.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,11 @@ It can be used to export, import, or sync entities to Kong.`,
211211
rootCmd.AddCommand(newPingCmd())
212212
rootCmd.AddCommand(newDumpCmd())
213213
rootCmd.AddCommand(newDiffCmd())
214-
rootCmd.AddCommand(newConvertCmd())
214+
rootCmd.AddCommand(newConvertCmd(true)) // deprecated, to exist under the `file` subcommand only
215215
rootCmd.AddCommand(newCompletionCmd())
216216
rootCmd.AddCommand(newKonnectCmd())
217-
// commands from go-apiops library:
218-
fileCmd := newAddFileCmd()
219217
{
218+
fileCmd := newAddFileCmd()
220219
rootCmd.AddCommand(fileCmd)
221220
fileCmd.AddCommand(newAddPluginsCmd())
222221
fileCmd.AddCommand(newAddTagsCmd())
@@ -225,6 +224,8 @@ It can be used to export, import, or sync entities to Kong.`,
225224
fileCmd.AddCommand(newMergeCmd())
226225
fileCmd.AddCommand(newPatchCmd())
227226
fileCmd.AddCommand(newOpenapi2KongCmd())
227+
fileCmd.AddCommand(newConvertCmd(false))
228+
fileCmd.AddCommand(newValidateCmd()) // alias; since this does both file+online
228229
}
229230
return rootCmd
230231
}

0 commit comments

Comments
 (0)