forked from 99designs/smartling
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdo_files_push.go
302 lines (253 loc) · 6.14 KB
/
do_files_push.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/Smartling/api-sdk-go"
"github.com/reconquest/hierr-go"
)
func doFilesPush(
client smartling.ClientInterface,
config Config,
args map[string]interface{},
) error {
var (
failedFiles []string
project = config.ProjectID
result error
file, _ = args["<file>"].(string)
uri, useURI = args["<uri>"].(string)
branch, _ = args["--branch"].(string)
locales, _ = args["--locale"].([]string)
authorize = args["--authorize"].(bool)
directory = args["--directory"].(string)
fileType, _ = args["--type"].(string)
directives, _ = args["--directive"].([]string)
)
if branch == "@auto" {
var err error
branch, err = getGitBranch()
if err != nil {
return hierr.Errorf(
err,
"unable to autodetect branch name",
)
}
logger.Infof("autodetected branch name: %s", branch)
}
if branch != "" {
branch = strings.TrimSuffix(branch, "/") + "/"
}
patterns := []string{}
if file != "" {
patterns = append(patterns, file)
} else {
for pattern, section := range config.Files {
if section.Push.Type != "" {
patterns = append(patterns, pattern)
}
}
}
files := []string{}
for _, pattern := range patterns {
base, pattern := getDirectoryFromPattern(pattern)
chunk, err := globFilesLocally(
directory,
base,
pattern,
)
if err != nil {
return NewError(
hierr.Errorf(
err,
`unable to find matching files to upload`,
),
`Check, that specified pattern is valid and refer to help for`+
` more information about glob patterns.`,
)
}
files = append(files, chunk...)
}
if len(files) == 0 {
return NewError(
fmt.Errorf(`no files found by specified patterns`),
`Check command line pattern if any and configuration file for`+
` more patterns to search for.`,
)
}
if uri != "" && len(files) > 1 {
return NewError(
fmt.Errorf(
`more than one file is matching speciifed pattern and <uri>`+
` is specified too`,
),
`Either remove <uri> argument or make sure that only one file`+
` is matching mask.`,
)
}
base, err := filepath.Abs(config.path)
if err != nil {
return NewError(
hierr.Errorf(
err,
`unable to resolve absolute path to config`,
),
`It's internal error, please, contact developer for more info`,
)
}
base = filepath.Dir(base)
for _, file := range files {
name, err := filepath.Abs(file)
if err != nil {
return NewError(
hierr.Errorf(
err,
`unable to resolve absolute path to file: %q`,
file,
),
`Check, that file exists and you have proper permissions `+
`to access it.`,
)
}
if !filepath.HasPrefix(name, base) {
return NewError(
errors.New(
`you are trying to push file outside project directory`,
),
`Check file path and path to configuration file and try again.`,
)
}
name, err = filepath.Rel(base, name)
if err != nil {
return NewError(
hierr.Errorf(
err,
`unable to resolve relative path to file: %q`,
file,
),
`Check, that file exists and you have proper permissions `+
`to access it.`,
)
}
if !useURI {
uri = name
}
fileConfig, err := config.GetFileConfig(file)
if err != nil {
return NewError(
hierr.Errorf(
err,
`unable to retrieve file specific configuration`,
),
``,
)
}
contents, err := ioutil.ReadFile(file)
if err != nil {
return NewError(
hierr.Errorf(
err,
`unable to read file contents "%s"`,
file,
),
`Check that file exists and readable by current user.`,
)
}
request := smartling.FileUploadRequest{
File: contents,
Authorize: authorize,
LocalesToAuthorize: locales,
}
request.FileURI = branch + uri
if fileConfig.Push.Type == "" {
if fileType == "" {
request.FileType = smartling.GetFileTypeByExtension(
filepath.Ext(file),
)
if request.FileType == smartling.FileTypeUnknown {
return NewError(
fmt.Errorf(
"unable to deduce file type from extension: %q",
filepath.Ext(file),
),
`You need to specify file type via --type option.`,
)
}
} else {
request.FileType = smartling.FileType(fileType)
}
} else {
request.FileType = smartling.FileType(fileConfig.Push.Type)
}
request.Smartling.Directives = fileConfig.Push.Directives
for _, directive := range directives {
spec := strings.SplitN(directive, "=", 2)
if len(spec) != 2 {
return NewError(
fmt.Errorf(
"invalid directive specification: %q",
directive,
),
`Should be in the form of <name>=<value>.`,
)
}
if request.Smartling.Directives == nil {
request.Smartling.Directives = map[string]string{}
}
request.Smartling.Directives[spec[0]] = spec[1]
}
response, err := client.UploadFile(project, request)
if err != nil {
if returnError(err) {
return NewError(
err,
fmt.Sprintf(`unable to upload file "%s"`, file),
`Check, that you have enough permissions to upload file to`+
` the specified project`,
)
}
_, _ = fmt.Fprintln(os.Stderr, "Unable to upload file "+file)
failedFiles = append(failedFiles, file)
} else {
status := "new"
if response.Overwritten {
status = "overwritten"
}
fmt.Printf(
"%s (%s) %s [%d strings %d words]\n",
uri,
request.FileType,
status,
response.StringCount,
response.WordCount,
)
}
}
if len(failedFiles) != 0 {
result = NewError(fmt.Errorf("failed to upload %d files", len(failedFiles)), "failed to upload files "+strings.Join(failedFiles, ", "))
}
return result
}
func returnError(err error) bool {
if errors.Is(err, smartling.NotAuthorizedError{}) {
return true
}
for {
smartlingApiError, isSmartlingApiError := err.(smartling.APIError)
if isSmartlingApiError {
reasons := map[string]struct{}{
"AUTHENTICATION_ERROR": {},
"AUTHORIZATION_ERROR": {},
"MAINTENANCE_MODE_ERROR": {},
}
_, stopExecution := reasons[smartlingApiError.Code]
return stopExecution
}
if err = errors.Unwrap(err); err == nil {
return false
}
}
}