diff --git a/clients/cli/cmd/internal/pull.go b/clients/cli/cmd/internal/pull.go index 4b8c5558..0e5d4f2c 100644 --- a/clients/cli/cmd/internal/pull.go +++ b/clients/cli/cmd/internal/pull.go @@ -58,13 +58,17 @@ func (cmd *PullCommand) Run(config *phrase.Config) error { } cmd.Branch = branchName - projectIdToLocales, err := LocalesForProjects(client, targets, cmd.Branch) + localesCache, err := GetLocalesCache(client, targets, cmd.Branch) if err != nil { return err } for _, target := range targets { - val, ok := projectIdToLocales[LocaleCacheKey{target.ProjectID, cmd.Branch}] + if cmd.Branch != "" { + target.Params.Branch = optional.NewString(cmd.Branch) + } + + val, ok := localesCache[LocalesCacheKey{target.ProjectID, target.Params.Branch.Value()}] if !ok || len(val) == 0 { if cmd.Branch != "" { continue @@ -75,7 +79,7 @@ func (cmd *PullCommand) Run(config *phrase.Config) error { } for _, target := range targets { - err := target.Pull(client, cmd.Branch, cmd.Async) + err := target.Pull(client, cmd.Async) if err != nil { return err } @@ -103,7 +107,7 @@ type PullParams struct { LocaleID string `json:"locale_id"` } -func (target *Target) Pull(client *phrase.APIClient, branch string, async bool) error { +func (target *Target) Pull(client *phrase.APIClient, async bool) error { if err := target.CheckPreconditions(); err != nil { return err } @@ -124,7 +128,7 @@ func (target *Target) Pull(client *phrase.APIClient, branch string, async bool) return err } - err = target.DownloadAndWriteToFile(client, localeFile, branch, async) + err = target.DownloadAndWriteToFile(client, localeFile, async) if err != nil { if openapiError, ok := err.(phrase.GenericOpenAPIError); ok { print.Warn("API response: %s", openapiError.Body()) @@ -139,7 +143,7 @@ func (target *Target) Pull(client *phrase.APIClient, branch string, async bool) return nil } -func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFile *LocaleFile, branch string, async bool) error { +func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFile *LocaleFile, async bool) error { localVarOptionals := phrase.LocaleDownloadOpts{} if target.Params != nil { @@ -155,10 +159,6 @@ func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFil localVarOptionals.FileFormat = optional.NewString(localeFile.FileFormat) } - if branch != "" { - localVarOptionals.Branch = optional.NewString(branch) - } - if localeFile.Tag != "" { localVarOptionals.Tags = optional.NewString(localeFile.Tag) localVarOptionals.Tag = optional.EmptyString() diff --git a/clients/cli/cmd/internal/pull_target.go b/clients/cli/cmd/internal/pull_target.go index 68914ee6..7b68c7f7 100644 --- a/clients/cli/cmd/internal/pull_target.go +++ b/clients/cli/cmd/internal/pull_target.go @@ -15,12 +15,12 @@ import ( type Targets []*Target -func (targets Targets) ProjectIds() []string { - projectIds := []string{} +func (targets Targets) GetAllLocalesCacheKeys() []LocalesCacheKey { + localesCacheKeys := []LocalesCacheKey{} for _, target := range targets { - projectIds = append(projectIds, target.ProjectID) + localesCacheKeys = append(localesCacheKeys, LocalesCacheKey{target.ProjectID, target.Params.Branch.Value()}) } - return projectIds + return localesCacheKeys } type Target struct { diff --git a/clients/cli/cmd/internal/push.go b/clients/cli/cmd/internal/push.go index 23fdaa03..78e1e81c 100644 --- a/clients/cli/cmd/internal/push.go +++ b/clients/cli/cmd/internal/push.go @@ -130,12 +130,12 @@ func (cmd *PushCommand) Run() error { } } - projectIdToLocales, err := LocalesForProjects(client, sources, cmd.Branch) + localesCache, err := GetLocalesCache(client, sources, cmd.Branch) if err != nil { return err } for _, source := range sources { - val, ok := projectIdToLocales[LocaleCacheKey{source.ProjectID, cmd.Branch}] + val, ok := localesCache[LocalesCacheKey{source.ProjectID, cmd.Branch}] if ok { source.RemoteLocales = val } diff --git a/clients/cli/cmd/internal/push_source.go b/clients/cli/cmd/internal/push_source.go index e5265044..ee7a60e2 100644 --- a/clients/cli/cmd/internal/push_source.go +++ b/clients/cli/cmd/internal/push_source.go @@ -148,12 +148,12 @@ func (source *Source) CheckPreconditions() error { return nil } -func (sources Sources) ProjectIds() []string { - projectIds := []string{} +func (sources Sources) GetAllLocalesCacheKeys() []LocalesCacheKey { + projectIdsBranches := []LocalesCacheKey{} for _, source := range sources { - projectIds = append(projectIds, source.ProjectID) + projectIdsBranches = append(projectIdsBranches, LocalesCacheKey{source.ProjectID, source.Branch}) } - return projectIds + return projectIdsBranches } func (source *Source) uploadFile(client *phrase.APIClient, localeFile *LocaleFile, branch string, tag string) (*phrase.Upload, error) { if Debug { diff --git a/clients/cli/cmd/internal/shared.go b/clients/cli/cmd/internal/shared.go index 6ab5b74f..4a74c6b0 100644 --- a/clients/cli/cmd/internal/shared.go +++ b/clients/cli/cmd/internal/shared.go @@ -20,31 +20,37 @@ import ( var Debug bool -type ProjectLocales interface { - ProjectIds() []string +type SourcesOrTargets interface { + // returns a list of LocalesCacheKeys (ProjectId, Branch) for all targets + GetAllLocalesCacheKeys() []LocalesCacheKey } -type LocaleCacheKey struct { +type LocalesCacheKey struct { ProjectID string Branch string } -type LocaleCache map[LocaleCacheKey][]*phrase.Locale +type LocaleCache map[LocalesCacheKey][]*phrase.Locale -func LocalesForProjects(client *phrase.APIClient, projectLocales ProjectLocales, branch string) (LocaleCache, error) { - projectIdToLocales := LocaleCache{} +// for every source or target, retrieves and caches the list of locales +func GetLocalesCache(client *phrase.APIClient, sourcesOrTargets SourcesOrTargets, branch string) (LocaleCache, error) { + localesCache := LocaleCache{} - for _, pid := range projectLocales.ProjectIds() { - key := LocaleCacheKey{ - ProjectID: pid, - Branch: branch, + for _, localesCacheKey := range sourcesOrTargets.GetAllLocalesCacheKeys() { + branchToUse := localesCacheKey.Branch + if branch != "" { + branchToUse = branch + } + key := LocalesCacheKey{ + ProjectID: localesCacheKey.ProjectID, + Branch: branchToUse, } - if _, ok := projectIdToLocales[key]; !ok { + if _, ok := localesCache[key]; !ok { remoteLocales, http_response, err := RemoteLocales(client, key) if err != nil { - if http_response != nil && http_response.StatusCode == 404 && branch != "" { + if http_response != nil && http_response.StatusCode == 404 && branchToUse != "" { // skip this key if we targeted a branch in // a project which does not exist continue @@ -52,13 +58,13 @@ func LocalesForProjects(client *phrase.APIClient, projectLocales ProjectLocales, return nil, err } - projectIdToLocales[key] = remoteLocales + localesCache[key] = remoteLocales } } - return projectIdToLocales, nil + return localesCache, nil } -func RemoteLocales(client *phrase.APIClient, key LocaleCacheKey) ([]*phrase.Locale, *phrase.APIResponse, error) { +func RemoteLocales(client *phrase.APIClient, key LocalesCacheKey) ([]*phrase.Locale, *phrase.APIResponse, error) { page := 1 localVarOptionals := phrase.LocalesListOpts{