-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (62 loc) · 1.58 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/atotto/clipboard"
)
var retrieveRaw bool
func main() {
// Parse flags
flag.StringVar(&serverURL, "server", "https://hastebin.com", "Hastebin server URL")
flag.BoolVar(&retrieveRaw, "raw", false, "Retrieve raw URL")
flag.Parse()
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
var content []byte
// Determine if input comes from stdin
// Or if input should be an argument file
if info.Mode()&os.ModeCharDevice != 0 || info.Size() <= 0 {
// Retrieve file to read from command line arguments
if len(flag.Args()) <= 0 {
fmt.Println("Specify a file to read or pipe data to this application")
return
}
fileName := flag.Args()[0]
content, err = ioutil.ReadFile(fileName)
} else {
// Read everything from stdin
content, err = ioutil.ReadAll(os.Stdin)
}
if err != nil {
panic(err)
}
resp, err := Upload(content)
if err != nil {
fmt.Println("Unable to send data to " + serverURL)
fmt.Println(err.Error())
return
}
// Check for errors
if resp.Message != "" {
fmt.Println("Unable to send data to " + serverURL)
fmt.Println(err.Error())
return
}
fmt.Println("Document uploaded to " + serverURL)
finalURL := serverURL + "/" + resp.Key
if retrieveRaw {
finalURL = serverURL + "/raw/" + resp.Key
}
// Try to write result to clipboard if possible
if err := clipboard.WriteAll(finalURL); err != nil {
fmt.Println("- " + finalURL)
fmt.Println("Unable to copy link to clipboard")
fmt.Println(err.Error())
} else {
fmt.Println("- " + finalURL + " (copied to clipboard)")
}
}