-
Notifications
You must be signed in to change notification settings - Fork 127
/
convert.go
106 lines (98 loc) · 3.78 KB
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/kong/deck/convert"
"github.com/kong/deck/cprint"
"github.com/kong/deck/utils"
"github.com/spf13/cobra"
)
var (
convertCmdSourceFormat string
convertCmdDestinationFormat string
convertCmdInputFile string
convertCmdOutputFile string
convertCmdInputFolder string
convertCmdOutputFolder string
)
// newConvertCmd represents the convert command
func newConvertCmd() *cobra.Command {
convertCmd := &cobra.Command{
Use: "convert",
Short: "Convert files from one format into another format",
Long: `The convert command changes configuration files from one format
into another compatible format. For example, a configuration for 'kong-gateway'
can be converted into a 'konnect' 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 convertCmdInputFolder == "" {
if yes, err := utils.ConfirmFileOverwrite(convertCmdOutputFile, "", false); err != nil {
return err
} else if !yes {
return nil
}
err = convert.Convert(convertCmdInputFile, convertCmdOutputFile, sourceFormat, destinationFormat)
if err != nil {
return fmt.Errorf("converting file: %v", err)
}
} else {
files, err := getConfigurationFiles()
if err != nil {
return fmt.Errorf("getting files from directory: %w", err)
}
for _, filename := range files {
outputFile := filepath.Join(convertCmdOutputFolder, filepath.Base(filename))
if _, err := os.Stat(convertCmdOutputFolder); os.IsNotExist(err) {
if err := os.Mkdir(convertCmdOutputFolder, 0o700); err != nil {
return fmt.Errorf("creating output folder: %w", err)
}
}
err = convert.Convert(filename, outputFile, sourceFormat, destinationFormat)
if err != nil {
return fmt.Errorf("converting '%s' file: %v", 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/deployment/import.\n")
}
return nil
},
}
sourceFormats := []convert.Format{convert.FormatKongGateway, convert.FormatKongGateway2x}
destinationFormats := []convert.Format{convert.FormatKonnect, convert.FormatKongGateway3x}
convertCmd.Flags().StringVar(&convertCmdSourceFormat, "from", "",
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", "",
"configuration file to be converted. Use `-` to read from stdin.")
convertCmd.Flags().StringVar(&convertCmdOutputFile, "output-file", "",
"file to write configuration to after conversion. Use `-` to write to stdout.")
convertCmd.Flags().StringVar(&convertCmdInputFolder, "input-folder", "",
"configuration folder to be converted.")
convertCmd.Flags().StringVar(&convertCmdOutputFolder, "output-folder", "",
"folder to write configuration to after conversion.")
return convertCmd
}
func getConfigurationFiles() ([]string, error) {
finfo, err := os.Stat(convertCmdInputFolder)
if err != nil {
return nil, fmt.Errorf("reading input-folder: %w", err)
}
if !finfo.IsDir() {
return nil, fmt.Errorf("not a directory: %s", convertCmdInputFolder)
}
return utils.ConfigFilesInDir(convertCmdInputFolder)
}