Skip to content

Instantly share code, notes, and snippets.

@callerobertsson
Last active August 29, 2015 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callerobertsson/12494fbfbabe6bfb36c5 to your computer and use it in GitHub Desktop.
Save callerobertsson/12494fbfbabe6bfb36c5 to your computer and use it in GitHub Desktop.
Golang: Command line wrapper for posting to pastebin.com
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
)
const apiUrl = "http://pastebin.com/api/api_post.php"
// Flags
var (
k string // user key
o string // option
p int // private
n string // paste name
x string // expire
f string // format
)
func init() {
flag.StringVar(&k, "k", "", "the api key")
flag.StringVar(&o, "o", "paste", "api option (paste)")
flag.IntVar(&p, "p", 1, "0 = public, 1 = unlisted, 2 = private")
flag.StringVar(&n, "n", "", "paste name")
flag.StringVar(&x, "x", "10M", "expiration")
flag.StringVar(&f, "f", "text", "paste format")
flag.Parse()
}
func main() {
// Check for API key
if k == "" {
fmt.Fprintf(os.Stderr, "No API key! Please get one from pastebin.com and set the -k option\n")
return
}
// Read code from stdin
code, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not read from STDIN: %v\n", err.Error())
return
}
// Call PasteBin
resp, err := http.PostForm(apiUrl, url.Values{
"api_dev_key": {k},
"api_paste_private": {strconv.Itoa(p)},
"api_option": {o},
"api_paste_name": {n},
"api_paste_expire_date": {x},
"api_paste_format": {f},
"api_paste_code": {string(code)},
})
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
return
}
// Print response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading response: %v\n", err.Error())
return
}
fmt.Println(string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment