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

Added Support for Bearer Token #1

Merged
merged 4 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 49 additions & 27 deletions cli.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package main

import (
"encoding/json"
"fmt"
"github.com/TylerBrock/colorjson"
mets "github.com/athul/pwcli/methods"
"github.com/urfave/cli"
"io/ioutil"
"log"
"net/http"
"os"
)

Expand All @@ -21,32 +17,58 @@ func main() {
cli.StringFlag{
Name: "url",
Value: "https://reqres.in/api/users",
Usage: "The URL/Endpoint you want to check",
Required: true,
},
cli.StringFlag{
Name: "token",
Value: "Bearer Token",
Usage: "Send the Request with Bearer Token",
},
}
app.Commands = []cli.Command{{
Name: "gt",
Usage: "Send a GET request",
Flags: myFlags,
Action: func(c *cli.Context) error {
resp, err := http.Get(c.String("url"))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
str := string(body)
var obj map[string]interface{}
json.Unmarshal([]byte(str), &obj)
f := colorjson.NewFormatter()
f.Indent = 6
s, _ := f.Marshal(obj)
//log.Println()
fmt.Print(resp)
fmt.Printf("\n\n %s", string(s))
return nil
bauthFlags := []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: "u",
Value: "Username",
Usage: "Add the Username",
Required: true,
},
cli.StringFlag{
Name: "p",
Value: "Password",
Usage: "Add the Password",
Required: true,
},
}
app.Commands = []cli.Command{
{
Name: "get",
Usage: "Send a GET request",
Flags: myFlags,
Action: func(c *cli.Context) error {
if c.String("token") != "" {
mets.Getwtoken(c)
} else {
mets.Getreq(c)
}
return nil
},
},
{
Name: "bauth",
Usage: "Send GET Request with Basic Auth(Don't Use get command with this)",
Flags: bauthFlags,
Action: func(c *cli.Context) error {
mets.Getbasic(c)
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ go 1.13
require (
github.com/TylerBrock/colorjson v0.0.0-20180527164720-95ec53f28296
github.com/fatih/color v1.9.0 // indirect
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e // indirect
github.com/urfave/cli v1.22.2
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM=
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
Expand All @@ -19,6 +21,7 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
27 changes: 27 additions & 0 deletions methods/fns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package methods

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/TylerBrock/colorjson"
)

// Formatresp formats the Response with Indents and Colors
func Formatresp(resp *http.Response) string {
body, err := ioutil.ReadAll(resp.Body)
str := string(body)
var obj map[string]interface{}
json.Unmarshal([]byte(str), &obj)
f := colorjson.NewFormatter()
f.Indent = 6
s, _ := f.Marshal(obj)
retbody := fmt.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n\n%s\n", resp.Status, resp.StatusCode, string(s))
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
return retbody
}
77 changes: 77 additions & 0 deletions methods/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package methods

import (
"encoding/base64"
"fmt"
"log"
"net/http"

"github.com/urfave/cli"
)

//Getreq sends a simple GET request to the url
func Getreq(c *cli.Context) error {
var url = c.String("url")

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
defer resp.Body.Close()
s := Formatresp(resp)
//log.Println()
fmt.Print(resp)
fmt.Printf("\n\n %s", s)
return nil
}

//Getwtoken send a get request with the Token for Authorization Header
func Getwtoken(c *cli.Context) error {
var url = c.String("url")
var bearer = "Bearer " + c.String("token")
req, err := http.NewRequest("GET", url, nil)

req.Header.Add("Authorization", bearer)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
defer resp.Body.Close()
s := Formatresp(resp)
fmt.Print(resp)
fmt.Printf("\n\n %s", s)

return nil
}

//Getbasic helps you send a request with Basic Auth as Authorization Method
func Getbasic(c *cli.Context) error {
un := c.String("u")
pw := c.String("p")
url := c.String("url")
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
s := Formatresp(resp)
if s != "" {
fmt.Printf("\n\n %s", s)
} else {
fmt.Print(resp)
}

return nil
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}