Add a CLI tool for easily interacting with pastebin

This commit is contained in:
James Mills
2017-07-04 01:37:59 -07:00
parent 61b79025e8
commit 84159c33bf
3 changed files with 109 additions and 6 deletions

48
client/client.go Normal file
View File

@@ -0,0 +1,48 @@
package client
import (
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"net/http"
)
const (
contentType = "application/x-www-form-urlencoded"
)
// Client ...
type Client struct {
url string
insecure bool
}
// NewClient ...
func NewClient(url string, insecure bool) *Client {
return &Client{url: url, insecure: insecure}
}
// Paste ...
func (c *Client) Paste(body io.Reader) error {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !c.insecure},
}
client := &http.Client{Transport: tr}
res, err := client.Post(c.url, contentType, body)
if err != nil {
log.Printf("error pasting to %s: %s", c.url, err)
return err
}
if res.StatusCode != 200 {
log.Printf("unexpected response from %s: %d", c.url, res.StatusCode)
return errors.New("unexpected response")
}
fmt.Printf("%s", res.Request.URL.String())
return nil
}