Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
🛠Removed the URL flags and updated error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Feb 5, 2020
1 parent 1e702f7 commit f2f276a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 28 deletions.
20 changes: 4 additions & 16 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ func main() {
app.Description = color.HiBlueString("Made with <3 by Postwoman Team")

getFlags := []cli.Flag{
cli.StringFlag{
Name: "url",
Value: "https://reqres.in/api/users",
Usage: "The URL/Endpoint you want to check",
Required: true,
},
cli.StringFlag{
Name: "token, t",
Usage: "Send the Request with Bearer Token",
Expand All @@ -38,12 +32,6 @@ func main() {
},
}
postFlags := []cli.Flag{
cli.StringFlag{
Name: "url",
Value: "https://reqres.in/api/users",
Usage: "The URL/Endpoint you want to check",
Required: true,
},
cli.StringFlag{
Name: "token, t",
Usage: "Send the Request with Bearer Token",
Expand All @@ -66,13 +54,13 @@ func main() {
Usage: "Body of the Post Request",
},
}
sendFlag := []cli.Flag{
/* sendFlag := []cli.Flag{
cli.StringFlag{
Name: "pt",
Usage: "The Path of Postwoman Collection.json",
Required: true,
},
}
} */
app.Commands = []cli.Command{
{
Name: "get",
Expand Down Expand Up @@ -122,7 +110,7 @@ func main() {
{
Name: "send",
Usage: "Test all the Endpoints in the Postwoman Collection.json",
Flags: sendFlag,
//Flags: sendFlag,
Action: func(c *cli.Context) error {
mets.ReadCollection(c)
return nil
Expand All @@ -133,7 +121,7 @@ func main() {
WE REALLY NEED YOUR FEEDBACK,
CREATE A NEW ISSUE FOR BUGS AND FEATURE REQUESTS : <http://github.com/athul/pwcli>
CREATE A NEW ISSUE FOR BUGS AND FEATURE REQUESTS : < http://github.com/athul/pwcli >
`, cli.AppHelpTemplate)

err := app.Run(os.Args)
Expand Down
9 changes: 7 additions & 2 deletions methods/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import (
)

//Deletebasic sends a basic DELETE request
func Deletebasic(c *cli.Context) {
url := c.String("url")
func Deletebasic(c *cli.Context) error {
url := c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
return nil
}
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("DELETE", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
Expand All @@ -32,4 +36,5 @@ func Deletebasic(c *cli.Context) {
//defer resp.Body.Close()
s := formatresp(resp)
fmt.Println(s)
return nil
}
17 changes: 12 additions & 5 deletions methods/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import (
"fmt"
"log"
"net/http"
"os"

"github.com/fatih/color"
"github.com/urfave/cli"
)

//Getbasic sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth
func Getbasic(c *cli.Context) error {
var url = c.String("url")
func Getbasic(c *cli.Context) {
var url = c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
os.Exit(0)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
log.Println("Error on Request.\nDid you give a correct URL? Try giving that")
os.Exit(0)
}
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
Expand All @@ -27,10 +34,10 @@ func Getbasic(c *cli.Context) error {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
log.Println(color.RedString("Error on response.\nDid you give a correct URL? Try giving that"))
}

s := formatresp(resp)
fmt.Println(s)
return nil

}
8 changes: 7 additions & 1 deletion methods/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
"bytes"
"fmt"
"net/http"
"os"

"github.com/urfave/cli"
)

//Patchbasic sends a basic PATCH request
func Patchbasic(c *cli.Context) {
url := c.String("url")
url := c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
os.Exit(0)
}
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
Expand All @@ -32,4 +37,5 @@ func Patchbasic(c *cli.Context) {
//defer resp.Body.Close()
s := formatresp(resp)
fmt.Println(s)

}
7 changes: 6 additions & 1 deletion methods/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
"bytes"
"fmt"
"net/http"
"os"

"github.com/urfave/cli"
)

//Postbasic sends a basic POST request
func Postbasic(c *cli.Context) {
url := c.String("url")
url := c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
os.Exit(0)
}
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
Expand Down
9 changes: 7 additions & 2 deletions methods/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import (
)

//Putbasic sends a basic PUT request
func Putbasic(c *cli.Context) {
url := c.String("url")
func Putbasic(c *cli.Context) error {
url := c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
return nil
}
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
Expand All @@ -32,4 +36,5 @@ func Putbasic(c *cli.Context) {
//defer resp.Body.Close()
s := formatresp(resp)
fmt.Println(s)
return nil
}
7 changes: 6 additions & 1 deletion methods/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"net/http"
"os"

"github.com/fatih/color"
"github.com/urfave/cli"
Expand Down Expand Up @@ -43,7 +44,11 @@ type Bpardata struct {

//ReadCollection reads the PostWoman Collection Json File and does the Magic Stuff
func ReadCollection(c *cli.Context) {
data, err := ioutil.ReadFile(c.String("pt"))
data, err := ioutil.ReadFile(c.Args().Get(0))
if string(data) == "" {
fmt.Print("PATH is needed")
os.Exit(0)
}
if err != nil {
fmt.Print(err)
}
Expand Down

0 comments on commit f2f276a

Please sign in to comment.