/
main.go
57 lines (46 loc) · 1.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// © 2012 Steve McCoy. Available under the MIT License.
/*
topad pastes its standard input to itsapad.appspot.com and prints the URL
on standard output.
For example:
echo Hello | topad
will paste "Hello" to itsapad.appspot.com and print out the URL of the paste.
*/
package main
import (
"errors"
"flag"
"io/ioutil"
"net/http"
"net/url"
"os"
)
var serviceURL = flag.String("url", "https://pad.mccoy.space", "URL to pad service")
func main() {
flag.Parse()
text, err := ioutil.ReadAll(os.Stdin)
if err != nil {
os.Stderr.WriteString("Failed to read anything: " + err.Error() + "\n")
os.Exit(1)
}
vals := make(url.Values)
vals.Set("body", string(text))
skipRedirect := errors.New("I want the Location")
c := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return skipRedirect
},
}
resp, err := c.PostForm(*serviceURL, vals)
if err != nil && err.(*url.Error).Err != skipRedirect {
os.Stderr.WriteString("Request failed: " + err.Error() + "\n")
os.Exit(1)
}
defer resp.Body.Close()
loc, err := resp.Location()
if err != nil {
os.Stderr.WriteString("Response failed: " + err.Error() + "\n")
os.Exit(1)
}
os.Stderr.WriteString(loc.String() + "\n")
}