Skip to content

Commit

Permalink
docs: openapi2apisix messages and usage (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
pottekkat committed Sep 18, 2023
1 parent f724da4 commit 33737b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ adc diff

Shows the differences in configuration between the connected APISIX instance and the local configuration file.

### adc openapi2apisix

```shell
adc openapi2apisix -o config.yaml -f openAPI.yaml
```

Converts the configuration in OpenAPI format (`openAPI.yaml`) to APISIX configuration (`config.yaml`).

### adc version

```shell
Expand Down
18 changes: 9 additions & 9 deletions cmd/openapi2apisix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
func newOpenAPI2APISIXCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "openapi2apisix",
Short: "Convert OpenAPI file to ADC configuration file",
Long: `The openapi2apisix command can be used to convert OpenAPI file to ADC configuration file.`,
Short: "Convert OpenAPI configuration to ADC configuration",
Long: `Converts the configuration in OpenAPI format to the ADC configuration format.`,
RunE: func(cmd *cobra.Command, args []string) error {

err := openAPI2APISIX(cmd)
Expand All @@ -38,17 +38,17 @@ func newOpenAPI2APISIXCmd() *cobra.Command {
func openAPI2APISIX(cmd *cobra.Command) error {
output, err := cmd.Flags().GetString("output")
if err != nil {
color.Red("Get file path failed: %v", err)
color.Red("Failed to get output file path: %v", err)
return err
}
if output == "" {
color.Red("Output path is empty.")
color.Red("Output file path is empty. Please specify a file path: adc openapi2apisix -o config.yaml")
return nil
}

filename, err := cmd.Flags().GetString("file")
if err != nil {
color.Red("Get file path failed: %v", err)
color.Red("Failed to get OpenAPI file path: %v", err)
return err
}
if filename == "" {
Expand All @@ -58,28 +58,28 @@ func openAPI2APISIX(cmd *cobra.Command) error {

f, err := os.Open(filename)
if err != nil {
color.Red("Open file %s failed: %s", filename, err)
color.Red("Failed to open %s: %s", filename, err)
return err
}
defer f.Close()

reader := bufio.NewReader(f)
fileContent, err := io.ReadAll(reader)
if err != nil {
color.Red("Read file %s failed: %s", filename, err)
color.Red("Failed to read file %s: %s", filename, err)
return err
}

conf, err := openapi2apisix.Convert(context.Background(), fileContent)
if err != nil {
color.Red("Convert OpenAPI file %s failed: %s", filename, err)
color.Red("Failed to convert OpenAPI file %s: %s", filename, err)
return err
}

err = common.SaveAPISIXConfiguration(output, conf)
if err != nil {
return err
}
color.Green("Successfully convert OpenAPI fileto " + output)
color.Green("Converted OpenAPI file to %s successfully ", output)
return nil
}

0 comments on commit 33737b0

Please sign in to comment.