|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log/slog" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/aep-dev/aep-lib-go/pkg/api" |
| 9 | + "github.com/aep-dev/aep-lib-go/pkg/openapi" |
| 10 | + "github.com/aep-dev/aepcli/internal/config" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func handleCoreCommand(additionalArgs []string, configFile string) error { |
| 15 | + coreCmd := &cobra.Command{ |
| 16 | + Use: "core", |
| 17 | + Args: cobra.MinimumNArgs(1), |
| 18 | + Short: "Core API management commands", |
| 19 | + } |
| 20 | + |
| 21 | + coreCmd.AddCommand(openAPICommand()) |
| 22 | + coreCmd.AddCommand(configCmd(configFile)) |
| 23 | + |
| 24 | + coreCmd.SetArgs(additionalArgs) |
| 25 | + if err := coreCmd.Execute(); err != nil { |
| 26 | + return fmt.Errorf("error executing core command: %v", err) |
| 27 | + } |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +func openAPICommand() *cobra.Command { |
| 32 | + c := &cobra.Command{ |
| 33 | + Use: "openapi", |
| 34 | + Short: "OpenAPI commands", |
| 35 | + } |
| 36 | + var inputPath string |
| 37 | + var outputPath string |
| 38 | + var pathPrefix string |
| 39 | + |
| 40 | + convertCmd := &cobra.Command{ |
| 41 | + Use: "convert", |
| 42 | + Short: "Best effort conversion of OpenAPI specification to an AEP API", |
| 43 | + Run: func(cmd *cobra.Command, args []string) { |
| 44 | + if inputPath == "" { |
| 45 | + fmt.Println("Input path is required") |
| 46 | + os.Exit(1) |
| 47 | + } |
| 48 | + slog.Debug("Converting OpenAPI spec", "inputPath", inputPath, "outputPath", outputPath, "pathPrefix", pathPrefix) |
| 49 | + |
| 50 | + originalOAS, err := openapi.FetchOpenAPI(inputPath) |
| 51 | + if err != nil { |
| 52 | + fmt.Printf("Error fetching OpenAPI spec: %v\n", err) |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | + |
| 56 | + api, err := api.GetAPI(originalOAS, "", pathPrefix) |
| 57 | + if err != nil { |
| 58 | + fmt.Printf("Error converting to AEP API: %v\n", err) |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | + |
| 62 | + finalOAS, err := api.ConvertToOpenAPIBytes() |
| 63 | + if err != nil { |
| 64 | + fmt.Printf("Error converting to OpenAPI: %v\n", err) |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | + |
| 68 | + if outputPath == "" { |
| 69 | + fmt.Println(string(finalOAS)) |
| 70 | + } else { |
| 71 | + err = os.WriteFile(outputPath, []byte(finalOAS), 0644) |
| 72 | + if err != nil { |
| 73 | + fmt.Printf("Error writing output file: %v\n", err) |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + convertCmd.Flags().StringVarP(&inputPath, "input", "i", "", "Input OpenAPI specification file path") |
| 82 | + convertCmd.Flags().StringVarP(&outputPath, "output", "o", "", "Output file path. If unset, print to stdout") |
| 83 | + convertCmd.Flags().StringVar(&pathPrefix, "path-prefix", "", "Path prefix to strip from paths when evaluating resource hierarchy") |
| 84 | + convertCmd.MarkFlagRequired("input") |
| 85 | + |
| 86 | + c.AddCommand(convertCmd) |
| 87 | + return c |
| 88 | +} |
| 89 | + |
| 90 | +func configCmd(configFile string) *cobra.Command { |
| 91 | + var openAPIPath string |
| 92 | + var overwrite bool |
| 93 | + var api config.API |
| 94 | + var serverURL string |
| 95 | + var headers []string |
| 96 | + var pathPrefix string |
| 97 | + |
| 98 | + configCmd := &cobra.Command{ |
| 99 | + Use: "config", |
| 100 | + Short: "Manage core API configurations", |
| 101 | + } |
| 102 | + |
| 103 | + addCmd := &cobra.Command{ |
| 104 | + Use: "add [name]", |
| 105 | + Short: "Add a new core API configuration", |
| 106 | + Args: cobra.ExactArgs(1), |
| 107 | + Run: func(cmd *cobra.Command, args []string) { |
| 108 | + api = config.API{ |
| 109 | + Name: args[0], |
| 110 | + OpenAPIPath: openAPIPath, |
| 111 | + ServerURL: serverURL, |
| 112 | + Headers: headers, |
| 113 | + PathPrefix: pathPrefix, |
| 114 | + } |
| 115 | + if err := config.WriteAPIWithName(configFile, api, overwrite); err != nil { |
| 116 | + fmt.Printf("Error writing API config: %v\n", err) |
| 117 | + os.Exit(1) |
| 118 | + } |
| 119 | + fmt.Printf("Core API configuration '%s' added successfully\n", args[0]) |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + addCmd.Flags().StringVar(&openAPIPath, "openapi-path", "", "Path to OpenAPI specification file") |
| 124 | + addCmd.Flags().StringArrayVar(&headers, "header", []string{}, "Headers in format key=value") |
| 125 | + addCmd.Flags().StringVar(&serverURL, "server-url", "", "Server URL") |
| 126 | + addCmd.Flags().StringVar(&pathPrefix, "path-prefix", "", "Path prefix") |
| 127 | + addCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite existing configuration") |
| 128 | + |
| 129 | + readCmd := &cobra.Command{ |
| 130 | + Use: "get [name]", |
| 131 | + Short: "Get an API configuration", |
| 132 | + Args: cobra.ExactArgs(1), |
| 133 | + Run: func(cmd *cobra.Command, args []string) { |
| 134 | + cfg, err := config.ReadConfigFromFile(configFile) |
| 135 | + if err != nil { |
| 136 | + fmt.Printf("Error reading config file: %v\n", err) |
| 137 | + os.Exit(1) |
| 138 | + } |
| 139 | + |
| 140 | + api, exists := cfg.APIs[args[0]] |
| 141 | + if !exists { |
| 142 | + fmt.Printf("No API configuration found with name '%s'\n", args[0]) |
| 143 | + os.Exit(1) |
| 144 | + } |
| 145 | + |
| 146 | + fmt.Printf("Name: %s\n", api.Name) |
| 147 | + fmt.Printf("OpenAPI Path: %s\n", api.OpenAPIPath) |
| 148 | + fmt.Printf("Server URL: %s\n", api.ServerURL) |
| 149 | + fmt.Printf("Headers: %v\n", api.Headers) |
| 150 | + fmt.Printf("Path Prefix: %s\n", api.PathPrefix) |
| 151 | + }, |
| 152 | + } |
| 153 | + |
| 154 | + listCmd := &cobra.Command{ |
| 155 | + Use: "list", |
| 156 | + Short: "List all API configurations", |
| 157 | + Args: cobra.NoArgs, |
| 158 | + Run: func(cmd *cobra.Command, args []string) { |
| 159 | + apis, err := config.ListAPIs(configFile) |
| 160 | + if err != nil { |
| 161 | + fmt.Printf("Error listing APIs: %v\n", err) |
| 162 | + os.Exit(1) |
| 163 | + } |
| 164 | + |
| 165 | + if len(apis) == 0 { |
| 166 | + fmt.Println("No API configurations found") |
| 167 | + return |
| 168 | + } |
| 169 | + |
| 170 | + for _, api := range apis { |
| 171 | + fmt.Printf("Name: %s\n", api.Name) |
| 172 | + fmt.Printf("OpenAPI Path: %s\n", api.OpenAPIPath) |
| 173 | + fmt.Printf("Server URL: %s\n", api.ServerURL) |
| 174 | + fmt.Printf("Headers: %v\n", api.Headers) |
| 175 | + fmt.Printf("Path Prefix: %s\n", api.PathPrefix) |
| 176 | + fmt.Println() |
| 177 | + } |
| 178 | + }, |
| 179 | + } |
| 180 | + |
| 181 | + configCmd.AddCommand(addCmd) |
| 182 | + configCmd.AddCommand(readCmd) |
| 183 | + configCmd.AddCommand(listCmd) |
| 184 | + |
| 185 | + return configCmd |
| 186 | +} |
0 commit comments