diff --git a/internal/clierrors/command_failed.go b/internal/clierrors/command_failed.go index ac641d063..fc07156b2 100644 --- a/internal/clierrors/command_failed.go +++ b/internal/clierrors/command_failed.go @@ -8,13 +8,6 @@ type CommandFailedError struct { var _ ClientError = CommandFailedError{} -func min(a, b int) int { - if a < b { - return a - } - return b -} - func (err CommandFailedError) ErrorCode() int { return min(err.FailedCount, 99) } diff --git a/internal/commands/storage/import.go b/internal/commands/storage/import.go index 8edca4e8f..0355176fc 100644 --- a/internal/commands/storage/import.go +++ b/internal/commands/storage/import.go @@ -3,6 +3,7 @@ package storage import ( "fmt" "io" + "math" "net/url" "os" "path/filepath" @@ -249,12 +250,16 @@ func (s *importCommand) ExecuteWithoutArguments(exec commands.Executor) (output. }) } else { // we have no knowledge of the remote file size, report bytes uploaded + transferred := fmt.Sprintf("%sB", "-1") + if statusUpdate.bytesTransferred <= math.MaxUint32 { + transferred = ui.AbbrevNumBinaryPrefix(uint(statusUpdate.bytesTransferred)) //nolint:gosec // disable G115: false positive because value is checked + } exec.PushProgressUpdate(messages.Update{ Key: msg, ProgressMessage: fmt.Sprintf( "- %sed %sB (%sBps)", transferType, - ui.AbbrevNumBinaryPrefix(uint(statusUpdate.bytesTransferred)), + transferred, ui.AbbrevNumBinaryPrefix(uint(bps)), ), }) diff --git a/internal/format/stringslice.go b/internal/format/stringslice.go index 75da0cbf8..52d6b433d 100644 --- a/internal/format/stringslice.go +++ b/internal/format/stringslice.go @@ -49,13 +49,13 @@ func toIfaceSlice(val interface{}) ([]interface{}, bool) { } func maxStringLen(strings []string) int { - max := 0 + maxLen := 0 for _, str := range strings { - if strLen := len(str); strLen > max { - max = strLen + if strLen := len(str); strLen > maxLen { + maxLen = strLen } } - return max + return maxLen } func stringSliceString(values []interface{}, andOrOr string, singleLine bool) string { diff --git a/internal/ui/list_layout.go b/internal/ui/list_layout.go index b3314f085..5b850d8cd 100644 --- a/internal/ui/list_layout.go +++ b/internal/ui/list_layout.go @@ -106,7 +106,7 @@ func (s *ListLayout) appendSection(title, note string, sectionBody []string) { if s.style.NoteSeparator { s.appendLine() } - s.l.AppendItem(DefaultNoteColours.Sprintf(note)) + s.l.AppendItem(DefaultNoteColours.Sprint(note)) } if s.style.MarginBottom { s.appendLine() diff --git a/internal/ui/numeric.go b/internal/ui/numeric.go index f912261ae..a3a418702 100644 --- a/internal/ui/numeric.go +++ b/internal/ui/numeric.go @@ -82,6 +82,10 @@ func AbbrevNumBinaryPrefix(raw uint) string { // FormatBytes returns a string with the given number interpreted as bytes and abbreviated with binary formatting (eg 1024 = 1KiB) func FormatBytes(n int) string { + if n < 0 || n > math.MaxUint32 { + return fmt.Sprintf("%sB", "-1") + } + return fmt.Sprintf("%sB", AbbrevNumBinaryPrefix(uint(n))) } diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 18406d070..923cd8546 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -50,7 +50,11 @@ func FormatRange(start, end string) string { // ConcatStrings like join but handles well the empty strings func ConcatStrings(strs ...string) string { - ret := fmt.Sprintf(strs[0]) + if len(strs) == 0 { + return "" + } + + ret := strs[0] if len(strs) <= 1 { return ret diff --git a/internal/ui/ui_test.go b/internal/ui/ui_test.go new file mode 100644 index 000000000..c35ec6c25 --- /dev/null +++ b/internal/ui/ui_test.go @@ -0,0 +1,37 @@ +package ui + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConcatStringsSingleString(t *testing.T) { + result := ConcatStrings("hello") + expected := "hello" + assert.Equal(t, expected, result) +} + +func TestConcatStringsMultipleStrings(t *testing.T) { + result := ConcatStrings("hello", "world", "foo", "bar") + expected := "hello/world/foo/bar" + assert.Equal(t, expected, result) +} + +func TestConcatStringsWithEmptyStrings(t *testing.T) { + result := ConcatStrings("hello", "", "world", "", "foo", "bar") + expected := "hello/world/foo/bar" + assert.Equal(t, expected, result) +} + +func TestConcatStringsAllEmptyStrings(t *testing.T) { + result := ConcatStrings("", "", "") + expected := "" + assert.Equal(t, expected, result) +} + +func TestConcatStringsEmptyInput(t *testing.T) { + result := ConcatStrings() + expected := "" + assert.Equal(t, expected, result) +}