|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/smf8/gurl/internal/app/gurl/request" |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "io" |
| 8 | + "math" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + methodHelp = `Specify HTTP method, either POST, GET (default), PATCH, DELETE, PUT` |
| 14 | + headersHelp = `HTTP headers in key:value format. use comma (,) as separator. Default Content-Type header is set to "application/x-www-form-urlencoded"` |
| 15 | + bodyHelp = `HTTP request body, can't use with --json or --file` |
| 16 | + fileHelp = `used to send a file as request body. This option also sets "Content-Type" header to "application/octet-stream". You can override this by setting Content-Type header manually with -H option` |
| 17 | + jsonHelp = `used to specify json Body. This option also sets "Content-Type" header to "application/json". You can override this by setting Content-Type header manually with -H option` |
| 18 | + timeoutHelp = `Set client request Timeout. this timeout is only considered in client side.` |
| 19 | +) |
| 20 | + |
| 21 | +func main(gurl *request.GURL) error { |
| 22 | + httpRequest, err := gurl.ToHTTPRequest() |
| 23 | + if err != nil { |
| 24 | + return fmt.Errorf("failed to launch gurl command: %w", err) |
| 25 | + } |
| 26 | + |
| 27 | + fmt.Println(httpRequest) |
| 28 | + fmt.Println("method:", httpRequest.Method) |
| 29 | + |
| 30 | + body, err := io.ReadAll(httpRequest.Body) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + fmt.Printf("Data: %s", string(body)) |
| 35 | + |
| 36 | + fmt.Println("headers:", httpRequest.Header) |
| 37 | + fmt.Println("URL:", httpRequest.URL.String()) |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +// NewCommand creates an instance of gURL cli cobra request |
| 42 | +func NewCommand() *cobra.Command { |
| 43 | + |
| 44 | + gurl := new(request.GURL) |
| 45 | + gurl.Headers = make(map[string]string) |
| 46 | + gurl.QueryParams = make(map[string][]string) |
| 47 | + |
| 48 | + var headers *[]string |
| 49 | + |
| 50 | + gurlCommand := &cobra.Command{ |
| 51 | + Use: "gurl URL [-M Method] [-H Headers {key1:value1,...}] [-D Data | --json JsonData | --file FilePath] [--timeout timeout]", |
| 52 | + Short: "gurl is a simple curl rip off", |
| 53 | + Args: cobra.ExactArgs(1), |
| 54 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 55 | + gurl.URL = args[0] |
| 56 | + |
| 57 | + for _, h := range *headers { |
| 58 | + record := strings.ToLower(h) |
| 59 | + header := strings.Split(record, ":") |
| 60 | + |
| 61 | + if len(header) != 2 { |
| 62 | + return fmt.Errorf("invalid header format. see -h for correct format") |
| 63 | + } |
| 64 | + |
| 65 | + gurl.Headers[header[0]] = header[1] |
| 66 | + } |
| 67 | + |
| 68 | + if err := main(gurl); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + return nil |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + gurlCommand.Flags().StringVarP(&gurl.Method, "method", "M", "GET", methodHelp) |
| 76 | + headers = gurlCommand.Flags().StringSliceP("headers", "H", nil, headersHelp) |
| 77 | + gurlCommand.Flags().StringVarP(&gurl.Data, "data", "D", "", bodyHelp) |
| 78 | + gurlCommand.Flags().StringVar(&gurl.FilePath, "file", "", fileHelp) |
| 79 | + gurlCommand.MarkPersistentFlagDirname("file") |
| 80 | + gurlCommand.Flags().StringVar(&gurl.JSONMessage, "json", "", jsonHelp) |
| 81 | + gurlCommand.Flags().IntVar(&gurl.ClientTimeout, "timeout", math.MaxInt32, timeoutHelp) |
| 82 | + |
| 83 | + return gurlCommand |
| 84 | +} |
0 commit comments