-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (84 loc) · 2.03 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"time"
"golang.org/x/crypto/sha3"
"github.com/atotto/clipboard"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/ieee0824/getenv"
)
type config struct {
Bucket string `json:"bucket"`
Profile string `json:"profile"`
}
func loadConfig() (*config, error) {
var ret = new(config)
f, err := os.Open(filepath.Clean(fmt.Sprintf("%s/.chise/config.json", getenv.String("HOME"))))
if err != nil {
return nil, err
}
if err := json.NewDecoder(f).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
type uploader struct {
bucketName string
uploader *s3manager.Uploader
}
func newUploader(c *config) *uploader {
ret := new(uploader)
ret.bucketName = c.Bucket
cfg := new(aws.Config)
cfg.Credentials = credentials.NewSharedCredentials("", c.Profile)
ret.uploader = s3manager.NewUploader(session.New(cfg))
return ret
}
func (u *uploader) upload(f *os.File, key string) error {
_, err := u.uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(u.bucketName),
Key: aws.String(key),
Body: f,
ContentType: aws.String("image/png"),
})
return err
}
func main() {
cfg, err := loadConfig()
if err != nil {
log.Fatalln(err)
}
u := newUploader(cfg)
buf := []byte(fmt.Sprint(time.Now().UnixNano()))
h := make([]byte, 64)
sha3.ShakeSum256(h, buf)
key := fmt.Sprintf("%v.png", fmt.Sprintf("%X", h))
fileName := fmt.Sprintf("/tmp/%v", key)
cmd := exec.Command("screencapture", "-i", fileName)
if err := cmd.Run(); err != nil {
log.Fatalln(err)
}
defer func() {
os.Remove(fileName)
}()
f, err := os.Open(fileName)
if err != nil {
log.Fatalln(err)
}
if err := u.upload(f, "/"+key); err != nil {
log.Fatalln(err)
}
result := fmt.Sprintf("https://%s/%s", cfg.Bucket, key)
fmt.Println(result)
if err := clipboard.WriteAll(result); err != nil {
log.Fatalln(err)
}
}