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
/
Copy pathpull.go
258 lines (219 loc) · 6.38 KB
/
pull.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/phrase/phraseapp-client/internal/paths"
"github.com/phrase/phraseapp-client/internal/placeholders"
"github.com/phrase/phraseapp-client/internal/print"
"github.com/phrase/phraseapp-go/phraseapp"
)
const (
timeoutInMinutes = 30 * time.Minute
)
type PullCommand struct {
phraseapp.Config
Branch string `cli:"opt --branch"`
UseLocalBranchName bool `cli:"opt --use-local-branch-name desc='pull from the branch with the name of your currently checked out branch (git or mercurial)'"`
}
func (cmd *PullCommand) Run() error {
if cmd.Config.Debug {
// suppresses content output
cmd.Config.Debug = false
Debug = true
}
client, err := newClient(cmd.Config.Credentials, cmd.Config.Debug)
if err != nil {
return err
}
targets, err := TargetsFromConfig(cmd.Config)
if err != nil {
return err
}
branchName, err := usedBranchName(cmd.UseLocalBranchName, cmd.Branch)
if err != nil {
return err
}
cmd.Branch = branchName
projectIdToLocales, err := LocalesForProjects(client, targets, cmd.Branch)
if err != nil {
return err
}
for _, target := range targets {
val, ok := projectIdToLocales[LocaleCacheKey{target.ProjectID, cmd.Branch}]
if !ok || len(val) == 0 {
if cmd.Branch != "" {
continue
}
return fmt.Errorf("Could not find any locales for project %q", target.ProjectID)
}
target.RemoteLocales = val
}
for _, target := range targets {
err := target.Pull(client, cmd.Branch)
if err != nil {
return err
}
}
return nil
}
type PullParams struct {
phraseapp.LocaleDownloadParams
LocaleID string
}
func (target *Target) Pull(client *phraseapp.Client, branch string) error {
if err := target.CheckPreconditions(); err != nil {
return err
}
localeFiles, err := target.LocaleFiles()
if err != nil {
return err
}
startedAt := time.Now()
for _, localeFile := range localeFiles {
if time.Since(startedAt) >= timeoutInMinutes {
return fmt.Errorf("Timeout of %d minutes exceeded", timeoutInMinutes)
}
err := createFile(localeFile.Path)
if err != nil {
return err
}
err = target.DownloadAndWriteToFile(client, localeFile, branch)
if err != nil {
return fmt.Errorf("%s for %s", err, localeFile.Path)
} else {
print.Success("Downloaded %s to %s", localeFile.Message(), localeFile.RelPath())
}
if Debug {
fmt.Fprintln(os.Stderr, strings.Repeat("-", 10))
}
}
return nil
}
func (target *Target) DownloadAndWriteToFile(client *phraseapp.Client, localeFile *LocaleFile, branch string) error {
downloadParams := &phraseapp.LocaleDownloadParams{Branch: &branch}
if target.Params != nil {
*downloadParams = target.Params.LocaleDownloadParams
downloadParams.Branch = &branch
}
if downloadParams.FileFormat == nil {
downloadParams.FileFormat = &localeFile.FileFormat
}
if Debug {
fmt.Fprintln(os.Stderr, "Target file pattern:", target.File)
fmt.Fprintln(os.Stderr, "Actual file path", localeFile.Path)
fmt.Fprintln(os.Stderr, "LocaleID", localeFile.ID)
fmt.Fprintln(os.Stderr, "ProjectID", target.ProjectID)
fmt.Fprintln(os.Stderr, "FileFormat", downloadParams.FileFormat)
fmt.Fprintln(os.Stderr, "ConvertEmoji", downloadParams.ConvertEmoji)
fmt.Fprintln(os.Stderr, "IncludeEmptyTranslations", downloadParams.IncludeEmptyTranslations)
fmt.Fprintln(os.Stderr, "KeepNotranslateTags", downloadParams.KeepNotranslateTags)
fmt.Fprintln(os.Stderr, "Tag", downloadParams.Tag)
fmt.Fprintln(os.Stderr, "FormatOptions", downloadParams.FormatOptions)
}
res, err := client.LocaleDownload(target.ProjectID, localeFile.ID, downloadParams)
if err != nil {
if rateLimitError, ok := (err).(*phraseapp.RateLimitingError); ok {
waitForRateLimit(rateLimitError)
res, err = client.LocaleDownload(target.ProjectID, localeFile.ID, downloadParams)
if err != nil {
return err
}
} else {
return err
}
}
err = ioutil.WriteFile(localeFile.Path, res, 0700)
return err
}
func (target *Target) LocaleFiles() (LocaleFiles, error) {
files := []*LocaleFile{}
if target.GetLocaleID() != "" {
// a specific locale was requested
remoteLocale, err := target.localeForRemote()
if err != nil {
return nil, err
}
localeFiles, err := target.createLocaleFiles(remoteLocale)
if err != nil {
return nil, err
}
files = append(files, localeFiles...)
} else if placeholders.ContainsLocalePlaceholder(target.File) {
// multiple locales were requested
for _, remoteLocale := range target.RemoteLocales {
localesFiles, err := target.createLocaleFiles(remoteLocale)
if err != nil {
return nil, err
}
files = append(files, localesFiles...)
}
} else {
// no local files match remote locale
return nil, fmt.Errorf("could not find any files on your system that matches the locales for project %q", target.ProjectID)
}
return files, nil
}
func (target *Target) createLocaleFiles(remoteLocale *phraseapp.Locale) (LocaleFiles, error) {
files := []*LocaleFile{}
tags := target.GetTags()
if len(tags) == 0 {
localeFile, err := createLocaleFile(target, remoteLocale, "")
if err != nil {
return nil, err
}
files = append(files, localeFile)
} else {
for _, tag := range tags {
localeFile, err := createLocaleFile(target, remoteLocale, tag)
if err != nil {
return nil, err
}
files = append(files, localeFile)
}
}
return files, nil
}
func waitForRateLimit(rateLimitError *phraseapp.RateLimitingError) {
if rateLimitError.Remaining == 0 {
reset := rateLimitError.Reset
resetTime := reset.Add(time.Second * 5).Sub(time.Now())
fmt.Printf("Rate limit exceeded. Download will resume in %d seconds\n", int64(resetTime.Seconds()))
time.Sleep(resetTime)
}
}
func createLocaleFile(target *Target, remoteLocale *phraseapp.Locale, tag string) (*LocaleFile, error) {
localeFile := &LocaleFile{
Name: remoteLocale.Name,
ID: remoteLocale.ID,
Code: remoteLocale.Code,
Tag: tag,
FileFormat: target.GetFormat(),
Path: target.File,
}
absPath, err := target.ReplacePlaceholders(localeFile)
if err != nil {
return nil, err
}
localeFile.Path = absPath
return localeFile, nil
}
func createFile(path string) error {
err := paths.Exists(path)
if err != nil {
absDir := filepath.Dir(path)
err := paths.Exists(absDir)
if err != nil {
os.MkdirAll(absDir, 0700)
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
}
return nil
}