|
| 1 | +/* |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package commands |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "odfe-cli/client" |
| 21 | + ctrl "odfe-cli/controller/es" |
| 22 | + entity "odfe-cli/entity/es" |
| 23 | + gateway "odfe-cli/gateway/es" |
| 24 | + handler "odfe-cli/handler/es" |
| 25 | + |
| 26 | + "github.com/spf13/cobra" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + curlCommandName = "curl" |
| 31 | + curlPrettyFlagName = "pretty" |
| 32 | + curlPathFlagName = "path" |
| 33 | + curlQueryParamsFlagName = "query-params" |
| 34 | + curlDataFlagName = "data" |
| 35 | + curlHeadersFlagName = "headers" |
| 36 | + curlOutputFormatFlagName = "output-format" |
| 37 | + curlOutputFilterPathFlagName = "filter-path" |
| 38 | +) |
| 39 | + |
| 40 | +//curlCommand is base command for Elasticsearch REST APIs. |
| 41 | +var curlCommand = &cobra.Command{ |
| 42 | + Use: curlCommandName, |
| 43 | + Short: "Manage Elasticsearch core features", |
| 44 | + Long: "Use the curl command to execute any REST API calls against the cluster.", |
| 45 | +} |
| 46 | + |
| 47 | +func init() { |
| 48 | + curlCommand.Flags().BoolP("help", "h", false, "Help for curl command") |
| 49 | + curlCommand.PersistentFlags().Bool(curlPrettyFlagName, false, "Response will be formatted") |
| 50 | + curlCommand.PersistentFlags().StringP(curlOutputFormatFlagName, "o", "", |
| 51 | + "Output format if supported by cluster, else, default format by Elasticsearch. Example json, yaml") |
| 52 | + curlCommand.PersistentFlags().StringP(curlOutputFilterPathFlagName, "f", "", |
| 53 | + "Filter output fields returned by Elasticsearch. Use comma ',' to separate list of filters") |
| 54 | + GetRoot().AddCommand(curlCommand) |
| 55 | +} |
| 56 | + |
| 57 | +//GetCurlCommand returns Curl base command, since this will be needed for subcommands |
| 58 | +//to add as parent later |
| 59 | +func GetCurlCommand() *cobra.Command { |
| 60 | + return curlCommand |
| 61 | +} |
| 62 | + |
| 63 | +//getCurlHandler returns handler by wiring the dependency manually |
| 64 | +func getCurlHandler() (*handler.Handler, error) { |
| 65 | + c, err := client.New(nil) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + profile, err := GetProfile() |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + g := gateway.New(c, profile) |
| 74 | + facade := ctrl.New(g) |
| 75 | + return handler.New(facade), nil |
| 76 | +} |
| 77 | + |
| 78 | +//CurlActionExecute executes API based on user request |
| 79 | +func CurlActionExecute(input entity.CurlCommandRequest) error { |
| 80 | + |
| 81 | + commandHandler, err := getCurlHandler() |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + response, err := handler.Curl(commandHandler, input) |
| 86 | + if err == nil { |
| 87 | + fmt.Println(string(response)) |
| 88 | + return nil |
| 89 | + } |
| 90 | + if requestError, ok := err.(*entity.RequestError); ok { |
| 91 | + fmt.Println(requestError.GetResponse()) |
| 92 | + return nil |
| 93 | + } |
| 94 | + return err |
| 95 | +} |
| 96 | + |
| 97 | +func FormatOutput() bool { |
| 98 | + isPretty, _ := curlCommand.PersistentFlags().GetBool(curlPrettyFlagName) |
| 99 | + return isPretty |
| 100 | +} |
| 101 | + |
| 102 | +func GetUserInputAsStringForFlag(flagName string) string { |
| 103 | + format, _ := curlCommand.PersistentFlags().GetString(flagName) |
| 104 | + return format |
| 105 | +} |
| 106 | + |
| 107 | +func Run(cmd cobra.Command, cmdName string) { |
| 108 | + input := entity.CurlCommandRequest{ |
| 109 | + Action: cmdName, |
| 110 | + Pretty: FormatOutput(), |
| 111 | + OutputFormat: GetUserInputAsStringForFlag(curlOutputFormatFlagName), |
| 112 | + OutputFilterPath: GetUserInputAsStringForFlag(curlOutputFilterPathFlagName), |
| 113 | + } |
| 114 | + input.Path, _ = cmd.Flags().GetString(curlPathFlagName) |
| 115 | + input.QueryParams, _ = cmd.Flags().GetString(curlQueryParamsFlagName) |
| 116 | + input.Data, _ = cmd.Flags().GetString(curlDataFlagName) |
| 117 | + input.Headers, _ = cmd.Flags().GetString(curlHeadersFlagName) |
| 118 | + err := CurlActionExecute(input) |
| 119 | + DisplayError(err, cmdName) |
| 120 | +} |
0 commit comments