-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.go
215 lines (180 loc) · 4.89 KB
/
cli.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
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"compress/gzip"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"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/sirupsen/logrus"
validator "gopkg.in/go-playground/validator.v8"
)
// Exit codes are int values that represent an exit code for a particular error.
const (
ExitCodeOK int = 0
ExitCodeError int = 1 + iota
)
// CLI is the command line object
type CLI struct {
// outStream and errStream are the stdout and stderr
// to write message from the CLI.
outStream, errStream io.Writer
}
// Run invokes the CLI with the given arguments.
func (cli *CLI) Run(args []string) int {
var (
awsKey string
awsID string
region string
bucket string
path string
concurrency int
version bool
)
// Define option flag parse
flags := flag.NewFlagSet(Name, flag.ContinueOnError)
flags.SetOutput(cli.errStream)
flags.StringVar(&awsKey, "secret-access-key", "", "Please specify secret access key of aws")
flags.StringVar(&awsID, "access-key-id", "", "Please specify access key id of aws")
flags.StringVar(®ion, "region", "ap-northeast-1", "Please specify region of aws")
flags.StringVar(&bucket, "bucket", "", "Please specify bucket of aws s3")
flags.StringVar(&path, "path", "/", "Please specify path of aws s3")
flags.IntVar(&concurrency, "concurrency", 5, "Upload concurrency")
flags.BoolVar(&version, "version", false, "Print version information and quit.")
// Parse commandline flag
if err := flags.Parse(args[1:]); err != nil {
return ExitCodeError
}
// Show version
if version {
fmt.Fprintf(cli.errStream, "%s version %s\n", Name, Version)
return ExitCodeOK
}
s, err := newSthree(awsID, awsKey, region, bucket, path, concurrency)
if err != nil {
logrus.Fatal(err)
}
for _, n := range flags.Args() {
if err := s.Put(n); err != nil {
logrus.Error(err)
}
}
return ExitCodeOK
}
type sthree struct {
SecretAccessKey string `validate:"required"`
AccessKeyID string `validate:"required"`
Region string `validate:"required"`
Bucket string `validate:"required"`
Path string `validate:"required"`
Concurrency int `validate:"required"`
}
func assignEnv(v *string, key string) {
if *v == "" && os.Getenv(key) != "" {
*v = os.Getenv(key)
}
}
func newSthree(id, key, region, bucket, path string, concurrency int) (*sthree, error) {
assignEnv(&id, "AWS_ACCESS_KEY_ID")
assignEnv(&key, "AWS_SECRET_ACCESS_KEY")
assignEnv(®ion, "AWS_REGION")
assignEnv(&bucket, "AWS_BUCKET")
s := &sthree{
AccessKeyID: id,
SecretAccessKey: key,
Region: region,
Bucket: bucket,
Path: path,
Concurrency: concurrency,
}
config := &validator.Config{TagName: "validate"}
validate := validator.New(config)
err := validate.Struct(s)
if err != nil {
return nil, err
}
return s, nil
}
func today() string {
t := time.Now().Local()
return t.Format("20060102")
}
func saveName(filePath string) string {
rep := regexp.MustCompile(fmt.Sprintf(`(\.1$|\.gz$|[-\.]%s)`, today()))
filePath = rep.ReplaceAllString(filePath, "")
hostname, err := os.Hostname()
if err != nil {
logrus.Error(err)
return filepath.Base(fmt.Sprintf("%s.%s.gz", filePath, today()))
}
if os.Getenv("GOTHREE_PREFIX") != "" {
hostname = os.Getenv("GOTHREE_PREFIX")
}
return fmt.Sprintf("%s-%s", hostname, filepath.Base(fmt.Sprintf("%s.%s.gz", filePath, today())))
}
func exists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}
func lotateFileName(filePath string) string {
patterns := []string{
fmt.Sprintf("%s.1", filePath),
fmt.Sprintf("%s-%s", filePath, today()),
fmt.Sprintf("%s.%s", filePath, today()),
}
for _, p := range patterns {
if exists(p) {
return p
}
}
return filePath
}
func (s *sthree) Put(filePath string) error {
var reader io.Reader
filePath = lotateFileName(filePath)
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
if !strings.HasSuffix(filePath, ".gz") {
r, w := io.Pipe()
reader = r
go func() {
gw := gzip.NewWriter(w)
io.Copy(gw, file)
file.Close()
gw.Close()
w.Close()
}()
} else {
reader = file
}
st, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(s.AccessKeyID, s.SecretAccessKey, ""),
Region: &s.Region,
})
if err != nil {
return err
}
uploader := s3manager.NewUploader(st)
uploader.Concurrency = s.Concurrency
logrus.Infof("start upload file:%s save: %s", filePath, saveName(filePath))
result, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(filepath.Join(s.Path, saveName(filePath))),
Body: reader,
})
if err != nil {
return err
}
logrus.Infof("upload success: %s", result.Location)
return nil
}