-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (59 loc) · 1.27 KB
/
main.go
File metadata and controls
70 lines (59 loc) · 1.27 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
)
var (
appName = filepath.Base(os.Args[0])
in = flag.String("in", "", "input from file instead of STDIN")
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s\n", appName)
fmt.Fprintf(os.Stderr, "Upload document to hastebin.com.")
fmt.Fprintf(os.Stderr, "options:\n")
flag.PrintDefaults()
os.Exit(2)
}
func _main() error {
flag.Usage = usage
flag.Parse()
reader := os.Stdin
if *in != "" {
f, err := os.Open(*in)
if err != nil {
return err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return fmt.Errorf("error during stat: %v", err)
}
if stat.Size() > 512*1024 {
return fmt.Errorf("file is larger than 512KiB")
}
reader = f
}
url := "https://hastebin.com/documents"
resp, err := http.Post(url, "text/plain", reader)
if err != nil {
return fmt.Errorf("unable to post document: %v", err)
}
if resp.StatusCode != 200 {
fmt.Fprintf(os.Stderr, "Server responded with error %s", resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unable to read reply contents: %v", err)
}
fmt.Println(string(data))
return nil
}
func main() {
if err := _main(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
}
}