forked from 99designs/smartling
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdo_files_import.go
86 lines (71 loc) · 1.67 KB
/
do_files_import.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/Smartling/api-sdk-go"
"github.com/reconquest/hierr-go"
)
func doFilesImport(
client *smartling.Client,
config Config,
args map[string]interface{},
) error {
var (
project = config.ProjectID
uri = args["<uri>"].(string)
file = args["<file>"].(string)
locale = args["<locale>"].(string)
fileType, _ = args["--type"].(string)
)
contents, err := ioutil.ReadFile(file)
if err != nil {
return NewError(
hierr.Errorf(err, "unable to read file for import"),
"Check that specified file exists and you have permissions "+
"to read it.",
)
}
request := smartling.ImportRequest{}
request.File = contents
request.FileURI = uri
request.TranslationState = smartling.TranslationStatePublished
if args["--post-translation"].(bool) {
request.TranslationState = smartling.TranslationStatePostTranslation
}
if args["--overwrite"].(bool) {
request.Overwrite = true
}
if fileType != "" {
request.FileType = smartling.FileType(fileType)
} else {
request.FileType = smartling.GetFileTypeByExtension(
filepath.Ext(file),
)
if request.FileType == smartling.FileTypeUnknown {
return NewError(
fmt.Errorf(
"unable to deduce file type from extension: %q",
filepath.Ext(file),
),
`You need to specify file type via --type option.`,
)
}
}
result, err := client.Import(project, locale, request)
if err != nil {
return hierr.Errorf(
err,
`unable to import file "%s" (original "%s")`,
file,
uri,
)
}
fmt.Printf(
"%s imported [%d strings %d words]\n",
file,
result.StringCount,
result.WordCount,
)
return nil
}