Skip to content

Commit 1ccdf51

Browse files
authored
Override file type (#87)
Override file type
1 parent d89ae0f commit 1ccdf51

File tree

6 files changed

+56
-24
lines changed

6 files changed

+56
-24
lines changed

cmd/cmd_root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewRootCmd() *cobra.Command {
3030
rootCmd := &cobra.Command{
3131
Use: "smartling-cli",
3232
Short: "Manage translation files using Smartling CLI.",
33-
Version: "2.4",
33+
Version: "2.4.1",
3434
Long: `Manage translation files using Smartling CLI.
3535
Complete documentation is available at https://www.smartling.com`,
3636
PersistentPreRun: func(cmd *cobra.Command, _ []string) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.24.3
44

55
require (
66
dario.cat/mergo v1.0.2
7-
github.com/Smartling/api-sdk-go v0.0.0-20250818153034-c191bf364793
7+
github.com/Smartling/api-sdk-go v0.0.0-20250825152248-be95181892e1
88
github.com/charmbracelet/bubbles v0.21.0
99
github.com/charmbracelet/bubbletea v1.3.6
1010
github.com/charmbracelet/lipgloss v1.1.0

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0
44
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
55
github.com/Smartling/api-sdk-go v0.0.0-20250818153034-c191bf364793 h1:Mr1XVsyxqYLWExfTicQs7wfOzdkA6eC3XnY+xZvxoXo=
66
github.com/Smartling/api-sdk-go v0.0.0-20250818153034-c191bf364793/go.mod h1:BSUxirtjHu/mZmb41K2sKo8B8C2tfRP3NMCCV6yP27k=
7+
github.com/Smartling/api-sdk-go v0.0.0-20250825104541-4721a8490a18 h1:MGjmKQubHwOVLv4/6jtAkJJ3ppPrakwK9i1+HM2pDUk=
8+
github.com/Smartling/api-sdk-go v0.0.0-20250825104541-4721a8490a18/go.mod h1:BSUxirtjHu/mZmb41K2sKo8B8C2tfRP3NMCCV6yP27k=
9+
github.com/Smartling/api-sdk-go v0.0.0-20250825105330-2951221b8ec9 h1:9Zo9/msRdhz/B6AKwsjjWVWMar/uCx1xtWdlW2Thaa0=
10+
github.com/Smartling/api-sdk-go v0.0.0-20250825105330-2951221b8ec9/go.mod h1:BSUxirtjHu/mZmb41K2sKo8B8C2tfRP3NMCCV6yP27k=
11+
github.com/Smartling/api-sdk-go v0.0.0-20250825151401-cef26d6a5b31 h1:KE43nC/f/K/yVU7qWSp3t2Kl+WeEBNqkJ8toohXmnyQ=
12+
github.com/Smartling/api-sdk-go v0.0.0-20250825151401-cef26d6a5b31/go.mod h1:BSUxirtjHu/mZmb41K2sKo8B8C2tfRP3NMCCV6yP27k=
13+
github.com/Smartling/api-sdk-go v0.0.0-20250825152248-be95181892e1 h1:uO74ub5cz0VQzu1uZblMsyimIiYwIB5ynU4KwRsu1Xo=
14+
github.com/Smartling/api-sdk-go v0.0.0-20250825152248-be95181892e1/go.mod h1:BSUxirtjHu/mZmb41K2sKo8B8C2tfRP3NMCCV6yP27k=
715
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
816
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
917
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=

services/files/run_push.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/Smartling/smartling-cli/services/helpers/rlog"
1818

1919
api "github.com/Smartling/api-sdk-go/api/batches"
20+
smfile "github.com/Smartling/api-sdk-go/helpers/sm_file"
2021
"github.com/reconquest/hierr-go"
2122
)
2223

@@ -211,9 +212,20 @@ Check that file exists and readable by current user.`,
211212
},
212213
}
213214
}
214-
fileType, found := api.TypeByExt[filepath.Ext(file)]
215+
var fileType api.Type
216+
var found bool
217+
if params.FileType != "" {
218+
fileType, found = smfile.ParseType(api.FirstType, api.LastType, params.FileType)
219+
if !found {
220+
rlog.Debugf("unknown override file type: %s", params.FileType)
221+
}
222+
}
215223
if !found {
216-
rlog.Debugf("unknown file type: %s", file)
224+
var found bool
225+
fileType, found = api.TypeByExt[filepath.Ext(file)]
226+
if !found {
227+
rlog.Debugf("unknown file type for file: %s", file)
228+
}
217229
}
218230
locales := params.Locales
219231
if len(locales) == 0 {

services/mt/run_translate.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/Smartling/smartling-cli/services/helpers/rlog"
1717

1818
api "github.com/Smartling/api-sdk-go/api/mt"
19+
smfile "github.com/Smartling/api-sdk-go/helpers/sm_file"
1920
)
2021

2122
// TranslateParams is the parameters for the RunTranslate method.
@@ -30,27 +31,38 @@ type TranslateParams struct {
3031
AccountUID api.AccountUID
3132
}
3233

33-
func (s service) RunTranslate(ctx context.Context, p TranslateParams, files []string, updates chan any) ([]TranslateOutput, error) {
34+
func (s service) RunTranslate(ctx context.Context, params TranslateParams, files []string, updates chan any) ([]TranslateOutput, error) {
3435
var res []TranslateOutput
3536

3637
for fileID, file := range files {
3738
rlog.Debugf("Running translate for file %s", file)
38-
contents, err := getContent(p.InputDirectory, file)
39+
contents, err := getContent(params.InputDirectory, file)
3940
if err != nil {
4041
return nil, err
4142
}
42-
fileType, found := api.TypeByExt[filepath.Ext(file)]
43+
var fileType api.Type
44+
var found bool
45+
if params.OverrideFileType != "" {
46+
fileType, found = smfile.ParseType(api.FirstType, api.LastType, params.OverrideFileType)
47+
if !found {
48+
rlog.Debugf("unknown override file type: %s", params.OverrideFileType)
49+
}
50+
}
4351
if !found {
44-
rlog.Debugf("unknown file type: %s", file)
52+
var found bool
53+
fileType, found = api.TypeByExt[filepath.Ext(file)]
54+
if !found {
55+
rlog.Debugf("unknown file type for file: %s", file)
56+
}
4557
}
4658
request := api.UploadFileRequest{
4759
File: contents,
48-
LocalesToAuthorize: []string{p.SourceLocale},
60+
LocalesToAuthorize: []string{params.SourceLocale},
4961
FileType: fileType,
50-
Directives: p.Directives,
62+
Directives: params.Directives,
5163
}
5264
rlog.Debugf("start upload")
53-
uploadFileResponse, err := s.uploader.UploadFile(p.AccountUID, filepath.Base(file), request)
65+
uploadFileResponse, err := s.uploader.UploadFile(params.AccountUID, filepath.Base(file), request)
5466
if err != nil {
5567
return nil, err
5668
}
@@ -59,9 +71,9 @@ func (s service) RunTranslate(ctx context.Context, p TranslateParams, files []st
5971
update := TranslateUpdates{ID: uint32(fileID), Upload: pointer.NewP(true)}
6072
updates <- update
6173

62-
if p.SourceLocale == "" {
74+
if params.SourceLocale == "" {
6375
rlog.Debugf("detect language")
64-
detectFileLanguageResponse, err := s.translationControl.DetectFileLanguage(p.AccountUID, uploadFileResponse.FileUID)
76+
detectFileLanguageResponse, err := s.translationControl.DetectFileLanguage(params.AccountUID, uploadFileResponse.FileUID)
6577
if err != nil {
6678
return nil, err
6779
}
@@ -72,7 +84,7 @@ func (s service) RunTranslate(ctx context.Context, p TranslateParams, files []st
7284
return nil, errors.New("timeout exceeded for polling detect file language progress: FileUID:" + string(uploadFileResponse.FileUID))
7385
}
7486
rlog.Debugf("check detection progress")
75-
detectionProgressResponse, err := s.translationControl.DetectionProgress(p.AccountUID, uploadFileResponse.FileUID, detectFileLanguageResponse.LanguageDetectionUID)
87+
detectionProgressResponse, err := s.translationControl.DetectionProgress(params.AccountUID, uploadFileResponse.FileUID, detectFileLanguageResponse.LanguageDetectionUID)
7688
if err != nil {
7789
return nil, err
7890
}
@@ -92,17 +104,17 @@ func (s service) RunTranslate(ctx context.Context, p TranslateParams, files []st
92104
break
93105
}
94106
if len(detectionProgressResponse.DetectedSourceLanguages) > 0 {
95-
p.SourceLocale = detectionProgressResponse.DetectedSourceLanguages[0].LanguageID
107+
params.SourceLocale = detectionProgressResponse.DetectedSourceLanguages[0].LanguageID
96108
}
97109
}
98110
}
99111

100-
params := api.StartParams{
101-
SourceLocaleIO: p.SourceLocale,
102-
TargetLocaleIDs: p.TargetLocales,
112+
startParams := api.StartParams{
113+
SourceLocaleID: params.SourceLocale,
114+
TargetLocaleIDs: params.TargetLocales,
103115
}
104116
rlog.Debugf("start translation")
105-
translatorStartResponse, err := s.fileTranslator.Start(p.AccountUID, uploadFileResponse.FileUID, params)
117+
translatorStartResponse, err := s.fileTranslator.Start(params.AccountUID, uploadFileResponse.FileUID, startParams)
106118
if err != nil {
107119
return nil, err
108120
}
@@ -131,7 +143,7 @@ func (s service) RunTranslate(ctx context.Context, p TranslateParams, files []st
131143
return nil, errors.New("timeout exceeded for polling file translation progress FileUID:" + string(uploadFileResponse.FileUID))
132144
}
133145
rlog.Debugf("check translation progress")
134-
progressResponse, err := s.fileTranslator.Progress(p.AccountUID, uploadFileResponse.FileUID, translatorStartResponse.MtUID)
146+
progressResponse, err := s.fileTranslator.Progress(params.AccountUID, uploadFileResponse.FileUID, translatorStartResponse.MtUID)
135147
if err != nil {
136148
return nil, err
137149
}
@@ -172,21 +184,21 @@ func (s service) RunTranslate(ctx context.Context, p TranslateParams, files []st
172184

173185
for _, localeProcessStatus := range progressResponse.LocaleProcessStatuses {
174186
rlog.Debugf("download start")
175-
reader, err := s.downloader.File(p.AccountUID, uploadFileResponse.FileUID, translatorStartResponse.MtUID, localeProcessStatus.LocaleID)
187+
reader, err := s.downloader.File(params.AccountUID, uploadFileResponse.FileUID, translatorStartResponse.MtUID, localeProcessStatus.LocaleID)
176188
if err != nil {
177189
return nil, err
178190
}
179191
rlog.Debugf("download finished")
180192
ext := filepath.Ext(file)
181193
filenameLocale := strings.TrimSuffix(file, ext) + "_" + localeProcessStatus.LocaleID + ext
182-
outputDirectory, err := filepath.Abs(p.OutputDirectory)
194+
outputDirectory, err := filepath.Abs(params.OutputDirectory)
183195
if err != nil {
184196
return nil, clierror.UIError{
185197
Err: err,
186198
Operation: "get absolute output directory",
187199
Description: "unable to get absolute path for output directory",
188200
Fields: map[string]string{
189-
"outputDirectory": p.OutputDirectory,
201+
"outputDirectory": params.OutputDirectory,
190202
},
191203
}
192204
}

tests/cmd/files/push/push_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestFilesPush(t *testing.T) {
4848
name: "Override file type",
4949
args: append(subCommands, filename, "--type", "PLAIN_TEXT"),
5050
expectedOutputs: []string{filename, "(PLAIN_TEXT)", "successfully", "ACCEPTED"},
51-
unexpectedOutputs: []string{"DEBUG", "ERROR"},
51+
unexpectedOutputs: []string{"DEBUG", "ERROR", "error", "errors"},
5252
wantErr: false,
5353
},
5454
{

0 commit comments

Comments
 (0)