-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
181 lines (156 loc) · 4.55 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
package main
import (
"bufio"
"crypto/md5"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
)
var (
flags = flag.NewFlagSet("s3up", flag.ExitOnError)
versionFlag = flags.Bool("version", false, "print the version and exit")
configFlag = flags.String("config", "", "config file")
accessKeyFlag = flags.String("access-key", "", "s3 access key")
secretKeyFlag = flags.String("secret-key", "", "s3 secret key")
regionFlag = flags.String("region", "", "s3 region")
bucketFlag = flags.String("bucket", "", "s3 bucket")
prefixFlag = flags.String("prefix", "", "s3 path prefix")
sourcePathFlag = flags.String("source", "", "local source path to upload")
dryrunFlag = flags.Bool("dryrun", false, "Dryrun, dont upload anything, just list files to upload")
confirmFlag = flags.Bool("confirm", false, "Confirm final settings with user before triggering upload")
parallelFlag = flags.Int("parallel", 10, "Number of parallel uploads (default=10)")
hashPrefixFlag = flags.Bool("auto-content-hash-prefix", false, "Compute the md5 hash of a file's contents and use it as the path prefix")
hashPrefixBytesFlag = flags.Uint("content-hash-bytes", 6, "Number of bytes of md5 hash to use in the path prefix. Max 16. 6 and 12 best (base64 encoding)")
syncFlag = flags.Bool("sync", false, "Only upload files that are new or have changed")
listFlag = flags.Bool("list", false, "List files data to be processed (outputs in JSON format)")
manifestFlag = flags.String("manifest", "", "path to write a manifest file")
cacheTTLFlag = flags.Int("cache-ttl", 0, "TTL for cache control headers (in seconds)")
md5sum = flags.String("md5sum", "", "Convenience flag to provide md5 sum of filepath")
)
func main() {
flags.Parse(os.Args[1:])
if len(os.Args) == 1 {
flags.Usage()
os.Exit(0)
}
if *versionFlag {
fmt.Println(VERSION)
os.Exit(0)
}
if *md5sum != "" {
sum, err := getFileMD5(*md5sum)
if err != nil {
log.Fatal(err)
}
fmt.Println(sum)
os.Exit(0)
}
cfg, err := newConfig(*configFlag)
if err != nil {
log.Fatal(err)
}
// override from file configs if specific flags provided
if *accessKeyFlag != "" {
cfg.S3.AccessKey = *accessKeyFlag
}
if *secretKeyFlag != "" {
cfg.S3.SecretKey = *secretKeyFlag
}
if *regionFlag != "" {
cfg.S3.Region = *regionFlag
}
if *bucketFlag != "" {
cfg.S3.Bucket = *bucketFlag
}
if *prefixFlag != "" {
cfg.S3.Prefix = *prefixFlag
}
if *sourcePathFlag != "" {
cfg.S3.Source = *sourcePathFlag
}
if *cacheTTLFlag > 0 {
cfg.S3.ExpiresAfterSeconds = *cacheTTLFlag
cfg.S3.CacheControl = fmt.Sprintf("public, max-age=%d", *cacheTTLFlag)
}
if cfg.S3.Source == "" {
log.Fatal("invalid source path")
}
s3up, err := NewS3Upload(cfg)
if err != nil {
log.Fatal(err)
}
// just list files, it is like dry-run but with output as JSON array
if *listFlag {
fileDataList, err := s3up.sourceFiles()
if err != nil {
log.Fatal(err)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(fileDataList); err != nil {
log.Fatal(err)
}
os.Exit(0)
}
if err := s3up.Connect(); err != nil {
log.Fatal(err)
}
if *confirmFlag && !confirm(cfg) {
os.Exit(0)
}
// run actual upload
n, err := s3up.Upload(*parallelFlag, *dryrunFlag)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\nDone! uploaded %d files.\n", n)
}
func confirm(cfg *Config) bool {
fmt.Println("s3up config:")
fmt.Println("------------")
fmt.Println("BUCKET : ", cfg.S3.Bucket)
fmt.Println("PREFIX : ", cfg.S3.Prefix)
fmt.Println("SOURCE : ", cfg.S3.Source)
fmt.Println("ACL : ", cfg.S3.ACL)
fmt.Println("IGNORE : ", cfg.S3.Ignore)
fmt.Println("HASH PREFIX : ", *hashPrefixFlag)
fmt.Println("SYNC : ", *syncFlag)
fmt.Println("")
fmt.Printf("upload? (y/n): ")
r := bufio.NewReader(os.Stdin)
char, _, err := r.ReadRune()
if err != nil {
log.Fatal(err)
}
switch char {
case 'Y':
return true
case 'y':
return true
}
return false
}
func getFileMD5(path string) (string, error) {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return "", fmt.Errorf("%s file does not exist", path)
}
if info.IsDir() {
return "", fmt.Errorf("md5sum cannot run on a directory")
}
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
// calculate md5-hash of file
h := md5.New()
if _, err := io.Copy(h, file); err != nil {
return "", err
}
md5Hash := h.Sum(nil)
return fmt.Sprintf("%x", md5.Sum(md5Hash)), nil
}