Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clients/cli/cmd/internal/pull_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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
Expand Down
35 changes: 17 additions & 18 deletions clients/cli/cmd/internal/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
25 changes: 13 additions & 12 deletions clients/cli/cmd/internal/push_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
57 changes: 29 additions & 28 deletions clients/cli/cmd/internal/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion clients/cli/cmd/internal/upload_cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 4 additions & 2 deletions clients/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Loading