Skip to content

Commit 35f836f

Browse files
committed
add support for patterns from config file
1 parent 9393188 commit 35f836f

File tree

7 files changed

+196
-77
lines changed

7 files changed

+196
-77
lines changed

compile_format_option.go renamed to compile_format.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@ import (
88
"github.com/reconquest/hierr-go"
99
)
1010

11-
func CompileFormatOption(
12-
args map[string]interface{},
13-
) (*Format, error) {
14-
value := args["--format"].(string)
11+
var (
12+
// because we can have specific formats for different file types defined
13+
// in config file, we need to cache templates to prevent overhead in
14+
// runtime
15+
compiledFormatsCache = map[string]*Format{}
16+
)
17+
18+
func compileFormat(definition string) (*Format, error) {
19+
if format, ok := compiledFormatsCache[definition]; ok {
20+
return format, nil
21+
}
1522

16-
value = strings.NewReplacer(`\n`, "\n", `\t`, "\t").Replace(value)
23+
definition = strings.NewReplacer(
24+
`\n`, "\n",
25+
`\t`, "\t",
26+
).Replace(definition)
1727

1828
funcs := template.FuncMap{
1929
"name": func(path string) string {
@@ -30,18 +40,22 @@ func CompileFormatOption(
3040
err error
3141
)
3242

33-
format.Template, err = template.New("format").Funcs(funcs).Parse(value)
43+
format.Template, err = template.New("format").Funcs(funcs).Parse(
44+
definition,
45+
)
3446
if err != nil {
3547
return nil, NewError(
3648
hierr.Errorf(
3749
err,
38-
"failed to compile template for --format option",
50+
"failed to compile format template",
3951
),
4052

4153
"Check template syntax accordingly to text/template "+
4254
"documentation:\n\thttps://golang.org/pkg/text/template/",
4355
)
4456
}
4557

58+
compiledFormatsCache[definition] = &format
59+
4660
return &format, nil
4761
}

config.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,33 @@ package main
33
import (
44
"os"
55

6+
"github.com/gobwas/glob"
7+
"github.com/imdario/mergo"
68
"github.com/kovetskiy/ko"
9+
hierr "github.com/reconquest/hierr-go"
710
yaml "gopkg.in/yaml.v2"
811
)
912

13+
type FileConfig struct {
14+
Pull struct {
15+
Format string
16+
}
17+
18+
Push struct {
19+
Type string
20+
Directives map[string]string
21+
}
22+
}
23+
1024
type Config struct {
1125
UserID string `yaml:"user_id",required:"true"`
1226
Secret string `yaml:"secret",required:"true"`
1327
AccountID string `yaml:"account_id"`
1428
ProjectID string `yaml:"project_id"`
1529
Threads int `yaml:"threads"`
1630

31+
Files map[string]FileConfig `yaml:"files"`
32+
1733
path string
1834
}
1935

@@ -33,3 +49,43 @@ func NewConfig(path string) (Config, error) {
3349

3450
return config, nil
3551
}
52+
53+
func (config *Config) GetFileConfig(path string) (FileConfig, error) {
54+
var match *FileConfig
55+
56+
for key, candidate := range config.Files {
57+
pattern, err := glob.Compile(key, '/')
58+
if err != nil {
59+
return FileConfig{}, NewError(
60+
hierr.Errorf(
61+
err,
62+
`unable to compile pattern from config file (key "%s")`,
63+
key,
64+
),
65+
66+
`File match pattern is malformed. Check out help for more `+
67+
`information on globbing patterns.`,
68+
)
69+
}
70+
71+
if pattern.Match(path) {
72+
match = &candidate
73+
}
74+
}
75+
76+
defaults := config.Files["default"]
77+
78+
if match == nil {
79+
return defaults, nil
80+
}
81+
82+
err := mergo.Merge(match, defaults)
83+
if err != nil {
84+
return FileConfig{}, NewError(
85+
hierr.Errorf(err, "unable to merge file config options"),
86+
`It's internal error. Consider reporting bug.`,
87+
)
88+
}
89+
90+
return *match, nil
91+
}

do_files_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func doFilesList(
2424
args["--format"] = defaultFilesListFormat
2525
}
2626

27-
format, err := CompileFormatOption(args)
27+
format, err := compileFormat(args["--format"].(string))
2828
if err != nil {
2929
return err
3030
}

do_files_pull.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,14 @@ func doFilesPull(
1010
args map[string]interface{},
1111
) error {
1212
var (
13-
directory = args["--directory"].(string)
14-
project = args["--project"].(string)
15-
uri, _ = args["<uri>"].(string)
16-
source = args["--source"].(bool)
13+
project = args["--project"].(string)
14+
uri, _ = args["<uri>"].(string)
1715
)
1816

1917
if args["--format"] == nil {
2018
args["--format"] = defaultFilePullFormat
2119
}
2220

23-
format, err := CompileFormatOption(args)
24-
if err != nil {
25-
return err
26-
}
27-
2821
files, err := globFiles(client, project, uri)
2922
if err != nil {
3023
return err
@@ -36,14 +29,7 @@ func doFilesPull(
3629
// func closure required to pass different file objects to goroutines
3730
func(file smartling.File) {
3831
pool.Do(func() {
39-
err := downloadFileTranslations(
40-
client,
41-
project,
42-
file,
43-
format,
44-
directory,
45-
source,
46-
)
32+
err := downloadFileTranslations(client, config, args, file)
4733

4834
if err != nil {
4935
logger.Error(err)

do_files_status.go

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ func doFilesStatus(
1818
project = args["--project"].(string)
1919
uri, _ = args["<uri>"].(string)
2020
directory = args["--directory"].(string)
21+
22+
defaultFormat, _ = args["--format"].(string)
2123
)
2224

23-
if args["--format"] == nil {
24-
args["--format"] = defaultFileStatusFormat
25+
if defaultFormat == "" {
26+
defaultFormat = defaultFileStatusFormat
2527
}
2628

27-
format, err := CompileFormatOption(args)
29+
info, err := client.GetProjectDetails(project)
2830
if err != nil {
2931
return err
3032
}
@@ -34,68 +36,73 @@ func doFilesStatus(
3436
return err
3537
}
3638

37-
var (
38-
table = NewTableWriter(os.Stdout)
39-
)
39+
var table = NewTableWriter(os.Stdout)
4040

4141
for _, file := range files {
42-
path, err := format.Execute(map[string]interface{}{
43-
"FileURI": file.FileURI,
44-
})
45-
if err != nil {
46-
return err
47-
}
48-
4942
status, err := client.GetFileStatus(project, file.FileURI)
5043
if err != nil {
5144
return err
5245
}
5346

54-
path = filepath.Join(directory, path)
55-
56-
state := "source"
57-
if !isFileExists(path) {
58-
state = "missing"
59-
}
60-
61-
writeFileStatus(table, map[string]string{
62-
"Path": path,
63-
"State": state,
64-
"Progress": "source",
65-
"Strings": fmt.Sprint(status.TotalStringCount),
66-
"Words": fmt.Sprint(status.TotalWordCount),
67-
})
68-
69-
for _, locale := range status.Items {
70-
path, err := format.Execute(map[string]interface{}{
71-
"FileURI": file.FileURI,
72-
"Locale": locale.LocaleID,
73-
})
47+
translations := status.Items
48+
49+
translations = append(
50+
[]smartling.FileStatusTranslation{
51+
{
52+
CompletedStringCount: status.TotalStringCount,
53+
CompletedWordCount: status.TotalWordCount,
54+
},
55+
},
56+
translations...,
57+
)
58+
59+
for _, translation := range translations {
60+
path, err := executeFileFormat(
61+
config,
62+
file,
63+
defaultFormat,
64+
usePullFormat,
65+
map[string]interface{}{
66+
"FileURI": file.FileURI,
67+
"Locale": translation.LocaleID,
68+
},
69+
)
7470
if err != nil {
7571
return err
7672
}
7773

7874
path = filepath.Join(directory, path)
7975

80-
state := "remote"
81-
if !isFileExists(path) {
82-
state = "missing"
83-
}
76+
var (
77+
locale = info.SourceLocaleID
78+
state = "source"
79+
progress = "source"
80+
)
8481

85-
writeFileStatus(table, map[string]string{
86-
"Path": path,
87-
"Locale": locale.LocaleID,
88-
"State": state,
89-
"Progress": fmt.Sprintf(
82+
if translation.LocaleID != "" {
83+
locale = translation.LocaleID
84+
state = "remote"
85+
progress = fmt.Sprintf(
9086
"%d%%",
9187
int(
9288
100*
93-
float64(locale.CompletedStringCount)/
89+
float64(translation.CompletedStringCount)/
9490
float64(status.TotalStringCount),
9591
),
96-
),
97-
"Strings": fmt.Sprint(locale.CompletedStringCount),
98-
"Words": fmt.Sprint(locale.CompletedWordCount),
92+
)
93+
}
94+
95+
if !isFileExists(path) {
96+
state = "missing"
97+
}
98+
99+
writeFileStatus(table, map[string]string{
100+
"Path": path,
101+
"Locale": locale,
102+
"State": state,
103+
"Progress": progress,
104+
"Strings": fmt.Sprint(translation.CompletedStringCount),
105+
"Words": fmt.Sprint(translation.CompletedWordCount),
99106
})
100107
}
101108
}

download_file_translations.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ import (
1010

1111
func downloadFileTranslations(
1212
client *smartling.Client,
13-
project string,
13+
config Config,
14+
args map[string]interface{},
1415
file smartling.File,
15-
format *Format,
16-
directory string,
17-
original bool,
1816
) error {
17+
var (
18+
project = args["--project"].(string)
19+
directory = args["--directory"].(string)
20+
source = args["--source"].(bool)
21+
22+
defaultFormat, _ = args["--format"].(string)
23+
)
24+
25+
if defaultFormat == "" {
26+
defaultFormat = defaultFileStatusFormat
27+
}
28+
1929
status, err := client.GetFileStatus(project, file.FileURI)
2030
if err != nil {
2131
return hierr.Errorf(
@@ -28,14 +38,18 @@ func downloadFileTranslations(
2838

2939
translations := status.Items
3040

31-
if original {
41+
if source {
3242
translations = append(translations, smartling.FileStatusTranslation{
3343
LocaleID: "",
3444
})
3545
}
3646

3747
for _, locale := range translations {
38-
path, err := format.Execute(
48+
path, err := executeFileFormat(
49+
config,
50+
file,
51+
defaultFormat,
52+
usePullFormat,
3953
map[string]interface{}{
4054
"FileURI": file.FileURI,
4155
"Locale": locale.LocaleID,

0 commit comments

Comments
 (0)