/
kehlua.go
114 lines (88 loc) · 2.19 KB
/
kehlua.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
package main
import (
"flag"
"fmt"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
"io/ioutil"
"os"
"path/filepath"
"regexp"
s "strings"
)
type file struct {
path string
data []byte
contentType string
}
const permissions = s3.BucketOwnerFull
var versionRegex = regexp.MustCompile(`\d+-\d+-\d+`)
func prepareFilesInDir(localDir, s3Dir string, files *[]file) {
dir, err := ioutil.ReadDir(localDir)
if err != nil {
panic(err)
}
for _, _file := range dir {
name := _file.Name()
if s.HasPrefix(name, ".") {
continue
}
localPath := fmt.Sprintf("%s/%s", localDir, name)
s3Path := fmt.Sprintf("%s/%s", s3Dir, name)
if _file.IsDir() {
prepareFilesInDir(localPath, s3Path, files)
} else {
data, fileErr := ioutil.ReadFile(localPath)
if fileErr != nil {
panic(fileErr)
}
contentType := CONTENT_TYPE_LOOKUP[filepath.Ext(localPath)]
s3Path = s.Replace(s3Path, "./", "", 1)
item := file{s3Path, data, contentType}
*files = append(*files, item)
}
}
}
func upload(_file file, uploads chan<- bool, client *s3.S3, bucket *s3.Bucket) {
err := bucket.Put(_file.path, _file.data, _file.contentType, permissions)
if err != nil {
fmt.Printf("UPLOAD ERROR: %+v\n", err)
panic(err)
}
uploads <- true
fmt.Printf("Uploaded %s!\n", _file.path)
}
func main() {
dirnamePtr := flag.String("folder", "", "the older where the assets reside")
versionPtr := flag.String("version", "", "the version of the styleguide")
flag.Parse()
dirname := *dirnamePtr
version := *versionPtr
if dirname == "" {
fmt.Println("`folder` must be specified")
return
}
if !versionRegex.MatchString(version) {
fmt.Println("`version` must be specified in the format major-minor-patch")
return
}
var files []file
prepareFilesInDir(dirname, version, &files)
requestNumber := len(files)
uploads := make(chan bool, requestNumber)
auth, err := aws.EnvAuth()
if err != nil {
panic(err)
os.Exit(1)
}
client := s3.New(auth, aws.USEast)
bucket := client.Bucket("testington")
for _, _file := range files {
go upload(_file, uploads, client, bucket)
}
for u := 1; u <= requestNumber; u++ {
<-uploads
}
close(uploads)
fmt.Println("DONE")
}