Skip to content

Commit bc72112

Browse files
author
Phrase
committed
1 parent 6e007ba commit bc72112

File tree

4 files changed

+65
-117
lines changed

4 files changed

+65
-117
lines changed

cmd/internal/push.go

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ type PushCommand struct {
3030
Tag string
3131
}
3232

33+
type PushResult struct {
34+
ProjectID string
35+
Branch string
36+
UploadIDs []string
37+
}
38+
3339
func (cmd *PushCommand) Run() error {
3440
if cmd.Config.Debug {
3541
// suppresses content output
@@ -141,24 +147,51 @@ func (cmd *PushCommand) Run() error {
141147
}
142148
}
143149

150+
pushResults := []*PushResult{}
151+
144152
for _, source := range sources {
145-
err := source.Push(client, cmd.Wait, cmd.Cleanup, cmd.Branch, cmd.Tag)
153+
pushResult, err := source.Push(client, cmd.Wait, cmd.Branch, cmd.Tag)
146154
if err != nil {
147155
return err
148156
}
157+
if cmd.Wait && cmd.Cleanup {
158+
// collect all upload ids for cleanup by project and branch
159+
found := false
160+
for _, result := range pushResults {
161+
if result.ProjectID == pushResult.ProjectID && result.Branch == pushResult.Branch {
162+
result.UploadIDs = append(result.UploadIDs, pushResult.UploadIDs...)
163+
found = true
164+
break
165+
}
166+
}
167+
if !found {
168+
pushResults = append(pushResults, pushResult)
169+
}
170+
}
171+
}
172+
for _, pushResult := range pushResults {
173+
UploadCleanup(client, true, pushResult.UploadIDs, pushResult.Branch, pushResult.ProjectID)
149174
}
150175

151176
return nil
152177
}
153178

