This repository has been archived by the owner on Dec 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
push_source.go
272 lines (225 loc) · 6.43 KB
/
push_source.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
package main
import (
"fmt"
"os"
"strings"
"github.com/phrase/phraseapp-client/internal/paths"
"github.com/phrase/phraseapp-go/phraseapp"
yaml "gopkg.in/yaml.v2"
)
func SourcesFromConfig(config phraseapp.Config) (Sources, error) {
if config.Sources == nil || len(config.Sources) == 0 {
return nil, fmt.Errorf("no sources for upload specified")
}
tmp := struct {
Sources Sources
}{}
err := yaml.Unmarshal(config.Sources, &tmp)
if err != nil {
return nil, err
}
srcs := tmp.Sources
projectId := config.DefaultProjectID
fileFormat := config.DefaultFileFormat
validSources := []*Source{}
for _, source := range srcs {
if source == nil {
continue
}
if source.ProjectID == "" {
source.ProjectID = projectId
}
if source.Params == nil {
source.Params = new(phraseapp.UploadParams)
}
if source.Params.FileFormat == nil {
switch {
case source.FileFormat != "":
source.Params.FileFormat = &source.FileFormat
case fileFormat != "":
source.Params.FileFormat = &fileFormat
}
}
validSources = append(validSources, source)
}
if len(validSources) <= 0 {
return nil, fmt.Errorf("no sources could be identified! Refine the sources list in your config")
}
return validSources, nil
}
type Sources []*Source
func (sources Sources) Validate() error {
for _, source := range sources {
if err := source.CheckPreconditions(); err != nil {
return err
}
}
return nil
}
type Source struct {
File string
ProjectID string
Branch string
AccessToken string
FileFormat string
Params *phraseapp.UploadParams
RemoteLocales []*phraseapp.Locale
Format *phraseapp.Format
}
func (source *Source) GetLocaleID() string {
if source.Params != nil && source.Params.LocaleID != nil {
return *source.Params.LocaleID
}
return ""
}
func (source *Source) GetFileFormat() string {
if source.Params != nil && source.Params.FileFormat != nil {
return *source.Params.FileFormat
}
if source.FileFormat != "" {
return source.FileFormat
}
return ""
}
func (source *Source) CheckPreconditions() error {
if err := paths.Validate(source.File, source.FileFormat, ""); err != nil {
return err
}
duplicatedPlaceholders := []string{}
for _, name := range []string{"<locale_name>", "<locale_code>", "<tag>"} {
if strings.Count(source.File, name) > 1 {
duplicatedPlaceholders = append(duplicatedPlaceholders, name)
}
}
starCount := strings.Count(source.File, "*")
recCount := strings.Count(source.File, "**")
// starCount contains the `**` so that must be taken into account.
if starCount-(recCount*2) > 1 {
duplicatedPlaceholders = append(duplicatedPlaceholders, "*")
}
if recCount > 1 {
duplicatedPlaceholders = append(duplicatedPlaceholders, "**")
}
if len(duplicatedPlaceholders) > 0 {
dups := strings.Join(duplicatedPlaceholders, ", ")
return fmt.Errorf(fmt.Sprintf("%s can only occur once in a file pattern!", dups))
}
return nil
}
func (src *Source) UnmarshalYAML(unmarshal func(interface{}) error) error {
m := map[string]interface{}{}
err := phraseapp.ParseYAMLToMap(unmarshal, map[string]interface{}{
"file": &src.File,
"project_id": &src.ProjectID,
"access_token": &src.AccessToken,
"file_format": &src.FileFormat,
"params": &m,
})
if err != nil {
return err
}
src.Params = new(phraseapp.UploadParams)
return src.Params.ApplyValuesFromMap(m)
}
func (sources Sources) ProjectIds() []string {
projectIds := []string{}
for _, source := range sources {
projectIds = append(projectIds, source.ProjectID)
}
return projectIds
}
func (source *Source) uploadFile(client *phraseapp.Client, localeFile *LocaleFile, branch string) (*phraseapp.Upload, error) {
if Debug {
fmt.Fprintln(os.Stdout, "Source file pattern:", source.File)
fmt.Fprintln(os.Stdout, "Actual file location:", localeFile.Path)
}
params := new(phraseapp.UploadParams)
*params = *source.Params
params.File = &localeFile.Path
if params.LocaleID == nil {
switch {
case localeFile.ID != "":
params.LocaleID = &localeFile.ID
case localeFile.Code != "":
params.LocaleID = &localeFile.Code
}
}
if localeFile.Tag != "" {
var v string
if params.Tags != nil {
v = *params.Tags + ","
}
v += localeFile.Tag
params.Tags = &v
}
if branch != "" {
params.Branch = &branch
}
return client.UploadCreate(source.ProjectID, params)
}
func (source *Source) createLocale(client *phraseapp.Client, localeFile *LocaleFile, branch string) (*phraseapp.LocaleDetails, error) {
localeDetails, found, err := source.getLocaleIfExist(client, localeFile, branch)
if err != nil {
return nil, err
} else if found {
return localeDetails, nil
}
localeParams := new(phraseapp.LocaleParams)
if localeFile.Name != "" {
localeParams.Name = &localeFile.Name
} else if localeFile.Code != "" {
localeParams.Name = &localeFile.Code
}
if localeFile.Code == "" {
localeFile.Code = localeFile.Name
}
localeName := source.replacePlaceholderInParams(localeFile)
if localeName != "" && localeName != localeFile.Code {
localeParams.Name = &localeName
}
if localeFile.Code != "" {
localeParams.Code = &localeFile.Code
}
if branch != "" {
localeParams.Branch = &branch
}
// localeParams.Branch = branch
localeDetails, err = client.LocaleCreate(source.ProjectID, localeParams)
if err != nil {
return nil, err
}
return localeDetails, nil
}
func (source *Source) getLocaleIfExist(client *phraseapp.Client, localeFile *LocaleFile, branch string) (*phraseapp.LocaleDetails, bool, error) {
identifier := localeIdentifier(source, localeFile)
if identifier == "" {
return nil, false, nil
}
localeShowParams := &phraseapp.LocaleShowParams{Branch: &branch}
localeDetail, err := client.LocaleShow(source.ProjectID, identifier, localeShowParams)
if phraseapp.IsErrNotFound(err) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return localeDetail, true, nil
}
func localeIdentifier(source *Source, localeFile *LocaleFile) string {
localeName := source.replacePlaceholderInParams(localeFile)
if localeName != "" && localeName != localeFile.Code {
return localeName
}
if localeFile.Name != "" {
return localeFile.Name
}
if localeFile.Code != "" {
return localeFile.Code
}
return ""
}
func (source *Source) replacePlaceholderInParams(localeFile *LocaleFile) string {
if localeFile.Code != "" && strings.Contains(source.GetLocaleID(), "<locale_code>") {
return strings.Replace(source.GetLocaleID(), "<locale_code>", localeFile.Code, 1)
}
return ""
}