This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3shot.go
203 lines (174 loc) · 3.77 KB
/
s3shot.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
package main
import (
"gopkg.in/urfave/cli.v1"
"log"
"os"
"os/exec"
)
var region string
var bucket string
var uri string
var compression bool
var notify bool
var open bool
func main() {
app := cli.NewApp()
app.Name = "s3shot"
app.Usage = "Take a screenshot and upload it to an S3 bucket"
app.Version = "1.0.0"
app.Authors = []cli.Author{
{
Name: "Maarten Zuidhoorn",
Email: "maarten@zuidhoorn.com",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "region,r",
Usage: "use the region `REGION`",
Destination: ®ion,
},
cli.StringFlag{
Name: "bucket,b",
Usage: "upload the files to `BUCKET`",
Destination: &bucket,
},
cli.StringFlag{
Name: "url",
Value: "",
Usage: "the url to prepend to the filename",
Destination: &uri,
},
cli.BoolFlag{
Name: "compress,c",
Usage: "compress the image before uploading",
Destination: &compression,
},
cli.BoolFlag{
Name: "notify,n",
Usage: "get a notification when uploading is finished",
Destination: ¬ify,
},
cli.BoolFlag{
Name: "open,o",
Usage: "open the browser when uploading is finished",
Destination: &open,
},
}
app.Commands = []cli.Command{
{
Name: "all",
Aliases: []string{"a"},
Usage: "Capture the whole screen",
Action: func(context *cli.Context) error {
image, err := runCommand("maim", "-u")
// Ignore errors, since it likely means the user cancelled manually
if err == nil {
return handleUpload(image)
}
return nil
},
},
{
Name: "window",
Aliases: []string{"w"},
Usage: "Capture the current active window",
Action: func(context *cli.Context) error {
window, err := runCommand("xdotool", "getactivewindow")
if err != nil {
return err
}
image, err := runCommand("maim", "-ui", string(window))
// Ignore errors, since it likely means the user cancelled manually
if err == nil {
return handleUpload(image)
}
return nil
},
},
{
Name: "selection",
Aliases: []string{"s"},
Usage: "Capture a selection",
Action: func(context *cli.Context) error {
image, err := runCommand("maim", "-su")
// Ignore errors, since it likely means the user cancelled manually
if err == nil {
return handleUpload(image)
}
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func runCommand(command string, args ...string) ([]byte, error) {
output, err := exec.Command(command, args...).Output()
if err != nil {
return nil, err
}
return output, nil
}
func copyToClipboard(text string) error {
command := exec.Command("xclip", "-in", "-selection", "clipboard")
stdin, err := command.StdinPipe()
if err != nil {
return err
}
err = command.Start()
if err != nil {
return err
}
_, err = stdin.Write([]byte(text))
if err != nil {
return err
}
err = stdin.Close()
if err != nil {
return err
}
return command.Wait()
}
func sendNotification(location string) error {
_, err := runCommand("notify-send", "Screenshot uploaded", location)
return err
}
func handleUpload(contents []byte) error {
var image = contents
if compression {
compressed, err := compress(image)
if err != nil {
return err
}
image = compressed
}
filename := hashFile(image) + ".png"
output, err := uploadFile(region, bucket, filename, image)
if err != nil {
return err
}
var location = output.Location
if uri != "" {
location = uri + filename
}
err = copyToClipboard(location)
if err != nil {
return err
}
if notify {
err := sendNotification(location)
if err != nil {
return err
}
}
if open {
err := exec.Command("xdg-open", location).Run()
if err != nil {
return err
}
}
return nil
}