Skip to content

Commit

Permalink
handle curl
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Chernikov authored and Alexander Chernikov committed Jul 17, 2020
1 parent 6dcf87a commit e375249
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -31,6 +30,13 @@ var (
)

func main() {
// remove curl from args
for index, arg := range os.Args{
if arg == "curl"{
os.Args = append(os.Args[:index], os.Args[index+1:]...)
}
}

httpMethod = getopt.StringLong("request", 'X', "GET", "HTTP method to use")
help = getopt.BoolLong("help", 'h', "This help text")
postBody = getopt.StringLong("data", 'd', "", "HTTP POST data")
Expand All @@ -45,8 +51,9 @@ func main() {
}

args := getopt.Args()

if len(args) != 1 {
flag.Usage()
fmt.Println("too many arguments")
os.Exit(0)
}

Expand Down Expand Up @@ -184,6 +191,7 @@ func readResponseBody(req *http.Request, resp *http.Response) (response []byte)
if err != nil {
log.Fatal(err)
}

if string(bodyBytes)[0] == '[' {
err = json.Unmarshal(bodyBytes, &jsonMaps)
if err != nil {
Expand All @@ -199,6 +207,7 @@ func readResponseBody(req *http.Request, resp *http.Response) (response []byte)
if err != nil {
log.Fatal(err)
}

response, err = jsonC.MarshalIndentWithFormatter(jsonMap, "", " ", f)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit e375249

Please sign in to comment.