-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
256 lines (230 loc) · 5.15 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"github.com/dogbin/inu/dogbin"
"github.com/atotto/clipboard"
"github.com/urfave/cli"
)
const (
appAuthorName = "Till Kottmann"
appAuthorEmail = "[email protected]"
appCopyright = "(c) 2019 " + appAuthorName
appName = "inu"
appVersion = "v0.1.3"
)
var (
file string
server string
apiKey string
slug string
clipboardOutput bool
jsonOutput bool
)
func main() {
fileFlag := cli.StringFlag{
Name: "file, f",
Usage: "A file to upload to dogbin",
TakesFile: true,
Destination: &file,
}
serverFlag := cli.StringFlag{
Name: "server, r",
Usage: "The dogbin/hastebin server to use",
Value: "del.dog",
EnvVar: "DOGBIN_SERVER",
FilePath: "~/.inu/server",
Destination: &server,
}
slugFlag := cli.StringFlag{
Name: "slug, s",
Usage: "The slug to use instead of the server generated one [haste doesn't support this]",
Destination: &slug,
}
jsonFlag := cli.BoolFlag{
Name: "json, j",
Usage: "Outputs the result as JSON",
Destination: &jsonOutput,
}
clipboardFlag := cli.BoolFlag{
Name: "copy, c",
Usage: "Additionally puts the created URL in your clipboard",
Destination: &clipboardOutput,
}
apiKeyFlag := cli.StringFlag{
Name: "key, k",
Usage: "The dogbin api key to use",
Value: "",
EnvVar: "DOGBIN_KEY",
FilePath: "~/.inu/key",
Destination: &apiKey,
}
app := cli.NewApp()
app.Name = appName
app.Usage = "Use dogbin/hastebin right from your terminal"
app.Copyright = appCopyright
app.Authors = []cli.Author{
{
Name: appAuthorName,
Email: appAuthorEmail,
},
}
app.Version = appVersion
app.EnableBashCompletion = true
app.Action = put
app.Flags = []cli.Flag{
serverFlag,
slugFlag,
fileFlag,
jsonFlag,
clipboardFlag,
apiKeyFlag,
}
app.Commands = []cli.Command{
{
Name: "put",
Aliases: []string{"up", "p", "u", ""},
Usage: "Create a new paste",
Action: put,
Flags: []cli.Flag{
serverFlag,
slugFlag,
fileFlag,
jsonFlag,
clipboardFlag,
apiKeyFlag,
},
},
{
Name: "get",
Aliases: []string{"show", "s"},
Usage: "Obtains the contents of a paste",
Action: get,
Flags: []cli.Flag{
serverFlag,
cli.StringFlag{
Name: "slug, s",
Usage: "The slug of the paste to retrieve",
Destination: &slug,
},
jsonFlag,
cli.BoolFlag{
Name: "copy, c",
Usage: "Additionally puts the retrieved content in your clipboard",
Destination: &clipboardOutput,
},
},
},
}
err := app.Run(os.Args)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Error:", err)
}
}
func put(c *cli.Context) error {
info, err := os.Stdin.Stat()
if err != nil {
return fmt.Errorf("unable to stat os.Stdin: %w", err)
}
var content string
if info.Mode()&os.ModeNamedPipe != 0 {
content = readStdin()
if c.NArg() == 1 {
slug = c.Args()[0]
}
} else if file != "" {
buf, err := ioutil.ReadFile(file)
if err != nil {
return fmt.Errorf("unable to read the file '%s': %w", file, err)
}
content = string(buf)
if c.NArg() == 1 {
slug = c.Args()[0]
}
} else {
if c.NArg() == 1 {
content = c.Args()[0]
} else if c.NArg() == 2 {
slug = c.Args()[0]
content = c.Args()[1]
}
}
result, err := dogbin.NewServer(server, strings.TrimSpace(apiKey)).Put(slug, content)
if err != nil {
return cli.NewExitError(err, 1)
}
if clipboardOutput {
if err = clipboard.WriteAll(result.Url); err != nil {
return fmt.Errorf("unable to write the output into the clipboard: %w", err)
}
}
if jsonOutput {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(result)
}
fmt.Println(result.Url)
return nil
}
func get(c *cli.Context) error {
if c.NArg() == 1 {
slug = c.Args()[0]
}
if slug == "" {
return cli.ShowCommandHelp(c, "get")
}
pasteURL := slug
if strings.ContainsRune(pasteURL, '/') {
// convert slug to url to attempt to extract path + server from it
if !strings.HasPrefix(pasteURL, "http") && !strings.HasPrefix(pasteURL, "/") {
pasteURL = "https://" + pasteURL
}
u, err := url.Parse(pasteURL)
if err == nil {
if path := u.Path[1:]; path != "" {
pasteURL = path
}
u.Path = ""
u.RawQuery = ""
u.RawPath = ""
u.Fragment = ""
srv := u.String()
if srv != "" {
server = srv
}
}
}
if strings.ContainsRune(pasteURL, '.') {
pasteURL = strings.SplitN(pasteURL, ".", 2)[0]
}
doc, err := dogbin.NewServer(server, strings.TrimSpace(apiKey)).Get(pasteURL)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if clipboardOutput {
if err := clipboard.WriteAll(doc.Content); err != nil {
return err
}
}
if jsonOutput {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(doc)
}
fmt.Println(doc.Content)
return nil
}
func readStdin() string {
var input []byte
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Bytes()...)
input = append(input, '\n')
}
return string(input)
}