@@ -2,6 +2,7 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
+ "log"
5
6
"os"
6
7
7
8
"github.com/kong/deck/convert"
@@ -18,61 +19,73 @@ var (
18
19
convertCmdAssumeYes bool
19
20
)
20
21
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
+
21
69
// 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
+
23
81
convertCmd := & cobra.Command {
24
82
Use : "convert" ,
25
- Short : "Convert files from one format into another format" ,
83
+ Short : short ,
26
84
Long : `The convert command changes configuration files from one format
27
85
into another compatible format. For example, a configuration for 'kong-gateway-2.x'
28
86
can be converted into a 'kong-gateway-3.x' configuration file.` ,
29
87
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 ,
76
89
}
77
90
78
91
sourceFormats := []convert.Format {convert .FormatKongGateway , convert .FormatKongGateway2x }
0 commit comments