diff --git a/clients/cli/cmd/internal/pull_target.go b/clients/cli/cmd/internal/pull_target.go index 5663cb8a..38ee741c 100644 --- a/clients/cli/cmd/internal/pull_target.go +++ b/clients/cli/cmd/internal/pull_target.go @@ -160,7 +160,7 @@ func (t *Target) GetTags() []string { } func TargetsFromConfig(config phrase.Config) (Targets, error) { - if config.Targets == nil || len(config.Targets) == 0 { + if config.Pull == nil || len(config.Pull) == 0 { return nil, fmt.Errorf("no targets for download specified") } @@ -170,7 +170,7 @@ func TargetsFromConfig(config phrase.Config) (Targets, error) { targets := viper.New() targets.SetConfigType("yaml") - err := targets.ReadConfig(bytes.NewReader(config.Targets)) + err := targets.ReadConfig(bytes.NewReader(config.Pull)) if err != nil { return nil, err diff --git a/clients/cli/cmd/internal/push.go b/clients/cli/cmd/internal/push.go index b2bbce9a..a63ac574 100644 --- a/clients/cli/cmd/internal/push.go +++ b/clients/cli/cmd/internal/push.go @@ -43,19 +43,20 @@ func (cmd *PushCommand) Run() error { Debug = true } - if cmd.Cleanup && !cmd.Wait { - return fmt.Errorf("You can only use the --cleanup option together with --wait") - } - Config = &cmd.Config client := newClient() - sources, err := SourcesFromConfig(cmd.Config) + sources, deleteUnmentionedKeys, err := SourcesFromConfig(cmd.Config) if err != nil { return err } + // Use delete_unmentioned_keys from config if Cleanup wasn't explicitly set via command line + if deleteUnmentionedKeys && !cmd.Cleanup { + cmd.Cleanup = deleteUnmentionedKeys + } + if err := sources.Validate(); err != nil { return err } @@ -154,23 +155,21 @@ func (cmd *PushCommand) Run() error { if err != nil { return err } - if cmd.Wait && cmd.Cleanup { - // collect all upload ids for cleanup by project and branch - found := false - for _, result := range pushResults { - if result.ProjectID == pushResult.ProjectID && result.Branch == pushResult.Branch { - result.UploadIDs = append(result.UploadIDs, pushResult.UploadIDs...) - found = true - break - } - } - if !found { - pushResults = append(pushResults, pushResult) + // collect all upload ids by project and branch for batch creation (cleanup depends on cmd.Cleanup) + found := false + for _, result := range pushResults { + if result.ProjectID == pushResult.ProjectID && result.Branch == pushResult.Branch { + result.UploadIDs = append(result.UploadIDs, pushResult.UploadIDs...) + found = true + break } } + if !found { + pushResults = append(pushResults, pushResult) + } } for _, pushResult := range pushResults { - UploadCleanup(client, true, pushResult.UploadIDs, pushResult.Branch, pushResult.ProjectID) + CreateUploadBatch(client, true, pushResult.UploadIDs, pushResult.Branch, pushResult.ProjectID, cmd.Cleanup) } return nil diff --git a/clients/cli/cmd/internal/push_source.go b/clients/cli/cmd/internal/push_source.go index 57e6d1a3..401c5334 100644 --- a/clients/cli/cmd/internal/push_source.go +++ b/clients/cli/cmd/internal/push_source.go @@ -13,26 +13,27 @@ import ( "github.com/spf13/viper" ) -func SourcesFromConfig(config phrase.Config) (Sources, error) { - if config.Sources == nil || len(config.Sources) == 0 { - return nil, fmt.Errorf("no sources for upload specified") +func SourcesFromConfig(config phrase.Config) (Sources, bool, error) { + if config.Push == nil || len(config.Push) == 0 { + return nil, false, fmt.Errorf("no sources for upload specified") } tmp := struct { - Sources Sources + DeleteUnmentionedKeys bool `json:"delete_unmentioned_keys,omitempty"` + Sources Sources `json:"sources,omitempty"` }{} - sources := viper.New() - sources.SetConfigType("yaml") - err := sources.ReadConfig(bytes.NewReader(config.Sources)) + pushSection := viper.New() + pushSection.SetConfigType("yaml") + err := pushSection.ReadConfig(bytes.NewReader(config.Push)) if err != nil { - return nil, err + return nil, false, err } - err = sources.UnmarshalExact(&tmp, ViperStructTag()) + err = pushSection.UnmarshalExact(&tmp, ViperStructTag()) if err != nil { - return nil, err + return nil, false, err } srcs := tmp.Sources @@ -64,10 +65,10 @@ func SourcesFromConfig(config phrase.Config) (Sources, error) { } if len(validSources) <= 0 { - return nil, fmt.Errorf("no sources could be identified! Refine the sources list in your config") + return nil, false, fmt.Errorf("no sources could be identified! Refine the sources list in your config") } - return validSources, nil + return validSources, tmp.DeleteUnmentionedKeys, nil } type Sources []*Source diff --git a/clients/cli/cmd/internal/shared.go b/clients/cli/cmd/internal/shared.go index a3ddd061..37a597bc 100644 --- a/clients/cli/cmd/internal/shared.go +++ b/clients/cli/cmd/internal/shared.go @@ -210,49 +210,50 @@ func StringToInterface() mapstructure.DecodeHookFunc { } } -func UploadCleanup(client *phrase.APIClient, confirm bool, ids []string, branch string, projectId string) error { - if !shared.BatchMode { - fmt.Println("Keys not mentioned in the following uploads will be deleted:") - fmt.Println(strings.Join(ids, "\n")) - } - if !confirm { - if shared.BatchMode { - return errors.New("Can't ask for confirmation in batch mode. Aborting") - } - confirmation := "" - err := prompt.WithDefault("Are you sure you want to continue? (y/n)", &confirmation, "n") - if err != nil { - return err +func CreateUploadBatch(client *phrase.APIClient, confirm bool, ids []string, branch string, projectId string, cleanup bool) error { + if cleanup { + if !shared.BatchMode { + fmt.Println("Keys not mentioned in the following uploads will be deleted:") + fmt.Println(strings.Join(ids, "\n")) } + if !confirm { + if shared.BatchMode { + return errors.New("Can't ask for confirmation in batch mode. Aborting") + } + confirmation := "" + err := prompt.WithDefault("Are you sure you want to continue? (y/n)", &confirmation, "n") + if err != nil { + return err + } - if strings.ToLower(confirmation) != "y" { - fmt.Println("Clean up aborted") - return nil + if strings.ToLower(confirmation) != "y" { + fmt.Println("Clean up aborted") + return nil + } } } - q := "unmentioned_in_upload:" + strings.Join(ids, ",") - optionalBranch := optional.String{} - if branch != "" { - optionalBranch = optional.NewString(branch) - } - keysDeletelocalVarOptionals := phrase.KeysDeleteCollectionOpts{ - Q: optional.NewString(q), - Branch: optionalBranch, + uploadBatchesCreateParameters := phrase.UploadBatchesCreateParameters{ + Branch: branch, + DeleteUnmentionedKeys: &cleanup, + UploadIds: ids, } - affected, _, err := client.KeysApi.KeysDeleteCollection(Auth, projectId, &keysDeletelocalVarOptionals) - + uploadBatch, _, err := client.UploadBatchesApi.UploadBatchesCreate(Auth, projectId, uploadBatchesCreateParameters, nil) if err != nil { return err } + if !cleanup { + return nil + } + if shared.BatchMode { - jsonBuf, jsonErr := json.MarshalIndent(affected, "", " ") + jsonBuf, jsonErr := json.MarshalIndent(uploadBatch, "", " ") if jsonErr != nil { print.Error(jsonErr) } fmt.Printf("%s\n", string(jsonBuf)) } else { - print.Success("%d key(s) successfully deleted.\n", affected.RecordsAffected) + print.Success("Keys cleanup scheduled for an upload batch with %d uploads", len(ids)) } return nil } diff --git a/clients/cli/cmd/internal/upload_cleanup.go b/clients/cli/cmd/internal/upload_cleanup.go index c95b9d0e..ac6c560b 100644 --- a/clients/cli/cmd/internal/upload_cleanup.go +++ b/clients/cli/cmd/internal/upload_cleanup.go @@ -16,5 +16,5 @@ func (cmd *UploadCleanupCommand) Run() error { Config = &cmd.Config client := newClient() - return UploadCleanup(client, cmd.Confirm, cmd.IDs, cmd.Branch, cmd.ProjectID) + return CreateUploadBatch(client, cmd.Confirm, cmd.IDs, cmd.Branch, cmd.ProjectID, true) } diff --git a/clients/cli/go.sum b/clients/cli/go.sum index dfc1b598..e4552cef 100644 --- a/clients/cli/go.sum +++ b/clients/cli/go.sum @@ -215,8 +215,10 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/phrase/phrase-go/v4 v4.18.0 h1:LSD1izhuwF1TzxlLKdoItfyeSbCuiSs5RCmNiv8vmLg= -github.com/phrase/phrase-go/v4 v4.18.0/go.mod h1:4XplKvrbHS2LDaXfFp9xrVDtO5xk2WHFm0htutwwd8c= +github.com/phrase/phrase-go/v4 v4.18.1 h1:y1sv4z8ufEQB+kJA8ymSiH8nRAvH8gGoVSB5/7jvYEQ= +github.com/phrase/phrase-go/v4 v4.18.1/go.mod h1:4XplKvrbHS2LDaXfFp9xrVDtO5xk2WHFm0htutwwd8c= +github.com/phrase/phrase-go/v4 v4.19.0 h1:tNliCxO/0SMu2viLE9idzADBUoY9C6CqrDmp3ntgpQI= +github.com/phrase/phrase-go/v4 v4.19.0/go.mod h1:4XplKvrbHS2LDaXfFp9xrVDtO5xk2WHFm0htutwwd8c= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=