diff --git a/cli.go b/cli.go index 4883bf8..a56fe0e 100644 --- a/cli.go +++ b/cli.go @@ -1,10 +1,11 @@ package main import ( - mets "github.com/athul/pwcli/methods" - "github.com/urfave/cli" "log" "os" + + mets "github.com/athul/pwcli/methods" + "github.com/urfave/cli" ) func main() { @@ -13,7 +14,7 @@ func main() { app.Usage = "Test API endpoints without the hassle" app.Description = "Made with <3 by Postwoman Team" - myFlags := []cli.Flag{ + getFlags := []cli.Flag{ cli.StringFlag{ Name: "url", Value: "https://reqres.in/api/users", @@ -22,39 +23,72 @@ func main() { }, cli.StringFlag{ Name: "token", - Value: "Bearer Token", Usage: "Send the Request with Bearer Token", }, cli.StringFlag{ - Name: "u", - Value: "Username", - Usage: "Add the Username", - Required: true, + Name: "u", + Usage: "Add the Username", + }, + cli.StringFlag{ + Name: "p", + Usage: "Add the Password", }, + } + postFlags := []cli.Flag{ cli.StringFlag{ - Name: "p", - Value: "Password", - Usage: "Add the Password", + Name: "url", + Value: "https://reqres.in/api/users", + Usage: "The URL/Endpoint you want to check", Required: true, }, + cli.StringFlag{ + Name: "token", + Usage: "Send the Request with Bearer Token", + }, + cli.StringFlag{ + Name: "u", + Usage: "Add the Username", + }, + cli.StringFlag{ + Name: "p", + Usage: "Add the Password", + }, + cli.StringFlag{ + Name: "ctype", + Usage: "Change the Content Type", + }, + cli.StringFlag{ + Name: "body", + Usage: "Body of the Post Request", + }, } app.Commands = []cli.Command{ { Name: "get", Usage: "Send a GET request", - Flags: myFlags, + Flags: getFlags, Action: func(c *cli.Context) error { - if c.String("u") != "" && c.String("p") != "" { - mets.Getbasic(c) - } else if c.String("token") != "" { - mets.Getwtoken(c) - } else { - mets.Getreq(c) + tokavail := c.String("token") + uvail := c.String("u") + switch { + case tokavail != "": + mets.Authwtoken(c, "GET") + break + case uvail != "": + mets.Authbasic(c, "GET") + default: + mets.Basicreq(c) } + return nil }, }, + { + Name: "post", + Usage: "Send a POST Request", + + } } err := app.Run(os.Args) if err != nil { diff --git a/methods/get.go b/methods/get.go index 175cb89..b96e153 100644 --- a/methods/get.go +++ b/methods/get.go @@ -9,8 +9,8 @@ import ( "github.com/urfave/cli" ) -//Getreq sends a simple GET request to the url -func Getreq(c *cli.Context) error { +//Basicreq sends a simple GET request to the url +func Basicreq(c *cli.Context) error { var url = c.String("url") req, err := http.NewRequest("GET", url, nil) if err != nil { @@ -29,12 +29,12 @@ func Getreq(c *cli.Context) error { return nil } -//Getwtoken send a get request with the Token for Authorization Header -func Getwtoken(c *cli.Context) error { +//Authwtoken send a get request with the Token for Authorization Header +func Authwtoken(c *cli.Context, method string) error { var url = c.String("url") var bearer = "Bearer " + c.String("token") - req, err := http.NewRequest("GET", url, nil) - + req, err := http.NewRequest(method, url, nil) + req.Header.Add("Content-Type", "application/json") req.Header.Add("Authorization", bearer) client := &http.Client{} resp, err := client.Do(req) @@ -51,12 +51,12 @@ func Getwtoken(c *cli.Context) error { return nil } -//Getbasic helps you send a request with Basic Auth as Authorization Method -func Getbasic(c *cli.Context) error { +//Authbasic helps you send a request with Basic Auth as Authorization Method +func Authbasic(c *cli.Context, method string) error { un := c.String("u") pw := c.String("p") url := c.String("url") - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(method, url, nil) req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) client := &http.Client{} resp, err := client.Do(req)