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

Commit

Permalink
bubbling errors up to main() instead of panicking
Browse files Browse the repository at this point in the history
  • Loading branch information
gbmor committed Feb 13, 2020
1 parent 6f797bf commit e7296b0
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 74 deletions.
22 changes: 8 additions & 14 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"log"
"os"

mets "github.com/athul/pwcli/methods"
Expand Down Expand Up @@ -67,53 +66,47 @@ func main() {
Usage: "Send a GET request",
Flags: getFlags,
Action: func(c *cli.Context) error {
mets.Getbasic(c)
return nil
return mets.Getbasic(c)
},
},
{
Name: "post",
Usage: "Send a POST Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
mets.Postbasic(c)
return nil
return mets.Postbasic(c)
},
},
{
Name: "put",
Usage: "Send a PUT Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
mets.Putbasic(c)
return nil
return mets.Putbasic(c)
},
},
{
Name: "patch",
Usage: "Send a PATCH Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
mets.Patchbasic(c)
return nil
return mets.Patchbasic(c)
},
},
{
Name: "delete",
Usage: "Send a DELETE Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
mets.Deletebasic(c)
return nil
return mets.Deletebasic(c)
},
},
{
Name: "send",
Usage: "Test all the Endpoints in the Postwoman Collection.json",
//Flags: sendFlag,
Action: func(c *cli.Context) error {
mets.ReadCollection(c)
return nil
return mets.ReadCollection(c)
},
},
}
Expand All @@ -126,6 +119,7 @@ func main() {

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
c := color.New(color.FgRed).Add(color.Bold)
c.Println(fmt.Sprintf("\n%s", err.Error()))
}
}
11 changes: 7 additions & 4 deletions methods/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
func Deletebasic(c *cli.Context) error {
url, err := checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
return nil
return fmt.Errorf("URL validation error: %s", err.Error())
}
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("DELETE", url, bytes.NewBuffer(jsonStr))
Expand All @@ -31,10 +30,14 @@ func Deletebasic(c *cli.Context) error {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
return fmt.Errorf("Request error: %s", err.Error())
}
//defer resp.Body.Close()
s := formatresp(resp)
s, err := formatresp(resp)
if err != nil {
return err
}

fmt.Println(s)
return nil
}
24 changes: 12 additions & 12 deletions methods/fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"

Expand All @@ -15,13 +14,18 @@ import (
)

// Formatresp formats the Response with Indents and Colors
func formatresp(resp *http.Response) string {
func formatresp(resp *http.Response) (string, error) {
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)
if err != nil {
return "", fmt.Errorf("Error reading response body: %s", err.Error())
}

str := string(body)
if strings.Contains(heads, "json") {
var obj map[string]interface{}
Expand All @@ -39,19 +43,15 @@ func formatresp(resp *http.Response) string {
c.Print(key, " : ")
magenta.Print(value, "\n")
}
var s string
if strings.Contains(heads, "plain") {
s = str
} else {
s = c.Sprint(gohtml.Format(str))
var s string
if strings.Contains(heads, "plain") {
s = str
} else {
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)

}
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
return retbody
return retbody, nil
}

func basicAuth(username, password string) string {
Expand Down
21 changes: 10 additions & 11 deletions methods/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@ package methods

import (
"fmt"
"log"
"net/http"
"os"

"github.com/fatih/color"
"github.com/urfave/cli"
)

//Getbasic sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth
func Getbasic(c *cli.Context) {
func Getbasic(c *cli.Context) error {
var url, err = checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(0)
return err
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("Error on Request.\nDid you give a correct URL? Try giving that")
os.Exit(0)
return fmt.Errorf("Error creating request: %s", err.Error())
}
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
Expand All @@ -34,10 +29,14 @@ func Getbasic(c *cli.Context) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(color.RedString("Error on response.\nDid you give a correct URL? Try giving that"))
return fmt.Errorf("Error sending request: %s", err.Error())
}

s := formatresp(resp)
fmt.Println(s)
s, err := formatresp(resp)
if err != nil {
return err
}

fmt.Println(s)
return nil
}
16 changes: 9 additions & 7 deletions methods/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import (
"bytes"
"fmt"
"net/http"
"os"

"github.com/urfave/cli"
)

//Patchbasic sends a basic PATCH request
func Patchbasic(c *cli.Context) {
func Patchbasic(c *cli.Context) error {
url, err := checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(0)
return err
}
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(jsonStr))
Expand All @@ -32,10 +30,14 @@ func Patchbasic(c *cli.Context) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
return fmt.Errorf("Error sending request: %s", err.Error())
}
//defer resp.Body.Close()
s := formatresp(resp)
fmt.Println(s)
s, err := formatresp(resp)
if err != nil {
return err
}

fmt.Println(s)
return nil
}
14 changes: 8 additions & 6 deletions methods/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import (
"bytes"
"fmt"
"net/http"
"os"

"github.com/urfave/cli"
)

//Postbasic sends a basic POST request
func Postbasic(c *cli.Context) {
func Postbasic(c *cli.Context) error {
url, err := checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(0)
return err
}
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
Expand All @@ -34,9 +32,13 @@ func Postbasic(c *cli.Context) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
return fmt.Errorf("Error sending request: %s", err.Error())
}
//defer resp.Body.Close()
s := formatresp(resp)
s, err := formatresp(resp)
if err != nil {
return err
}
fmt.Println(s)
return nil
}
11 changes: 7 additions & 4 deletions methods/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
func Putbasic(c *cli.Context) error {
url, err := checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
return nil
return err
}
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
Expand All @@ -31,10 +30,14 @@ func Putbasic(c *cli.Context) error {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
return fmt.Errorf("Error sending request: %s", err.Error())
}
//defer resp.Body.Close()
s := formatresp(resp)
s, err := formatresp(resp)
if err != nil {
return err
}

fmt.Println(s)
return nil
}
Loading

0 comments on commit e7296b0

Please sign in to comment.