-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
160 lines (125 loc) · 3.47 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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func keyExists(session *session.Session, bucket, key string) (bool, error) {
svc := s3.New(session)
results, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucket), Prefix: aws.String(key)})
if err != nil {
return false, err
}
return len(results.Contents) > 0, nil
}
func downloadS3(session *session.Session, bucket, key string) error {
downloader := s3manager.NewDownloader(session)
filename := filepath.Base(key)
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("cannot create file %s: %v", filename, err)
}
defer file.Close()
_, err = downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return fmt.Errorf("cannot download key %s from s3: %v", key, err)
}
return nil
}
func getKey(session *session.Session, bucket, key string) error {
if err := downloadS3(session, bucket, key); err != nil {
return fmt.Errorf("cannot download from s3: %v", err)
}
if err := extractZipFile(filepath.Base(key), "."); err != nil {
return fmt.Errorf("cannot extract zip file: %v", err)
}
return nil
}
func uploadS3(session *session.Session, bucket, key, filename string) error {
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("cannot open file for saving: %v", err)
}
defer file.Close()
uploader := s3manager.NewUploader(session)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: file,
})
if err != nil {
return fmt.Errorf("cannot upload file to s3: %v", err)
}
return nil
}
func saveKey(session *session.Session, bucket, key string, files []string) error {
zipfilename := filepath.Base(key)
if err := createZipFile(zipfilename, files); err != nil {
return fmt.Errorf("cannot create zip file: %v", err)
}
if err := uploadS3(session, bucket, key, zipfilename); err != nil {
return fmt.Errorf("cannot upload to s3: %v", err)
}
return nil
}
var CACHR_BUCKET = os.Getenv("CACHR_BUCKET")
var AWS_REGION = os.Getenv("AWS_REGION")
func main() {
if CACHR_BUCKET == "" {
log.Fatalf("missing required env: CACHR_BUCKET")
}
if AWS_REGION == "" {
log.Fatalf("missing required env: AWS_REGION")
}
if len(os.Args) < 2 {
log.Fatalf("Usage: cachr <commandname> <args>")
}
command := os.Args[1]
args := os.Args[2:]
session, err := session.NewSession(&aws.Config{
Region: aws.String(AWS_REGION)},
)
if err != nil {
log.Fatalf("cannot create new AWS session: %v", err)
}
if command == "exists" {
if len(args) < 1 {
log.Fatalf("Usage: cachr exists <keyname>")
}
exists, err := keyExists(session, CACHR_BUCKET, args[0])
if err != nil {
log.Fatalf("failed to check if key exists: %v", err)
}
if exists {
os.Exit(0)
}
os.Exit(1)
} else if command == "get" {
if len(args) < 1 {
log.Fatalf("Usage: cachr get <keyname>")
}
err = getKey(session, CACHR_BUCKET, args[0])
if err != nil {
log.Fatalf("failed to get cache: %v", err)
}
} else if command == "save" {
if len(args) < 2 {
log.Fatalf("Usage: cachr save <keyname> <files>...")
}
err = saveKey(session, CACHR_BUCKET, args[0], args[1:])
if err != nil {
log.Fatalf("failed to save cache: %v", err)
}
} else {
log.Fatalf("invalid command: %s", command)
}
}