From 989fc92d32c79ff3794b5ef65e4f6a0dcf513c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20V=C3=A4lim=C3=A4ki?= Date: Fri, 20 Sep 2024 13:35:23 +0300 Subject: [PATCH 1/6] refactor(clierrors): remove min() in favor on a builtin --- internal/clierrors/command_failed.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/internal/clierrors/command_failed.go b/internal/clierrors/command_failed.go index ac641d06..fc07156b 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) } From d491ef1faa6e63595bdd51c050579e65b1ab0490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20V=C3=A4lim=C3=A4ki?= Date: Fri, 20 Sep 2024 13:35:55 +0300 Subject: [PATCH 2/6] fix(format): rename max to maxLen due conflict with builtin --- internal/format/stringslice.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/format/stringslice.go b/internal/format/stringslice.go index 75da0cbf..52d6b433 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 { From 59720a017c14e7260b2e0005b8dc148cb594e9b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20V=C3=A4lim=C3=A4ki?= Date: Fri, 20 Sep 2024 13:36:40 +0300 Subject: [PATCH 3/6] fix(storage): check int64 value before uint conversion --- internal/commands/storage/import.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/commands/storage/import.go b/internal/commands/storage/import.go index 8edca4e8..0355176f 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)), ), }) From 434cc1812186062c3b2dcbd715c5520d1ce46416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20V=C3=A4lim=C3=A4ki?= Date: Fri, 20 Sep 2024 13:37:42 +0300 Subject: [PATCH 4/6] fix(ui): add format for Sprintf --- internal/ui/list_layout.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ui/list_layout.go b/internal/ui/list_layout.go index b3314f08..3e4ea732 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.Sprintf("%s", note)) } if s.style.MarginBottom { s.appendLine() From 6da3d3d14999794308a249b2dc053e44679f31db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20V=C3=A4lim=C3=A4ki?= Date: Fri, 20 Sep 2024 13:38:36 +0300 Subject: [PATCH 5/6] fix(ui): check for emptiness in ConcatStrings --- internal/ui/ui.go | 6 +++++- internal/ui/ui_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 internal/ui/ui_test.go diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 18406d07..923cd854 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 00000000..d5220937 --- /dev/null +++ b/internal/ui/ui_test.go @@ -0,0 +1,45 @@ +package ui + +import ( + "testing" +) + +func TestConcatStringsSingleString(t *testing.T) { + result := ConcatStrings("hello") + expected := "hello" + if result != expected { + t.Errorf("expected %s, got %s", expected, result) + } +} + +func TestConcatStringsMultipleStrings(t *testing.T) { + result := ConcatStrings("hello", "world", "foo", "bar") + expected := "hello/world/foo/bar" + if result != expected { + t.Errorf("expected %s, got %s", expected, result) + } +} + +func TestConcatStringsWithEmptyStrings(t *testing.T) { + result := ConcatStrings("hello", "", "world", "", "foo", "bar") + expected := "hello/world/foo/bar" + if result != expected { + t.Errorf("expected %s, got %s", expected, result) + } +} + +func TestConcatStringsAllEmptyStrings(t *testing.T) { + result := ConcatStrings("", "", "") + expected := "" + if result != expected { + t.Errorf("expected %s, got %s", expected, result) + } +} + +func TestConcatStringsEmptyInput(t *testing.T) { + result := ConcatStrings() + expected := "" + if result != expected { + t.Errorf("expected %s, got %s", expected, result) + } +} From 5b72a13800e8fdac376ca926149fbc9231d35830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20V=C3=A4lim=C3=A4ki?= Date: Fri, 20 Sep 2024 13:38:57 +0300 Subject: [PATCH 6/6] fix(ui): check int value before uint conversion --- internal/ui/numeric.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/ui/numeric.go b/internal/ui/numeric.go index f912261a..a3a41870 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))) }