154-
func (source *Source) Push(client *phrase.APIClient, waitForResults bool, cleanup bool, branch string, tag string) error {
179+
func (source *Source) Push(client *phrase.APIClient, waitForResults bool, branch string, tag string) (*PushResult, error) {
155180
localeFiles, err := source.LocaleFiles()
156181
if err != nil {
157-
return err
182+
return nil, err
158183
}
159184

160185
noErrors := true
161-
uploadIds := []string{}
186+
pushResult := &PushResult{
187+
ProjectID: source.ProjectID,
188+
Branch: source.Params.Branch.Value(),
189+
UploadIDs: []string{},
190+
}
191+
if branch != "" {
192+
pushResult.Branch = branch
193+
}
194+
162195
for _, localeFile := range localeFiles {
163196
print.NonBatchPrintf("Uploading %s... ", localeFile.RelPath())
164197

@@ -179,9 +212,9 @@ func (source *Source) Push(client *phrase.APIClient, waitForResults bool, cleanu
179212
if openapiError, ok := err.(phrase.GenericOpenAPIError); ok {
180213
print.Warn("\nAPI response: %s", openapiError.Body())
181214
}
182-
return err
215+
return nil, err
183216
}
184-
uploadIds = append(uploadIds, upload.Id)
217+
pushResult.UploadIDs = append(pushResult.UploadIDs, upload.Id)
185218

186219
if waitForResults {
187220
print.NonBatchPrintf("\n")
@@ -198,7 +231,7 @@ func (source *Source) Push(client *phrase.APIClient, waitForResults bool, cleanu
198231
print.NonBatchPrintf("\n")
199232

200233
if err := <-taskErr; err != nil {
201-
return err
234+
return nil, err
202235
}
203236

204237
switch <-taskResult {
@@ -218,14 +251,10 @@ func (source *Source) Push(client *phrase.APIClient, waitForResults bool, cleanu
218251
}
219252
}
220253
if noErrors {
221-
if waitForResults && cleanup {
222-
return UploadCleanup(client, true, uploadIds, branch, source.ProjectID)
223-
}
254+
return pushResult, nil
224255
} else {
225-
return errors.New("not all files were uploaded successfully")
256+
return nil, errors.New("not all files were uploaded successfully")
226257
}
227-
228-
return nil
229258
}
230259

231260
func outputUpload(upload *phrase.Upload) {

cmd/internal/shared.go

Lines changed: 22 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"reflect"
8-
"sort"
98
"strings"
109

1110
"github.com/antihax/optional"
@@ -15,7 +14,6 @@ import (
1514
"github.com/phrase/phrase-cli/cmd/internal/shared"
1615
"github.com/phrase/phrase-go/v4"
1716
"github.com/spf13/viper"
18-
"golang.org/x/exp/maps"
1917
)
2018

2119
var Debug bool
@@ -212,66 +210,11 @@ func StringToInterface() mapstructure.DecodeHookFunc {
212210
}
213211
}
214212

215-
func findCommonUnmentionedKeys(client *phrase.APIClient, ids []string, branch string, projectId string) ([]phrase.TranslationKey, error) {
216-
commonUnmentionedKeys := map[string]phrase.TranslationKey{}
217-
alreadyInitialized := false
218-
for _, id := range ids {
219-
q := "unmentioned_in_upload:" + id
220-
keysListLocalVarOptionals := phrase.KeysListOpts{
221-
Page: optional.NewInt32(1),
222-
PerPage: optional.NewInt32(100),
223-
Q: optional.NewString(q),
224-
Branch: optional.NewString(branch),
225-
}
226-
allUnmentionedKeysInUpload := map[string]phrase.TranslationKey{}
227-
keys, _, err := client.KeysApi.KeysList(Auth, projectId, &keysListLocalVarOptionals)
228-
if err != nil {
229-
return nil, err
230-
}
231-
for len(keys) != 0 {
232-
for _, key := range keys {
233-
allUnmentionedKeysInUpload[key.Id] = key
234-
}
235-
236-
keysListLocalVarOptionals.Page = optional.NewInt32(keysListLocalVarOptionals.Page.Value() + 1)
237-
238-
keys, _, err = client.KeysApi.KeysList(Auth, projectId, &keysListLocalVarOptionals)
239-
if err != nil {
240-
return nil, err
241-
}
242-
}
243-
244-
if alreadyInitialized {
245-
newUnmentioned := map[string]phrase.TranslationKey{}
246-
for id, key := range allUnmentionedKeysInUpload {
247-
if _, ok := commonUnmentionedKeys[id]; ok {
248-
newUnmentioned[id] = key
249-
}
250-
}
251-
commonUnmentionedKeys = newUnmentioned
252-
} else {
253-
commonUnmentionedKeys = allUnmentionedKeysInUpload
254-
alreadyInitialized = true
255-
}
256-
}
257-
258-
return maps.Values(commonUnmentionedKeys), nil
259-
}
260-
261-
func deleteKeys(client *phrase.APIClient, confirm bool, branch string, projectId string, keys []phrase.TranslationKey) error {
262-
ids := make([]string, len(keys))
263-
names := make([]string, len(keys))
264-
for i, key := range keys {
265-
ids[i] = key.Id
266-
names[i] = key.Name
267-
}
268-
213+
func UploadCleanup(client *phrase.APIClient, confirm bool, ids []string, branch string, projectId string) error {
269214
if !shared.BatchMode {
270-
fmt.Println("Following key(s) are about to be deleted from your project:")
271-
sort.Strings(names)
272-
fmt.Println(strings.Join(names, "\n"))
215+
fmt.Println("Keys not mentioned in the following uploads will be deleted:")
216+
fmt.Println(strings.Join(ids, "\n"))
273217
}
274-
275218
if !confirm {
276219
if shared.BatchMode {
277220
return errors.New("Can't ask for confirmation in batch mode. Aborting")
@@ -288,50 +231,28 @@ func deleteKeys(client *phrase.APIClient, confirm bool, branch string, projectId
288231
}
289232
}
290233

291-
const ChunkSize = 100
292-
totalAffected := int32(0)
293-
294-
for i := 0; i < len(ids); i += ChunkSize {
295-
end := i + ChunkSize
296-
297-
if end > len(ids) {
298-
end = len(ids)
299-
}
300-
301-
q := "ids:" + strings.Join(ids[i:end], ",")
302-
keysDeletelocalVarOptionals := phrase.KeysDeleteCollectionOpts{
303-
Q: optional.NewString(q),
304-
Branch: optional.NewString(branch),
305-
}
306-
affected, _, err := client.KeysApi.KeysDeleteCollection(Auth, projectId, &keysDeletelocalVarOptionals)
307-
308-
if err != nil {
309-
return err
310-
}
311-
totalAffected += affected.RecordsAffected
312-
if shared.BatchMode {
313-
jsonBuf, jsonErr := json.MarshalIndent(affected, "", " ")
314-
if jsonErr != nil {
315-
print.Error(jsonErr)
316-
}
317-
fmt.Printf("%s\n", string(jsonBuf))
318-
}
234+
q := "unmentioned_in_upload:" + strings.Join(ids, ",")
235+
optionalBranch := optional.String{}
236+
if branch != "" {
237+
optionalBranch = optional.NewString(branch)
319238
}
320-
321-
if !shared.BatchMode {
322-
print.Success("%d key(s) successfully deleted.\n", totalAffected)
239+
keysDeletelocalVarOptionals := phrase.KeysDeleteCollectionOpts{
240+
Q: optional.NewString(q),
241+
Branch: optionalBranch,
323242
}
324-
return nil
325-
}
243+
affected, _, err := client.KeysApi.KeysDeleteCollection(Auth, projectId, &keysDeletelocalVarOptionals)
326244

327-
func UploadCleanup(client *phrase.APIClient, confirm bool, ids []string, branch string, projectId string) error {
328-
keys, error := findCommonUnmentionedKeys(client, ids, branch, projectId)
329-
if error != nil {
330-
return error
245+
if err != nil {
246+
return err
331247
}
332-
if len(keys) == 0 {
333-
print.Success("There were no keys unmentioned in the uploads.")
334-
return nil
248+
if shared.BatchMode {
249+
jsonBuf, jsonErr := json.MarshalIndent(affected, "", " ")
250+
if jsonErr != nil {
251+
print.Error(jsonErr)
252+
}
253+
fmt.Printf("%s\n", string(jsonBuf))
254+
} else {
255+
print.Success("%d key(s) successfully deleted.\n", affected.RecordsAffected)
335256
}
336-
return deleteKeys(client, confirm, branch, projectId, keys)
257+
return nil
337258
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ require (
2626
github.com/spf13/cast v1.7.1 // indirect
2727
github.com/spf13/pflag v1.0.5 // indirect
2828
github.com/subosito/gotenv v1.6.0 // indirect
29-
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67
3029
golang.org/x/oauth2 v0.24.0 // indirect
3130
golang.org/x/sys v0.28.0 // indirect
3231
golang.org/x/text v0.21.0 // indirect
3332
gopkg.in/ini.v1 v1.67.0 // indirect
3433
)
3534

3635
require (
36+
github.com/google/go-cmp v0.6.0 // indirect
3737
github.com/pelletier/go-toml v1.2.0 // indirect
3838
github.com/spf13/jwalterweatherman v1.1.0 // indirect
3939
github.com/stretchr/testify v1.9.0 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
301301
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
302302
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
303303
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
304-
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 h1:1UoZQm6f0P/ZO0w1Ri+f+ifG/gXhegadRdwBIXEFWDo=
305-
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
306304
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
307305
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
308306
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

0 commit comments

Comments
 (0)