-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (133 loc) · 3.4 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
package main
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/timrourke/funnel/s3"
"github.com/timrourke/funnel/tpl"
"github.com/timrourke/funnel/upload"
"golang.org/x/crypto/ssh/terminal"
"os"
"strings"
)
func validateCommandLineFlags() error {
if "" == strings.TrimSpace(region) {
return errors.New("must provide an AWS region where your S3 bucket exists")
}
if "" == strings.TrimSpace(bucket) {
return errors.New("must specify an AWS S3 bucket to save files in")
}
if numConcurrentUploads <= 0 || numConcurrentUploads > 100 {
return errors.New("number of concurrent uploads must be within the range 1-100")
}
return nil
}
var (
bucket string
logger = logrus.New()
numConcurrentUploads int
s3ObjectKeyTemplate string
shouldDeleteFileAfterUpload bool
shouldWatchPaths bool
region string
rootCmd = &cobra.Command{
Use: "funnel [OPTIONS] [PATHS]",
Short: "Funnel is a tool for quickly saving files to AWS S3.",
Example: "funnel --region=us-east-1 --bucket=some-cool-bucket /some/directory",
Version: "0.0.1",
RunE: func(cmd *cobra.Command, args []string) error {
return Execute(cmd, args)
},
}
)
// Execute configures the application and executes the root cobra command
func Execute(cmd *cobra.Command, args []string) error {
err := validateCommandLineFlags()
if err != nil {
return err
}
config := aws.NewConfig().
WithRegion(region).
WithMaxRetries(3)
sess := session.Must(session.NewSession(config))
s3UploadManager := s3manager.NewUploader(sess)
s3Uploader := s3.NewS3Uploader(s3UploadManager, bucket, logger)
keyTemplate, err := tpl.NewKeyTemplate(s3ObjectKeyTemplate, logger)
if err != nil {
return err
}
uploader := upload.NewUploader(
shouldDeleteFileAfterUpload,
shouldWatchPaths,
numConcurrentUploads,
s3Uploader,
keyTemplate,
logger,
)
return uploader.UploadFilesFromPathToBucket(args)
}
func configureLogger() {
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
logger.SetFormatter(&logrus.JSONFormatter{})
}
}
func configureRootCmd() {
rootCmd.PersistentFlags().StringVarP(
®ion,
"region",
"r",
"",
"The AWS region your S3 bucket is in, eg. \"us-east-1\"",
)
if "" == strings.TrimSpace(region) {
region = os.Getenv("AWS_DEFAULT_REGION")
}
rootCmd.PersistentFlags().StringVarP(
&bucket,
"bucket",
"b",
"",
"The AWS S3 bucket you want to save files to",
)
rootCmd.PersistentFlags().BoolVarP(
&shouldWatchPaths,
"watch",
"w",
false,
"Whether to watch a path for changes",
)
rootCmd.PersistentFlags().IntVarP(
&numConcurrentUploads,
"num-concurrent-uploads",
"n",
10,
"Number of concurrent uploads",
)
rootCmd.PersistentFlags().BoolVarP(
&shouldDeleteFileAfterUpload,
"delete-file-after-upload",
"",
false,
"Whether to delete the uploaded file after a successful upload",
)
rootCmd.PersistentFlags().StringVarP(
&s3ObjectKeyTemplate,
"s3-object-key-template",
"t",
"{{ filePath }}",
"The layout template to use for defining the key of an uploaded file",
)
rootCmd.DisableFlagsInUseLine = true
}
func init() {
configureLogger()
configureRootCmd()
}
func main() {
if err := rootCmd.Execute(); err != nil {
logger.Fatal(err)
}
}