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

Commit

Permalink
Added Support for Bearer Token
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Jan 19, 2020
1 parent 68a2452 commit 270289c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
30 changes: 10 additions & 20 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 @@ -23,27 +19,21 @@ func main() {
Value: "https://reqres.in/api/users",
Required: true,
},
cli.StringFlag{
Name: "token",
Value: "Bearer Token",
},
}
app.Commands = []cli.Command{{
Name: "gt",
Name: "get",
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)
if c.String("token") != "" {
mets.Getwtoken(c)
} else {
mets.Getreq(c)
}
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
},
},
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=
63 changes: 63 additions & 0 deletions methods/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package methods

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

"github.com/TylerBrock/colorjson"
"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)
}
body, err := ioutil.ReadAll(resp.Body)
s := formatresp(body)
//log.Println()
fmt.Print(resp)
fmt.Printf("\n\n %s", s)
return nil
}

//Getwtoken send a get request with the Token for Auth
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)
body, err := ioutil.ReadAll(resp.Body)
s := formatresp(body)
//log.Println()
fmt.Print(resp)
fmt.Printf("\n\n %s", s)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
return nil
}
func formatresp(body []byte) string {
str := string(body)
var obj map[string]interface{}
json.Unmarshal([]byte(str), &obj)
f := colorjson.NewFormatter()
f.Indent = 6
s, _ := f.Marshal(obj)
return string(s)
}

0 comments on commit 270289c

Please sign in to comment.