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
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
3 changed files with 109 additions and 6 deletions

View File

@ -5,9 +5,10 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/prologic/pastebin)](https://goreportcard.com/report/github.com/prologic/pastebin) [![Go Report Card](https://goreportcard.com/badge/github.com/prologic/pastebin)](https://goreportcard.com/report/github.com/prologic/pastebin)
[![Coverage](https://coveralls.io/repos/prologic/pastebin/badge.svg)](https://coveralls.io/r/prologic/pastebin) [![Coverage](https://coveralls.io/repos/prologic/pastebin/badge.svg)](https://coveralls.io/r/prologic/pastebin)
pastebin is a web app that allows you to create smart bookmarks, commands and aliases by pointing your web browser's default search engine at a running instance. Similar to bunny1 or yubnub. pastebin is a self-hosted pastebin web app that lets you create and share
"ephemeral" data between devices and users. There is a configurable expiry
## Installation (TTL) afterwhich the paste expires and is purged. There is also a handy
CLI for interacting with the service in a easy way or you can also use curl!
### Source ### Source
@ -17,6 +18,8 @@ $ go install github.com/prologic/pastebin/...
### OS X Homebrew ### OS X Homebrew
**Coming**
There is a formula provided that you can tap and install from There is a formula provided that you can tap and install from
[prologic/homebrew-pastebin](https://github.com/prologic/homebrew-pastebin): [prologic/homebrew-pastebin](https://github.com/prologic/homebrew-pastebin):
@ -37,12 +40,24 @@ much appreciated and highly welcome!
Run pastebin: Run pastebin:
```#!bash ```#!bash
$ pastebin -bind 127.0.0.1:8000 $ pastebin
``` ```
Set your browser's default pastebin engine to http://localhost:8000/?q=%s Create a paste:
Then type `help` to view the main help page, `g foo bar` to perform a [Google](https://google.com) search for "foo bar" or `list` to list all available commands. ```#!bash
$ echo "Hello World" | pb
http://localhost:8000/92sHUeGPfoFctazBxdEhae
```
Or use the Web UI: http://localhost:8000/
Or curl:
```#bash
$ echo "hello World" | curl -q -L -d @- -o - http://localhost:8000/
...
```
## License ## License

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
}

40
cmd/pb/main.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"os"
"github.com/prologic/pastebin/client"
"github.com/mitchellh/go-homedir"
"github.com/namsral/flag"
)
const (
defaultConfig = "pastebin.conf"
defaultUserConfig = "~/.pastebin.conf"
defaultURL = "https://localhost:8000"
)
func getDefaultConfig() string {
path, err := homedir.Expand(defaultUserConfig)
if err != nil {
return defaultConfig
}
return path
}
func main() {
var (
config string
url string
insecure bool
)
flag.StringVar(&config, "config", getDefaultConfig(), "path to config")
flag.StringVar(&url, "url", defaultURL, "pastebin service url")
flag.BoolVar(&insecure, "insecure", false, "insecure (skip ssl verify)")
flag.Parse()
client.NewClient(url, insecure).Paste(os.Stdin)
}