-
Notifications
You must be signed in to change notification settings - Fork 106
/
command.go
180 lines (145 loc) · 4.51 KB
/
command.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
package out
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/concourse/s3-resource"
"github.com/concourse/s3-resource/versions"
"github.com/fatih/color"
)
var ErrObjectVersioningNotEnabled = errors.New("object versioning not enabled")
var ErrorColor = color.New(color.FgWhite, color.BgRed, color.Bold)
var BlinkingErrorColor = color.New(color.BlinkSlow, color.FgWhite, color.BgRed, color.Bold)
func init() {
ErrorColor.EnableColor()
}
type Command struct {
stderr io.Writer
s3client s3resource.S3Client
}
func NewCommand(stderr io.Writer, s3client s3resource.S3Client) *Command {
return &Command{
stderr: stderr,
s3client: s3client,
}
}
func (command *Command) Run(sourceDir string, request Request) (Response, error) {
if request.Params.From != "" || request.Params.To != "" {
command.printDeprecationWarning()
}
if ok, message := request.Source.IsValid(); !ok {
return Response{}, errors.New(message)
}
if request.Params.File != "" && request.Params.From != "" {
return Response{}, errors.New("contains both file and from")
}
localPath, err := command.match(request.Params, sourceDir)
if err != nil {
return Response{}, err
}
remotePath := command.remotePath(request, localPath, sourceDir)
bucketName := request.Source.Bucket
options := s3resource.NewUploadFileOptions()
if request.Params.Acl != "" {
options.Acl = request.Params.Acl
}
options.ContentType = request.Params.ContentType
options.ServerSideEncryption = request.Source.ServerSideEncryption
options.KmsKeyId = request.Source.SSEKMSKeyId
options.DisableMultipart = request.Source.DisableMultipart
versionID, err := command.s3client.UploadFile(
bucketName,
remotePath,
localPath,
options,
)
if err != nil {
return Response{}, err
}
version := s3resource.Version{}
if request.Source.VersionedFile != "" {
if versionID == "" {
return Response{}, ErrObjectVersioningNotEnabled
}
version.VersionID = versionID
} else {
version.Path = remotePath
}
return Response{
Version: version,
Metadata: command.metadata(bucketName, remotePath, request.Source.Private, versionID),
}, nil
}
func (command *Command) remotePath(request Request, localPath string, sourceDir string) string {
if request.Source.VersionedFile != "" {
return request.Source.VersionedFile
}
if request.Params.To == "" && request.Params.From == "" && request.Source.Regexp != "" {
return filepath.Join(parentDir(request.Source.Regexp), filepath.Base(localPath))
}
folderDestination := strings.HasSuffix(request.Params.To, "/")
if folderDestination || request.Params.To == "" {
return filepath.Join(request.Params.To, filepath.Base(localPath))
}
compiled := regexp.MustCompile(request.Params.From)
fileName := strings.TrimPrefix(localPath, sourceDir+"/")
return compiled.ReplaceAllString(fileName, request.Params.To)
}
func parentDir(regexp string) string {
return regexp[:strings.LastIndex(regexp, "/")+1]
}
func (command *Command) match(params Params, sourceDir string) (string, error) {
var matches []string
var err error
var pattern string
if params.File != "" {
pattern = params.File
matches, err = filepath.Glob(filepath.Join(sourceDir, pattern))
} else {
paths := []string{}
filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error {
paths = append(paths, path)
return nil
})
pattern = params.From
matches, err = versions.MatchUnanchored(paths, pattern)
}
if err != nil {
return "", err
}
if len(matches) == 0 {
return "", fmt.Errorf("no matches found for pattern: %s", pattern)
}
if len(matches) > 1 {
return "", fmt.Errorf("more than one match found for pattern: %s\n%v", pattern, matches)
}
return matches[0], nil
}
func (command *Command) metadata(bucketName, remotePath string, private bool, versionID string) []s3resource.MetadataPair {
remoteFilename := filepath.Base(remotePath)
metadata := []s3resource.MetadataPair{
s3resource.MetadataPair{
Name: "filename",
Value: remoteFilename,
},
}
if !private {
metadata = append(metadata, s3resource.MetadataPair{
Name: "url",
Value: command.s3client.URL(bucketName, remotePath, false, versionID),
})
}
return metadata
}
func (command *Command) printDeprecationWarning() {
errorColor := ErrorColor.SprintFunc()
blinkColor := BlinkingErrorColor.SprintFunc()
command.stderr.Write([]byte(blinkColor("WARNING:")))
command.stderr.Write([]byte("\n"))
command.stderr.Write([]byte(errorColor("Parameters 'from/to' are deprecated, use 'file' instead")))
command.stderr.Write([]byte("\n\n"))
}