Skip to content

Commit 206fe28

Browse files
committed
refactor: replace formatBytes with humanize.IBytes
1 parent 89d5ca8 commit 206fe28

5 files changed

Lines changed: 3 additions & 39 deletions

File tree

cmd/checkpoint.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
truenas "github.com/deevus/truenas-go"
9+
"github.com/dustin/go-humanize"
910
"github.com/spf13/cobra"
1011

1112
"github.com/deevus/pixels/internal/cache"
@@ -120,7 +121,7 @@ func runCheckpointList(cmd *cobra.Command, args []string) error {
120121
w := newTabWriter(cmd)
121122
fmt.Fprintln(w, "LABEL\tSIZE")
122123
for _, s := range snapshots {
123-
fmt.Fprintf(w, "%s\t%s\n", s.SnapshotName, formatBytes(s.Referenced))
124+
fmt.Fprintf(w, "%s\t%s\n", s.SnapshotName, humanize.IBytes(uint64(s.Referenced)))
124125
}
125126
return w.Flush()
126127
}

cmd/helpers_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,3 @@ func TestResolveIP(t *testing.T) {
7575
})
7676
}
7777
}
78-
79-
func TestFormatBytes(t *testing.T) {
80-
tests := []struct {
81-
input int64
82-
want string
83-
}{
84-
{0, "0 B"},
85-
{512, "512 B"},
86-
{1024, "1.0 KiB"},
87-
{1536, "1.5 KiB"},
88-
{1048576, "1.0 MiB"},
89-
{13107200, "12.5 MiB"},
90-
{1073741824, "1.0 GiB"},
91-
}
92-
for _, tt := range tests {
93-
t.Run(tt.want, func(t *testing.T) {
94-
if got := formatBytes(tt.input); got != tt.want {
95-
t.Errorf("formatBytes(%d) = %q, want %q", tt.input, got, tt.want)
96-
}
97-
})
98-
}
99-
}

cmd/root.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,3 @@ func ensureSSHAuth(cmd *cobra.Command, ctx context.Context, ip, name string) err
137137

138138
return client.WriteAuthorizedKey(ctx, containerName(name), pubKey)
139139
}
140-
141-
func formatBytes(b int64) string {
142-
const unit = 1024
143-
if b < unit {
144-
return fmt.Sprintf("%d B", b)
145-
}
146-
div, exp := int64(unit), 0
147-
for n := b / unit; n >= unit; n /= unit {
148-
div *= unit
149-
exp++
150-
}
151-
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
152-
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ go 1.25.0
55
require (
66
github.com/BurntSushi/toml v1.6.0
77
github.com/deevus/truenas-go v0.4.0
8+
github.com/dustin/go-humanize v1.0.1
89
github.com/spf13/cobra v1.10.2
910
)
1011

1112
require (
1213
al.essio.dev/pkg/shellescape v1.6.0 // indirect
13-
github.com/dustin/go-humanize v1.0.1 // indirect
1414
github.com/gorilla/websocket v1.5.3 // indirect
1515
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1616
github.com/spf13/pflag v1.0.9 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ al.essio.dev/pkg/shellescape v1.6.0/go.mod h1:6sIqp7X2P6mThCQ7twERpZTuigpr6KbZWt
33
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
44
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
55
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
6-
github.com/deevus/truenas-go v0.3.0 h1:IuJ7wm+aW0aSmm4AjW8GK6f+8bjKolj9BVd9bTfKeSU=
7-
github.com/deevus/truenas-go v0.3.0/go.mod h1:a5MwZEqT4NE8jwSA9BHONOAO8yH4kCaS5a+d4ad6sLA=
86
github.com/deevus/truenas-go v0.4.0 h1:gESJ0naqtwzgdN1/gG5wBrp/Lm/5HF8xCBsRcwNgg78=
97
github.com/deevus/truenas-go v0.4.0/go.mod h1:a5MwZEqT4NE8jwSA9BHONOAO8yH4kCaS5a+d4ad6sLA=
108
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=

0 commit comments

Comments
 (0)