From 84159c33bfeb7132b6f65924a758c2442d3a2f66 Mon Sep 17 00:00:00 2001 From: James Mills Date: Tue, 4 Jul 2017 01:37:59 -0700 Subject: [PATCH] Add a CLI tool for easily interacting with pastebin --- README.md | 27 +++++++++++++++++++++------ client/client.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/pb/main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 client/client.go create mode 100644 cmd/pb/main.go diff --git a/README.md b/README.md index 9efc116..dda19e3 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,10 @@ [![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) -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. - -## Installation +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 +(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 @@ -17,6 +18,8 @@ $ go install github.com/prologic/pastebin/... ### OS X Homebrew +**Coming** + There is a formula provided that you can tap and install from [prologic/homebrew-pastebin](https://github.com/prologic/homebrew-pastebin): @@ -37,12 +40,24 @@ much appreciated and highly welcome! Run pastebin: ```#!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 diff --git a/client/client.go b/client/client.go new file mode 100644 index 0000000..2ccaeae --- /dev/null +++ b/client/client.go @@ -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 +} diff --git a/cmd/pb/main.go b/cmd/pb/main.go new file mode 100644 index 0000000..21a05f8 --- /dev/null +++ b/cmd/pb/main.go @@ -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) +}