@@ -6,10 +6,13 @@ import (
66 "errors"
77 "fmt"
88 "io/ioutil"
9+ "log"
910 "net/http"
11+ "os"
1012 "strings"
1113
1214 "github.com/fatih/color"
15+ "github.com/olekukonko/tablewriter"
1316 "go.uber.org/multierr"
1417)
1518
@@ -34,57 +37,61 @@ func ReadCollection(filename string) ([]Collection, error) {
3437// ProcessCollection parses the Collection struct and execute the said requests
3538func ProcessCollection (jsonArr []Collection ) (string , error ) {
3639 var (
37- out string
38- errs error
40+ out string
41+ errs error
42+ tabData [][]string
3943 )
4044 for _ , jsondat := range jsonArr {
41- fmt .Printf ("\n ------------ \ n Name:\t %s\n " , color .HiMagentaString (jsondat .Name ))
45+ fmt .Printf ("\n Name:\t %s\n " , color .HiMagentaString (jsondat .Name ))
4246 if len (jsondat .Folders ) > 0 {
4347 if err := jsondat .getDatafromFolders (); err != nil {
4448 return "" , err
4549 }
4650 } else {
4751 request := jsondat .Requests
4852 for _ , req := range request {
49- err := jsondat .request (req )
53+
54+ tabdata , err := jsondat .request (req )
55+ tabData = append (tabData , tabdata )
5056 if err != nil {
5157 errs = multierr .Append (errs , err )
5258 }
5359 }
60+ genTables (tabData )
5461 }
5562 }
5663 return out , errs
5764}
5865
5966// request process the Requests type and executes the Requests synchronously
60- func (c * Collection ) request (req Requests ) error {
67+ func (c * Collection ) request (req Requests ) ([] string , error ) {
6168 var (
62- colored = color .New (color .FgHiRed , color .Bold ) // Red string color
63- fURL = req .URL + req .Path // Full URL
64- err error
65- out , paramURL string
69+ colored = color .New (color .FgHiRed , color .Bold ) // Red string color
70+ fURL = req .URL + req .Path // Full URL
71+ err error
72+ paramURL , status , code string
73+ tabData []string
6674 )
6775
6876 if req .Method == "GET" {
69- out , paramURL , err = c .sendGET (req )
77+ status , code , paramURL , err = c .sendGET (req )
7078 } else {
71- out , err = c .sendPOST (req , req .Method )
79+ status , code , err = c .sendPOST (req , req .Method )
7280 }
7381 if err != nil {
74- return err
82+ return nil , err
7583 }
7684 if paramURL != "" {
77- fmt . Printf ( "%s | \t %s | %s | \t %s \n " , color .HiGreenString (req .Name ), colored .Sprintf (paramURL ), color .HiYellowString (req .Method ), out )
85+ tabData = [] string { color .HiGreenString (req .Name ), colored .Sprintf (paramURL ), color .HiYellowString (req .Method ), status , code }
7886 } else {
79- fmt . Printf ( "%s | \t %s | %s | \t %s \n " , color .HiGreenString (req .Name ), colored .Sprintf (fURL ), color .HiYellowString (req .Method ), out )
87+ tabData = [] string { color .HiGreenString (req .Name ), colored .Sprintf (fURL ), color .HiYellowString (req .Method ), status , code }
8088 }
81-
82- return nil
89+ return tabData , nil
8390}
8491
8592// sendGet sends a GET request to the said URL and returns a Response string and if it contains a
8693// params in the URL
87- func (c * Collection ) sendGET (req Requests ) (string , string , error ) {
94+ func (c * Collection ) sendGET (req Requests ) (string , string , string , error ) {
8895 var (
8996 url = req .URL + req .Path
9097 bearer string
@@ -103,8 +110,13 @@ func (c *Collection) sendGET(req Requests) (string, string, error) {
103110 url += paramstr
104111 }
105112 reqHTTP , err := http .NewRequest ("GET" , url , nil )
113+ if len (req .Headers ) > 0 {
114+ for _ , head := range req .Headers {
115+ reqHTTP .Header .Set (head .Key , head .Value )
116+ }
117+ }
106118 if err != nil {
107- return "" , "" , fmt .Errorf ("Error creating request: %s" , err .Error ())
119+ return "" , "" , "" , fmt .Errorf ("Error creating request: %s" , err .Error ())
108120 }
109121
110122 if req .Token != "" {
@@ -121,24 +133,25 @@ func (c *Collection) sendGET(req Requests) (string, string, error) {
121133 client := getHTTPClient ()
122134 resp , err := client .Do (reqHTTP )
123135 if err != nil {
124- return "" , "" , fmt .Errorf ("Error sending request: %s" , err .Error ())
136+ return "" , "" , "" , fmt .Errorf ("Error sending request: %s" , err .Error ())
125137 }
126138 defer resp .Body .Close ()
127139 // paramstr is suffixed with a `?` to append to the URL
128140 if paramstr != "?" {
129- s := fmt .Sprintf ("Status: %s\t StatusCode:\t %s \n " , color .HiBlueString (resp .Status ), color .New (color .FgHiBlue ).Sprintln (resp .StatusCode ))
130- return s , url , nil
141+ status := fmt .Sprintf ("%s" , color .HiBlueString (resp .Status ))
142+ code := fmt .Sprintf ("%s" , color .New (color .FgHiBlue ).Sprintln (resp .StatusCode ))
143+ return status , code , url , nil
131144 }
132- s := fmt .Sprintf ("Status: %s\t StatusCode:\t %s\n " , color .HiBlueString (resp .Status ), color .New (color .FgHiBlue ).Sprintln (resp .StatusCode ))
133- return s , "" , nil
145+ status := fmt .Sprintf ("%s" , color .HiBlueString (resp .Status ))
146+ code := fmt .Sprintf ("%s" , color .New (color .FgHiBlue ).Sprintln (resp .StatusCode ))
147+ return status , code , "" , nil
134148}
135149
136150// sendPOST sends all request other than GET requests since
137151// it's the only one which has BodyParams and RawParams
138- func (c * Collection ) sendPOST (req Requests , method string ) (string , error ) {
152+ func (c * Collection ) sendPOST (req Requests , method string ) (string , string , error ) {
139153
140154 var (
141- // colorcy = color.New(color.FgCyan, color.Bold)
142155 jsonStr []byte
143156 url = req .URL + req .Path
144157 reqData = req
@@ -164,11 +177,16 @@ func (c *Collection) sendPOST(req Requests, method string) (string, error) {
164177
165178 finalBytes , err := json .RawMessage (jsonStr ).MarshalJSON () // Marshal to JSON from strings
166179 if err != nil {
167- return "" , fmt .Errorf ("Error Marhsaling JSON: %s" , err .Error ())
180+ return "" , "" , fmt .Errorf ("Error Marhsaling JSON: %s" , err .Error ())
168181 }
169182 reqHTTP , err := http .NewRequest (method , url , bytes .NewBuffer (finalBytes ))
183+ if len (req .Headers ) > 0 {
184+ for _ , head := range req .Headers {
185+ reqHTTP .Header .Set (head .Key , head .Value )
186+ }
187+ }
170188 if err != nil {
171- return "" , fmt .Errorf ("Error creating request: %s" , err .Error ())
189+ return "" , "" , fmt .Errorf ("Error creating request: %s" , err .Error ())
172190 }
173191
174192 reqHTTP .Header .Set ("Content-Type" , req .Ctype ) // Set Content type to said Ctype in Collection
@@ -179,50 +197,58 @@ func (c *Collection) sendPOST(req Requests, method string) (string, error) {
179197 }
180198 if req .User != "" && req .Pass != "" {
181199 // Basic Auth
182- un := req .User
183- pw := req .Pass
184- reqHTTP .Header .Add ("Authorization" , "Basic " + basicAuth (un , pw ))
200+ reqHTTP .Header .Add ("Authorization" , "Basic " + basicAuth (req .User , req .Pass ))
185201 }
186202
187203 client := getHTTPClient ()
188204 resp , err := client .Do (reqHTTP )
189205 if err != nil {
190- return "" , fmt .Errorf ("Error sending request: %s" , err .Error ())
206+ return "" , "" , fmt .Errorf ("Error sending request: %s" , err .Error ())
191207 }
192208
193209 defer resp .Body .Close ()
194210
195- s := fmt .Sprintf ("Status: %s\t StatusCode:\t %s\n " , color .HiBlueString (resp .Status ), color .New (color .FgHiBlue ).Sprintln (resp .StatusCode ))
196- return s , nil
211+ status := fmt .Sprintf ("%s" , color .HiBlueString (resp .Status ))
212+ code := fmt .Sprintf ("%s" , color .New (color .FgHiBlue ).Sprintln (resp .StatusCode ))
213+ return status , code , nil
197214}
198215
199216// getDatafromFolders handle edge cases when requests are saved inside folders
200217// from hoppscotch itself
201218// This adds another loop to check for folders and requests
202219func (c * Collection ) getDatafromFolders () error {
203220 var (
204- err error
205- out , paramURL string
221+ err error
222+ paramURL , status , code string
223+ tabData [][]string
206224 )
207225 for _ , Folder := range c .Folders {
208- for j := range Folder .Requests {
209- fmt .Printf ("Folder Name:\t %s\n " , color .HiMagentaString (Folder .Name ))
210- fURL := fmt .Sprintf (Folder .Requests [j ].URL + Folder .Requests [j ].Path )
211- method := Folder .Requests [j ].Method
226+ for _ , fRequest := range Folder .Requests {
227+ fURL := fmt .Sprintf (fRequest .URL + fRequest .Path )
228+ method := fRequest .Method
212229 if method == "GET" {
213- out , paramURL , err = c .sendGET (Folder . Requests [ j ] )
230+ status , code , paramURL , err = c .sendGET (fRequest )
214231 } else {
215- out , err = c .sendPOST (Folder . Requests [ j ] , method )
232+ status , code , err = c .sendPOST (fRequest , method )
216233 }
217234 if err != nil {
218- return err
235+ log . Println ( err )
219236 }
220237 if paramURL != "" {
221- fmt . Printf ( "%s | \t %s | %s | \t %s \n " , color .HiGreenString (Folder . Requests [ j ]. Name ), color .HiRedString (paramURL ), color .HiYellowString (method ), out )
238+ tabData = append ( tabData , [] string { color .HiGreenString (fRequest . Name ), color .HiRedString (paramURL ), color .HiYellowString (method ), status , code } )
222239 } else {
223- fmt . Printf ( "%s | \t %s | %s | \t %s \n " , color .HiGreenString (Folder . Requests [ j ]. Name ), color .HiRedString (fURL ), color .HiYellowString (method ), out )
240+ tabData = append ( tabData , [] string { color .HiGreenString (fRequest . Name ), color .HiRedString (fURL ), color .HiYellowString (method ), status , code } )
224241 }
225242 }
226243 }
244+ genTables (tabData )
227245 return nil
228246}
247+
248+ //genTables generate the output in Tabular Form
249+ func genTables (data [][]string ) {
250+ table := tablewriter .NewWriter (os .Stdout )
251+ table .SetHeader ([]string {"Name" , "URL" , "Method" , "Status" , "Code" })
252+ table .AppendBulk (data )
253+ table .Render ()
254+ }
0 commit comments