From cee3b4dd0a3ab95a05b54a436d049f729c1ff4be Mon Sep 17 00:00:00 2001 From: athul Date: Tue, 11 Feb 2020 22:59:39 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Support=20for=20XML=20and=20H?= =?UTF-8?q?TML=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- go.mod | 2 ++ go.sum | 7 +++++++ methods/fns.go | 32 +++++++++++++++++++++++--------- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8606f37..fd75691 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# PostWoman CLI [![Build Status](https://travis-ci.com/athul/pwcli.svg?token=udLtq6DyJs4Gxpze9nqX&branch=master)](https://travis-ci.com/athul/pwcli)[![Postwoman](https://img.shields.io/badge/Made_for-Postwoman-hex_color_code?logo=Postwoman)](https://postwoman.io) +# PostWoman CLI [![Build Status](https://travis-ci.com/athul/pwcli.svg?token=udLtq6DyJs4Gxpze9nqX&branch=master)](https://travis-ci.com/athul/pwcli)[![Postwoman](https://img.shields.io/badge/Made_for-Postwoman-hex_color_code?logo=Postwoman)](https://postwoman.io) [![Go Report Card](https://goreportcard.com/badge/github.com/athul/pwcli)](https://goreportcard.com/report/github.com/athul/pwcli) Use Postwoman's CLI direct from your terminal. # Installation diff --git a/go.mod b/go.mod index 6798ca7..555bbbe 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,6 @@ require ( github.com/fatih/color v1.9.0 github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e // indirect github.com/urfave/cli v1.22.2 + github.com/yosssi/gohtml v0.0.0-20190915184251-7ff6f235ecaf + golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect ) diff --git a/go.sum b/go.sum index dadb30b..e2b18d7 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,15 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/yosssi/gohtml v0.0.0-20190915184251-7ff6f235ecaf h1:VA200mPTYh9FWY8zKX5ctXCtNk78HUez8ecTdsQGhoo= +github.com/yosssi/gohtml v0.0.0-20190915184251-7ff6f235ecaf/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 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= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 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= diff --git a/methods/fns.go b/methods/fns.go index bcaf27f..082b80d 100644 --- a/methods/fns.go +++ b/methods/fns.go @@ -7,28 +7,42 @@ import ( "io/ioutil" "log" "net/http" + "strings" "github.com/TylerBrock/colorjson" "github.com/fatih/color" + "github.com/yosssi/gohtml" ) // Formatresp formats the Response with Indents and Colors func formatresp(resp *http.Response) string { + var retbody string + heads := fmt.Sprint(resp.Header) c := color.New(color.FgCyan, color.Bold) magenta := color.New(color.FgHiMagenta) yellow := color.New(color.FgHiYellow) 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) - for key, value := range resp.Header { - c.Print(key, " : ") - magenta.Print(value, "\n") + if strings.Contains(heads, "json") { + var obj map[string]interface{} + json.Unmarshal([]byte(str), &obj) + f := colorjson.NewFormatter() + f.Indent = 6 + s, _ := f.Marshal(obj) + for key, value := range resp.Header { + c.Print(key, " : ") + magenta.Print(value, "\n") + } + retbody = yellow.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", string(s)) + } else if strings.Contains(heads, "xml") || strings.Contains(heads, "html") { + for key, value := range resp.Header { + c.Print(key, " : ") + magenta.Print(value, "\n") + } + s := c.Sprint(gohtml.Format(str)) + retbody = yellow.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", s) + } - retbody := yellow.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", string(s)) if err != nil { log.Println("Error on response.\n[ERRO] -", err) }