Skip to content

Commit fc80657

Browse files
authored
Merge pull request #216 from transifex/TX-15426_cli_errors
Truncate long messages
2 parents 823ef07 + de26dcc commit fc80657

File tree

8 files changed

+78
-15
lines changed

8 files changed

+78
-15
lines changed

internal/txlib/pull.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,19 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
153153
if args.Silent && !force {
154154
return
155155
}
156-
send(fmt.Sprintf(
156+
157+
message := fmt.Sprintf(
157158
"%s.%s - %s",
158159
cfgResource.ProjectSlug,
159160
cfgResource.ResourceSlug,
160161
body,
161-
))
162+
)
163+
164+
if !args.Silent {
165+
message = truncateMessage(message)
166+
}
167+
168+
send(message)
162169
}
163170
sendMessage("Getting info", false)
164171

@@ -364,13 +371,20 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
364371
}
365372

366373
cyan := color.New(color.FgCyan).SprintFunc()
367-
send(fmt.Sprintf(
374+
375+
message := fmt.Sprintf(
368376
"%s.%s %s - %s",
369377
cfgResource.ProjectSlug,
370378
cfgResource.ResourceSlug,
371379
cyan("["+code+"]"),
372380
body,
373-
))
381+
)
382+
383+
if !args.Silent {
384+
message = truncateMessage(message)
385+
}
386+
387+
send(message)
374388
}
375389
sendMessage("Pulling file", false)
376390

internal/txlib/push.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,16 @@ func (task *ResourcePushTask) Run(send func(string), abort func()) {
279279
if args.Silent && !force {
280280
return
281281
}
282-
send(fmt.Sprintf(
282+
message := fmt.Sprintf(
283283
"%s.%s - %s",
284284
cfgResource.ProjectSlug,
285285
cfgResource.ResourceSlug,
286286
body,
287-
))
287+
)
288+
if !args.Silent {
289+
message = truncateMessage(message)
290+
}
291+
send(message)
288292
}
289293
sendMessage("Getting info", false)
290294
resource, err := txapi.GetResourceById(api, cfgResource.GetAPv3Id())
@@ -542,12 +546,16 @@ func (task *LanguagePushTask) Run(send func(string), abort func()) {
542546
if args.Silent && !force {
543547
return
544548
}
545-
send(fmt.Sprintf(
549+
message := fmt.Sprintf(
546550
"%s (%s) - %s",
547551
parts[3],
548552
strings.Join(languages, ", "),
549553
body,
550-
))
554+
)
555+
if !args.Silent {
556+
message = truncateMessage(message)
557+
}
558+
send(message)
551559
}
552560
sendMessage("Pushing", false)
553561

@@ -594,7 +602,12 @@ func (task *SourceFilePushTask) Run(send func(string), abort func()) {
594602
if args.Silent && !force {
595603
return
596604
}
597-
send(fmt.Sprintf("%s.%s - %s", parts[3], parts[5], body))
605+
606+
message := fmt.Sprintf("%s.%s - %s", parts[3], parts[5], body)
607+
if !args.Silent {
608+
message = truncateMessage(message)
609+
}
610+
send(message)
598611
}
599612

600613
file, err := os.Open(sourceFile)
@@ -693,10 +706,14 @@ func (task *TranslationFileTask) Run(send func(string), abort func()) {
693706
if args.Silent && !force {
694707
return
695708
}
696-
send(fmt.Sprintf(
709+
message := fmt.Sprintf(
697710
"%s.%s %s - %s", parts[3], parts[5],
698711
cyan("["+languageCode+"]"), body,
699-
))
712+
)
713+
if !args.Silent {
714+
message = truncateMessage(message)
715+
}
716+
send(message)
700717
}
701718

702719
// Only check timestamps if -f isn't set and if resource isn't new

internal/txlib/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,11 @@ func isValidResolutionPolicy(policy string) (IsValid bool) {
210210
return false
211211

212212
}
213+
214+
func truncateMessage(message string) string {
215+
maxLength := 80
216+
if len(message) > maxLength {
217+
return message[:maxLength-2] + ".."
218+
}
219+
return message
220+
}

internal/txlib/utils_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,27 @@ func TestConflictResolution(t *testing.T) {
8080
}
8181

8282
}
83+
84+
func TestTruncateMessage(t *testing.T) {
85+
result := truncateMessage("short message")
86+
assert.Equal(t, result, "short message")
87+
88+
result = truncateMessage(
89+
"this is a long message that needs to be truncated because it exceeds " +
90+
"the maximum length of 75 characters",
91+
)
92+
assert.Equal(
93+
t,
94+
result,
95+
"this is a long message that needs to be truncated because it exceeds the maxim..",
96+
)
97+
98+
result = truncateMessage(
99+
"a message with exactly 75 characters - this message should not be truncated",
100+
)
101+
assert.Equal(
102+
t,
103+
result,
104+
"a message with exactly 75 characters - this message should not be truncated",
105+
)
106+
}

pkg/txapi/resource_strings_async_downloads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func PollResourceStringsDownload(download *jsonapi.Resource, filePath string) er
6969
return nil
7070
} else if download.Attributes["status"] == "failed" {
7171
return fmt.Errorf(
72-
"download of translation '%s' failed",
72+
"failed to download translation '%s'",
7373
download.Relationships["resource"].DataSingular.Id,
7474
)
7575

pkg/txapi/resource_strings_async_uploads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func PollSourceUpload(upload *jsonapi.Resource) error {
8484

8585
if uploadAttributes.Status == "failed" {
8686
// Wrap the "error"
87-
return fmt.Errorf("upload of resource '%s' failed - %w",
87+
return fmt.Errorf("failed to upload of resource '%s' - %w",
8888
upload.Relationships["resource"].DataSingular.Id,
8989
&uploadAttributes)
9090
} else if uploadAttributes.Status == "succeeded" {

pkg/txapi/resource_translations_async_downloads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func PollTranslationDownload(download *jsonapi.Resource, filePath string) error
5151
break
5252
} else if download.Attributes["status"] == "failed" {
5353
return fmt.Errorf(
54-
"download of translation '%s' failed",
54+
"failed to download translation '%s'",
5555
download.Relationships["resource"].DataSingular.Id,
5656
)
5757
}

pkg/txapi/resource_translations_async_uploads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func PollTranslationUpload(upload *jsonapi.Resource) error {
8888
if uploadAttributes.Status == "failed" {
8989
// Wrap the "error"
9090
return fmt.Errorf(
91-
"upload of resource '%s', language '%s' failed - %w",
91+
"failed to upload resource '%s', language '%s' - %w",
9292
upload.Relationships["resource"].DataSingular.Id,
9393
upload.Relationships["language"].DataSingular.Id,
9494
&uploadAttributes)

0 commit comments

Comments
 (0)