Skip to content

Commit

Permalink
Truncate long messages
Browse files Browse the repository at this point in the history
  • Loading branch information
foteinigk committed Apr 22, 2024
1 parent ee2462a commit 61543eb
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 15 deletions.
22 changes: 18 additions & 4 deletions internal/txlib/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,19 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf(

message := fmt.Sprintf(
"%s.%s - %s",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
body,
))
)

if !args.Silent {
message = truncateMessage(message)
}

send(message)
}
sendMessage("Getting info", false)

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

cyan := color.New(color.FgCyan).SprintFunc()
send(fmt.Sprintf(

message := fmt.Sprintf(
"%s.%s %s - %s",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
cyan("["+code+"]"),
body,
))
)

if !args.Silent {
message = truncateMessage(message)
}

send(message)
}
sendMessage("Pulling file", false)

Expand Down
31 changes: 24 additions & 7 deletions internal/txlib/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,16 @@ func (task *ResourcePushTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf(
message := fmt.Sprintf(
"%s.%s - %s",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
body,
))
)
if !args.Silent {
message = truncateMessage(message)
}
send(message)
}
sendMessage("Getting info", false)
resource, err := txapi.GetResourceById(api, cfgResource.GetAPv3Id())
Expand Down Expand Up @@ -542,12 +546,16 @@ func (task *LanguagePushTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf(
message := fmt.Sprintf(
"%s (%s) - %s",
parts[3],
strings.Join(languages, ", "),
body,
))
)
if !args.Silent {
message = truncateMessage(message)
}
send(message)
}
sendMessage("Pushing", false)

Expand Down Expand Up @@ -594,7 +602,12 @@ func (task *SourceFilePushTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf("%s.%s - %s", parts[3], parts[5], body))

message := fmt.Sprintf("%s.%s - %s", parts[3], parts[5], body)
if !args.Silent {
message = truncateMessage(message)
}
send(message)
}

file, err := os.Open(sourceFile)
Expand Down Expand Up @@ -693,10 +706,14 @@ func (task *TranslationFileTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf(
message := fmt.Sprintf(
"%s.%s %s - %s", parts[3], parts[5],
cyan("["+languageCode+"]"), body,
))
)
if !args.Silent {
message = truncateMessage(message)
}
send(message)
}

// Only check timestamps if -f isn't set and if resource isn't new
Expand Down
9 changes: 9 additions & 0 deletions internal/txlib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,12 @@ func isValidResolutionPolicy(policy string) (IsValid bool) {
return false

}

func truncateMessage(message string) string {
const maxLength = 75

if len(message) > maxLength {
return message[:maxLength] + ".."
}
return message
}
24 changes: 24 additions & 0 deletions internal/txlib/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,27 @@ func TestConflictResolution(t *testing.T) {
}

}

func TestTruncateMessage(t *testing.T) {
result := truncateMessage("short message")
assert.Equal(t, result, "short message")

result = truncateMessage(
"this is a long message that needs to be truncated because it exceeds " +
"the maximum length of 75 characters",
)
assert.Equal(
t,
result,
"this is a long message that needs to be truncated because it exceeds the ma..",
)

result = truncateMessage(
"a message with exactly 75 characters - this message should not be truncated",
)
assert.Equal(
t,
result,
"a message with exactly 75 characters - this message should not be truncated",
)
}
2 changes: 1 addition & 1 deletion pkg/txapi/resource_strings_async_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func PollResourceStringsDownload(download *jsonapi.Resource, filePath string) er
return nil
} else if download.Attributes["status"] == "failed" {
return fmt.Errorf(

Check failure on line 71 in pkg/txapi/resource_strings_async_downloads.go

View workflow job for this annotation

GitHub Actions / staticcheck

error strings should not be capitalized (ST1005)
"download of translation '%s' failed",
"Failed to download translation '%s'",
download.Relationships["resource"].DataSingular.Id,
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/txapi/resource_strings_async_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func PollSourceUpload(upload *jsonapi.Resource) error {

if uploadAttributes.Status == "failed" {
// Wrap the "error"
return fmt.Errorf("upload of resource '%s' failed - %w",
return fmt.Errorf("Failed to upload of resource '%s' - %w",

Check failure on line 87 in pkg/txapi/resource_strings_async_uploads.go

View workflow job for this annotation

GitHub Actions / staticcheck

error strings should not be capitalized (ST1005)
upload.Relationships["resource"].DataSingular.Id,
&uploadAttributes)
} else if uploadAttributes.Status == "succeeded" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/txapi/resource_translations_async_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func PollTranslationDownload(download *jsonapi.Resource, filePath string) error
break
} else if download.Attributes["status"] == "failed" {
return fmt.Errorf(

Check failure on line 53 in pkg/txapi/resource_translations_async_downloads.go

View workflow job for this annotation

GitHub Actions / staticcheck

error strings should not be capitalized (ST1005)
"download of translation '%s' failed",
"Failed to download translation '%s'",
download.Relationships["resource"].DataSingular.Id,
)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/txapi/resource_translations_async_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func PollTranslationUpload(upload *jsonapi.Resource) error {
if uploadAttributes.Status == "failed" {
// Wrap the "error"
return fmt.Errorf(

Check failure on line 90 in pkg/txapi/resource_translations_async_uploads.go

View workflow job for this annotation

GitHub Actions / staticcheck

error strings should not be capitalized (ST1005)
"upload of resource '%s', language '%s' failed - %w",
"Failed to upload resource '%s', language '%s' - %w",
upload.Relationships["resource"].DataSingular.Id,
upload.Relationships["language"].DataSingular.Id,
&uploadAttributes)
Expand Down

0 comments on commit 61543eb

Please sign in to comment.