From 7b5c7895050714b5cfe0bab24bbe2b841a03bec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=C3=B4i=20Tran?= Date: Thu, 25 Jun 2026 16:47:59 +0200 Subject: [PATCH 1/3] fix: use existing formatSize function, size regards model files only Follow up PR for #985 --- cmd/cli/commands/search.go | 11 ++--------- cmd/cli/commands/search_test.go | 24 ------------------------ cmd/cli/search/backend_resolution.go | 3 ++- pkg/distribution/format/dduf.go | 2 +- pkg/distribution/format/format.go | 9 ++++++--- pkg/distribution/format/format_test.go | 13 +++++++++---- pkg/distribution/format/safetensors.go | 2 +- 7 files changed, 21 insertions(+), 43 deletions(-) diff --git a/cmd/cli/commands/search.go b/cmd/cli/commands/search.go index 5c78204d2..fed1eb0fa 100644 --- a/cmd/cli/commands/search.go +++ b/cmd/cli/commands/search.go @@ -4,9 +4,9 @@ import ( "bytes" "fmt" - "github.com/docker/go-units" "github.com/docker/model-runner/cmd/cli/commands/formatter" "github.com/docker/model-runner/cmd/cli/search" + "github.com/docker/model-runner/pkg/distribution/format" "github.com/spf13/cobra" ) @@ -106,7 +106,7 @@ func prettyPrintSearchResults(results []search.SearchResult) string { name, r.Description, r.Backend, - formatSize(r.Size), + format.FormatSize(r.Size), formatCount(r.Downloads), formatCount(r.Stars), r.Source, @@ -128,10 +128,3 @@ func formatCount(n int64) string { return fmt.Sprintf("%d", n) } -// formatSize formats a byte count as a human-readable size string -func formatSize(n int64) string { - if n <= 0 { - return "n/a" - } - return units.CustomSize("%.2f%s", float64(n), 1000.0, []string{"B", "kB", "MB", "GB", "TB"}) -} diff --git a/cmd/cli/commands/search_test.go b/cmd/cli/commands/search_test.go index a38157d69..3177a3186 100644 --- a/cmd/cli/commands/search_test.go +++ b/cmd/cli/commands/search_test.go @@ -4,30 +4,6 @@ import ( "testing" ) -func TestFormatSize(t *testing.T) { - tests := []struct { - name string - input int64 - want string - }{ - {name: "zero returns n/a", input: 0, want: "n/a"}, - {name: "negative returns n/a", input: -1, want: "n/a"}, - {name: "bytes", input: 500, want: "500.00B"}, - {name: "kilobytes", input: 1500, want: "1.50kB"}, - {name: "megabytes", input: 2_500_000, want: "2.50MB"}, - {name: "gigabytes", input: 4_300_000_000, want: "4.30GB"}, - {name: "terabytes", input: 1_200_000_000_000, want: "1.20TB"}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := formatSize(tt.input); got != tt.want { - t.Errorf("formatSize(%d) = %q, want %q", tt.input, got, tt.want) - } - }) - } -} - func TestFormatCount(t *testing.T) { tests := []struct { name string diff --git a/cmd/cli/search/backend_resolution.go b/cmd/cli/search/backend_resolution.go index 3e7075f58..eb1fabc55 100644 --- a/cmd/cli/search/backend_resolution.go +++ b/cmd/cli/search/backend_resolution.go @@ -92,7 +92,8 @@ func (r *huggingFaceRepoBackendResolver) Resolve(ctx context.Context, target str if err != nil { return backendUnknown, 0, err } - return backendFromRepoFiles(repoFiles), distributionhf.TotalSize(repoFiles), nil + modelFiles, _ := distributionhf.FilterModelFiles(repoFiles) + return backendFromRepoFiles(repoFiles), distributionhf.TotalSize(modelFiles), nil } func backendFromFormat(format disttypes.Format) string { diff --git a/pkg/distribution/format/dduf.go b/pkg/distribution/format/dduf.go index e0d42d2ec..0be7568ab 100644 --- a/pkg/distribution/format/dduf.go +++ b/pkg/distribution/format/dduf.go @@ -61,7 +61,7 @@ func (d *DDUFFormat) ExtractConfig(paths []string) (types.Config, error) { return types.Config{ Format: types.FormatDDUF, Architecture: "diffusers", - Size: formatSize(totalSize), + Size: FormatSize(totalSize), Diffusers: map[string]string{ "layout": "dduf", "dduf_file": ddufFile, diff --git a/pkg/distribution/format/format.go b/pkg/distribution/format/format.go index 9b6fbf24e..07ed1930f 100644 --- a/pkg/distribution/format/format.go +++ b/pkg/distribution/format/format.go @@ -103,8 +103,11 @@ func formatParameters(params int64) string { return units.CustomSize("%.2f%s", float64(params), 1000.0, []string{"", "K", "M", "B", "T"}) } -// formatSize converts bytes to human-readable format matching Docker's style -// Returns format like "256MB" (decimal units, no space, matching `docker images`) -func formatSize(bytes int64) string { +// FormatSize converts bytes to human-readable format matching Docker's style. +// Returns "0.00B" for zero or negative sizes, otherwise formats like "256MB". +func FormatSize(bytes int64) string { + if bytes <= 0 { + bytes = 0 + } return units.CustomSize("%.2f%s", float64(bytes), 1000.0, []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}) } diff --git a/pkg/distribution/format/format_test.go b/pkg/distribution/format/format_test.go index c248f840b..13a4eca0e 100644 --- a/pkg/distribution/format/format_test.go +++ b/pkg/distribution/format/format_test.go @@ -223,17 +223,22 @@ func TestFormatSize(t *testing.T) { bytes int64 want string }{ - {1000, "1.00kB"}, + {0, "0.00B"}, + {-1, "0.00B"}, + {500, "500.00B"}, + {1500, "1.50kB"}, {1000000, "1.00MB"}, + {2_500_000, "2.50MB"}, {1000000000, "1.00GB"}, - {5000000000, "5.00GB"}, + {4_300_000_000, "4.30GB"}, + {1_200_000_000_000, "1.20TB"}, } for _, tt := range tests { t.Run(tt.want, func(t *testing.T) { - got := formatSize(tt.bytes) + got := FormatSize(tt.bytes) if got != tt.want { - t.Errorf("formatSize(%d) = %q, want %q", tt.bytes, got, tt.want) + t.Errorf("FormatSize(%d) = %q, want %q", tt.bytes, got, tt.want) } }) } diff --git a/pkg/distribution/format/safetensors.go b/pkg/distribution/format/safetensors.go index 530199aa9..c9ec3556e 100644 --- a/pkg/distribution/format/safetensors.go +++ b/pkg/distribution/format/safetensors.go @@ -130,7 +130,7 @@ func (s *SafetensorsFormat) ExtractConfig(paths []string) (types.Config, error) Format: types.FormatSafetensors, Parameters: formatParameters(params), Quantization: header.getQuantization(), - Size: formatSize(totalSize), + Size: FormatSize(totalSize), Architecture: architecture, Safetensors: header.extractMetadata(), ContextSize: contextSize, From 8d9e1988c9d1b56c59a9b6277b7f0102ffec1320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=C3=B4i=20Tran?= Date: Thu, 25 Jun 2026 16:56:47 +0200 Subject: [PATCH 2/3] fix: make FormatSize return "n/a" as suggested by gemini-code-assist --- pkg/distribution/format/format.go | 4 ++-- pkg/distribution/format/format_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/distribution/format/format.go b/pkg/distribution/format/format.go index 07ed1930f..377939f10 100644 --- a/pkg/distribution/format/format.go +++ b/pkg/distribution/format/format.go @@ -104,10 +104,10 @@ func formatParameters(params int64) string { } // FormatSize converts bytes to human-readable format matching Docker's style. -// Returns "0.00B" for zero or negative sizes, otherwise formats like "256MB". +// Returns "n/a" for zero or negative sizes, otherwise formats like "256MB". func FormatSize(bytes int64) string { if bytes <= 0 { - bytes = 0 + return "n/a" } return units.CustomSize("%.2f%s", float64(bytes), 1000.0, []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}) } diff --git a/pkg/distribution/format/format_test.go b/pkg/distribution/format/format_test.go index 13a4eca0e..0aff23f3e 100644 --- a/pkg/distribution/format/format_test.go +++ b/pkg/distribution/format/format_test.go @@ -223,8 +223,8 @@ func TestFormatSize(t *testing.T) { bytes int64 want string }{ - {0, "0.00B"}, - {-1, "0.00B"}, + {0, "n/a"}, + {-1, "n/a"}, {500, "500.00B"}, {1500, "1.50kB"}, {1000000, "1.00MB"}, From 129ab8a67a5e68a23a1ab87857fa370c3c0a4718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=C3=B4i=20Tran?= Date: Thu, 25 Jun 2026 21:54:26 +0200 Subject: [PATCH 3/3] fix: linting --- cmd/cli/commands/search.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/cli/commands/search.go b/cmd/cli/commands/search.go index fed1eb0fa..8a8a21263 100644 --- a/cmd/cli/commands/search.go +++ b/cmd/cli/commands/search.go @@ -127,4 +127,3 @@ func formatCount(n int64) string { } return fmt.Sprintf("%d", n) } -