-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
86 lines (66 loc) Β· 1.45 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type Response[T any] struct {
Success bool `json:"success"`
Data T `json:"data"`
}
type PostPasteData struct {
Id string `json:"id"`
Message string `json:"message"`
Content string `json:"content"`
}
func main() {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeNamedPipe) == 0 {
log.Println("Input must be through a pipe")
return
}
instance := flag.String("instance", "https://zer0b.in", "The instance to use")
markdown := flag.Bool("md", false, "Markdown mode")
flag.Parse()
buf, _ := ioutil.ReadAll(os.Stdin)
content := string(buf)
if *markdown {
content = "md " + content
}
resp, err := Post(*instance, content)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var response Response[PostPasteData]
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !response.Success {
fmt.Println(response.Data.Message)
os.Exit(1)
}
url := fmt.Sprintf("%s/%s", *instance, response.Data.Id)
fmt.Println(url)
}
func Post(instance string, content string) (*http.Response, error) {
values := map[string]string{"content": content}
payload, err := json.Marshal(values)
if err != nil {
return nil, err
}
return http.Post(instance + "/api/p/n", "application/json",
bytes.NewBuffer(payload))
}