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
2119var 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}
0 commit comments