Skip to content

Commit

Permalink
Fix placeholders in file path
Browse files Browse the repository at this point in the history
  • Loading branch information
theSoenke committed Oct 2, 2024
1 parent 580589e commit f0a0ef4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
21 changes: 21 additions & 0 deletions clients/cli/cmd/internal/placeholders/placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package placeholders

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/antihax/optional"
"github.com/phrase/phrase-cli/cmd/internal/stringz"
)

Expand Down Expand Up @@ -92,3 +94,22 @@ func Resolve(s, pattern string) (map[string]string, error) {

return values, nil
}

func ResolveTranslationKeyPrefix(translationKeyPrefix optional.String, filePath string) (optional.String, error) {
if strings.Contains(translationKeyPrefix.Value(), "<file_path>") {
currentDir, err := os.Getwd()
if err != nil {
return optional.EmptyString(), err
}

filePath, err := filepath.Rel(currentDir, filePath)
if err != nil {
return optional.EmptyString(), err
}

resolvedKeyPrefix := strings.Replace(translationKeyPrefix.Value(), "<file_path>", filePath, 1)
return optional.NewString(resolvedKeyPrefix), nil
}

return translationKeyPrefix, nil
}
6 changes: 6 additions & 0 deletions clients/cli/cmd/internal/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFil

if target.Params != nil {
localVarOptionals = target.Params.LocaleDownloadOpts
translationKeyPrefix, err := placeholders.ResolveTranslationKeyPrefix(target.Params.TranslationKeyPrefix, localeFile.Path)
if err != nil {
return err
}
localVarOptionals.TranslationKeyPrefix = translationKeyPrefix
}

if localVarOptionals.FileFormat.Value() == "" {
Expand All @@ -170,6 +175,7 @@ func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFil
debugFprintln("Tags", localVarOptionals.Tags)
debugFprintln("Branch", localVarOptionals.Branch)
debugFprintln("FormatOptions", localVarOptionals.FormatOptions)
debugFprintln("TranslationKeyPrefix", localVarOptionals.TranslationKeyPrefix)

if async {
return target.downloadAsynchronously(client, localeFile, localVarOptionals)
Expand Down
4 changes: 0 additions & 4 deletions clients/cli/cmd/internal/pull_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"
"strings"

"github.com/antihax/optional"
"github.com/phrase/phrase-cli/cmd/internal/paths"
"github.com/phrase/phrase-cli/cmd/internal/placeholders"
"github.com/phrase/phrase-cli/cmd/internal/shared"
Expand Down Expand Up @@ -191,9 +190,6 @@ func TargetsFromConfig(config phrase.Config) (Targets, error) {
if target.FileFormat == "" {
target.FileFormat = fileFormat
}
if target.Params.TranslationKeyPrefix.Value() == "<file_path>" {
target.Params.TranslationKeyPrefix = optional.NewString(target.File)
}
validTargets = append(validTargets, target)
}

Expand Down
10 changes: 7 additions & 3 deletions clients/cli/cmd/internal/push_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/antihax/optional"
"github.com/phrase/phrase-cli/cmd/internal/paths"
"github.com/phrase/phrase-cli/cmd/internal/placeholders"
"github.com/phrase/phrase-go/v3"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -50,9 +51,6 @@ func SourcesFromConfig(config phrase.Config) (Sources, error) {
if source.Params == nil {
source.Params = new(UploadParams)
}
if source.Params.TranslationKeyPrefix.Value() == "<file_path>" {
source.Params.TranslationKeyPrefix = optional.NewString(source.File)
}

if !source.Params.FileFormat.IsSet() {
switch {
Expand Down Expand Up @@ -202,6 +200,12 @@ func (source *Source) uploadFile(client *phrase.APIClient, localeFile *LocaleFil
params.Branch = optional.NewString(branch)
}

translationKeyPrefix, err := placeholders.ResolveTranslationKeyPrefix(params.UploadCreateOpts.TranslationKeyPrefix, localeFile.Path)
if err != nil {
return nil, err
}
params.UploadCreateOpts.TranslationKeyPrefix = translationKeyPrefix

upload, _, err := client.UploadsApi.UploadCreate(Auth, source.ProjectID, file, params.FileFormat.Value(), params.LocaleId.Value(), &params.UploadCreateOpts)

return &upload, err
Expand Down
4 changes: 2 additions & 2 deletions clients/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
github.com/phrase/phrase-go/v3 v3.3.0 h1:kq2eFgKE6mUUZpud1KWsTa1RO4T+ztB2JI3DsCfqHog=
github.com/phrase/phrase-go/v3 v3.3.0/go.mod h1:s0uOYiXLxKAYlaIS6TbKv3efkKFUlY4OB6OL+VgvK90=
github.com/phrase/phrase-go/v3 v3.5.0 h1:zviffIun5A10PNnnSV0ynHjs+VRY7fc9xXVtNOjkm3o=
github.com/phrase/phrase-go/v3 v3.5.0/go.mod h1:s0uOYiXLxKAYlaIS6TbKv3efkKFUlY4OB6OL+VgvK90=
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/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
Expand Down

0 comments on commit f0a0ef4

Please sign in to comment.