Skip to content

Commit 1c37e7b

Browse files
committed
feat(go, CLI): key cleanup using batches #SCD-687
1 parent 54c8be1 commit 1c37e7b

File tree

6 files changed

+67
-67
lines changed

6 files changed

+67
-67
lines changed

clients/cli/cmd/internal/pull_target.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (t *Target) GetTags() []string {
160160
}
161161

162162
func TargetsFromConfig(config phrase.Config) (Targets, error) {
163-
if config.Targets == nil || len(config.Targets) == 0 {
163+
if config.Pull == nil || len(config.Pull) == 0 {
164164
return nil, fmt.Errorf("no targets for download specified")
165165
}
166166

@@ -170,7 +170,7 @@ func TargetsFromConfig(config phrase.Config) (Targets, error) {
170170

171171
targets := viper.New()
172172
targets.SetConfigType("yaml")
173-
err := targets.ReadConfig(bytes.NewReader(config.Targets))
173+
err := targets.ReadConfig(bytes.NewReader(config.Pull))
174174

175175
if err != nil {
176176
return nil, err

clients/cli/cmd/internal/push.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ func (cmd *PushCommand) Run() error {
4343
Debug = true
4444
}
4545

46-
if cmd.Cleanup && !cmd.Wait {
47-
return fmt.Errorf("You can only use the --cleanup option together with --wait")
48-
}
49-
5046
Config = &cmd.Config
5147

5248
client := newClient()
5349

54-
sources, err := SourcesFromConfig(cmd.Config)
50+
sources, deleteUnmentionedKeys, err := SourcesFromConfig(cmd.Config)
5551
if err != nil {
5652
return err
5753
}
5854

55+
// Use delete_unmentioned_keys from config if Cleanup wasn't explicitly set via command line
56+
if deleteUnmentionedKeys && !cmd.Cleanup {
57+
cmd.Cleanup = deleteUnmentionedKeys
58+
}
59+
5960
if err := sources.Validate(); err != nil {
6061
return err
6162
}
@@ -154,23 +155,21 @@ func (cmd *PushCommand) Run() error {
154155
if err != nil {
155156
return err
156157
}
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)
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
169165
}
170166
}
167+
if !found {
168+
pushResults = append(pushResults, pushResult)
169+
}
171170
}
172171
for _, pushResult := range pushResults {
173-
UploadCleanup(client, true, pushResult.UploadIDs, pushResult.Branch, pushResult.ProjectID)
172+
CreateUploadBatch(client, true, pushResult.UploadIDs, pushResult.Branch, pushResult.ProjectID, cmd.Cleanup)
174173
}
175174

176175
return nil

clients/cli/cmd/internal/push_source.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,27 @@ import (
1313
"github.com/spf13/viper"
1414
)
1515

16-
func SourcesFromConfig(config phrase.Config) (Sources, error) {
17-
if config.Sources == nil || len(config.Sources) == 0 {
18-
return nil, fmt.Errorf("no sources for upload specified")
16+
func SourcesFromConfig(config phrase.Config) (Sources, bool, error) {
17+
if config.Push == nil || len(config.Push) == 0 {
18+
return nil, false, fmt.Errorf("no sources for upload specified")
1919
}
2020

2121
tmp := struct {
22-
Sources Sources
22+
DeleteUnmentionedKeys bool `json:"delete_unmentioned_keys,omitempty"`
23+
Sources Sources `json:"sources,omitempty"`
2324
}{}
2425

25-
sources := viper.New()
26-
sources.SetConfigType("yaml")
27-
err := sources.ReadConfig(bytes.NewReader(config.Sources))
26+
pushSection := viper.New()
27+
pushSection.SetConfigType("yaml")
28+
err := pushSection.ReadConfig(bytes.NewReader(config.Push))
2829

2930
if err != nil {
30-
return nil, err
31+
return nil, false, err
3132
}
3233

33-
err = sources.UnmarshalExact(&tmp, ViperStructTag())
34+
err = pushSection.UnmarshalExact(&tmp, ViperStructTag())
3435
if err != nil {
35-
return nil, err
36+
return nil, false, err
3637
}
3738

3839
srcs := tmp.Sources
@@ -64,10 +65,10 @@ func SourcesFromConfig(config phrase.Config) (Sources, error) {
6465
}
6566

6667
if len(validSources) <= 0 {
67-
return nil, fmt.Errorf("no sources could be identified! Refine the sources list in your config")
68+
return nil, false, fmt.Errorf("no sources could be identified! Refine the sources list in your config")
6869
}
6970

70-
return validSources, nil
71+
return validSources, tmp.DeleteUnmentionedKeys, nil
7172
}
7273

7374
type Sources []*Source

clients/cli/cmd/internal/shared.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -210,49 +210,50 @@ func StringToInterface() mapstructure.DecodeHookFunc {
210210
}
211211
}
212212

213-
func UploadCleanup(client *phrase.APIClient, confirm bool, ids []string, branch string, projectId string) error {
214-
if !shared.BatchMode {
215-
fmt.Println("Keys not mentioned in the following uploads will be deleted:")
216-
fmt.Println(strings.Join(ids, "\n"))
217-
}
218-
if !confirm {
219-
if shared.BatchMode {
220-
return errors.New("Can't ask for confirmation in batch mode. Aborting")
221-
}
222-
confirmation := ""
223-
err := prompt.WithDefault("Are you sure you want to continue? (y/n)", &confirmation, "n")
224-
if err != nil {
225-
return err
213+
func CreateUploadBatch(client *phrase.APIClient, confirm bool, ids []string, branch string, projectId string, cleanup bool) error {
214+
if cleanup {
215+
if !shared.BatchMode {
216+
fmt.Println("Keys not mentioned in the following uploads will be deleted:")
217+
fmt.Println(strings.Join(ids, "\n"))
226218
}
219+
if !confirm {
220+
if shared.BatchMode {
221+
return errors.New("Can't ask for confirmation in batch mode. Aborting")
222+
}
223+
confirmation := ""
224+
err := prompt.WithDefault("Are you sure you want to continue? (y/n)", &confirmation, "n")
225+
if err != nil {
226+
return err
227+
}
227228

228-
if strings.ToLower(confirmation) != "y" {
229-
fmt.Println("Clean up aborted")
230-
return nil
229+
if strings.ToLower(confirmation) != "y" {
230+
fmt.Println("Clean up aborted")
231+
return nil
232+
}
231233
}
232234
}
233235

234-
q := "unmentioned_in_upload:" + strings.Join(ids, ",")
235-
optionalBranch := optional.String{}
236-
if branch != "" {
237-
optionalBranch = optional.NewString(branch)
238-
}
239-
keysDeletelocalVarOptionals := phrase.KeysDeleteCollectionOpts{
240-
Q: optional.NewString(q),
241-
Branch: optionalBranch,
236+
uploadBatchesCreateParameters := phrase.UploadBatchesCreateParameters{
237+
Branch: branch,
238+
DeleteUnmentionedKeys: &cleanup,
239+
UploadIds: ids,
242240
}
243-
affected, _, err := client.KeysApi.KeysDeleteCollection(Auth, projectId, &keysDeletelocalVarOptionals)
244-
241+
uploadBatch, _, err := client.UploadBatchesApi.UploadBatchesCreate(Auth, projectId, uploadBatchesCreateParameters, nil)
245242
if err != nil {
246243
return err
247244
}
245+
if !cleanup {
246+
return nil
247+
}
248+
248249
if shared.BatchMode {
249-
jsonBuf, jsonErr := json.MarshalIndent(affected, "", " ")
250+
jsonBuf, jsonErr := json.MarshalIndent(uploadBatch, "", " ")
250251
if jsonErr != nil {
251252
print.Error(jsonErr)
252253
}
253254
fmt.Printf("%s\n", string(jsonBuf))
254255
} else {
255-
print.Success("%d key(s) successfully deleted.\n", affected.RecordsAffected)
256+
print.Success("Keys cleanup scheduled for an upload batch with %d uploads", len(ids))
256257
}
257258
return nil
258259
}

clients/cli/cmd/internal/upload_cleanup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ func (cmd *UploadCleanupCommand) Run() error {
1616
Config = &cmd.Config
1717
client := newClient()
1818

19-
return UploadCleanup(client, cmd.Confirm, cmd.IDs, cmd.Branch, cmd.ProjectID)
19+
return CreateUploadBatch(client, cmd.Confirm, cmd.IDs, cmd.Branch, cmd.ProjectID, true)
2020
}

clients/go/config.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package phrase
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98

@@ -32,8 +31,8 @@ type Config struct {
3231

3332
Defaults map[string]map[string]interface{}
3433

35-
Targets []byte
36-
Sources []byte
34+
Pull []byte
35+
Push []byte
3736

3837
UserAgent string
3938
}
@@ -83,7 +82,7 @@ func configContent(cfgFilePath string) ([]byte, error) {
8382
case path == "":
8483
return nil, nil
8584
default:
86-
return ioutil.ReadFile(path)
85+
return os.ReadFile(path)
8786
}
8887
}
8988

@@ -141,8 +140,8 @@ func (cfg *Config) UnmarshalYAML(unmarshal func(i interface{}) error) error {
141140
"per_page": &cfg.PerPage,
142141
"project_id": &cfg.DefaultProjectID,
143142
"file_format": &cfg.DefaultFileFormat,
144-
"push": &cfg.Sources,
145-
"pull": &cfg.Targets,
143+
"push": &cfg.Push,
144+
"pull": &cfg.Pull,
146145
"defaults": &m,
147146
})
148147
if err != nil {

0 commit comments

Comments
 (0)