Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix how byteSize is calculated for different input formats #11

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 34 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,21 +647,44 @@ func unixNatural(s string) int64 {
}

func byteSize(s string) int64 {
w := strings.Split(s, " ")
n, err := strconv.ParseFloat(w[0], 64)
if err != nil {
log.Fatal(err)
log.Printf("byteSize: %s\n", s)

// split s into [match, number, unit], or return 0 if no pattern match is found, eg:
// "0B" -> ["0B" "0" "B"]
// "1.5GB" -> ["1.5GB", "1.5", "GB"]
// "741.4kB" -> ["741.4kB", "741.4", "kB"]
re := regexp.MustCompile(`^(.*?)([[:blank:]]*[KkMmGg]*i*[Bb]*[[:blank:]]*)$`)

sm := re.FindStringSubmatch(s)
if len(sm) != 3 {
log.Fatalf("no pattern match found for %q: %v\n", s, sm)
}

n := 0.0
if sm[1] != "" {
var err error
if n, err = strconv.ParseFloat(sm[1], 64); err != nil {
log.Fatal(err)
}
}
m := 1
switch w[1] {
case "KiB":

m := 1.0
switch strings.ToLower(strings.TrimSpace(sm[2])) {
case "kb":
m = 1000
case "kib":
m = 1024
case "MiB":
case "mb":
m = 1000 * 1000
case "mib":
m = 1024 * 1024
case "GiB":
case "gb":
m = 1000 * 1000 * 1000
case "gib":
m = 1024 * 1024 * 1024
}
return int64(n * float64(m))

return int64(n * m)
}

func nerdctlPull(name string, w io.Writer) error {
Expand Down Expand Up @@ -877,30 +900,6 @@ func nerdctlBuild(dir string, w io.Writer, t string, f string, o string, p strin
return nil
}

func cacheSize(s string) int64 {
s = strings.Replace(s, "B", "", 1)
if s == "" {
return 0
}
m := 1
switch s[len(s)-1] {
case 'K':
m = 1024
s = s[:len(s)-1]
case 'M':
m = 1024 * 1024
s = s[:len(s)-1]
case 'G':
m = 1024 * 1024 * 1024
s = s[:len(s)-1]
}
n, err := strconv.ParseFloat(s, 64)
if err != nil {
log.Fatal(err)
}
return int64(n * float64(m))
}

func nerdctlBuildPrune() (int64, error) {
args := []string{"builder", "prune"}
nc, err := exec.Command(nerdctl, args...).CombinedOutput()
Expand All @@ -912,7 +911,7 @@ func nerdctlBuildPrune() (int64, error) {
for _, line := range lines {
if strings.HasPrefix(line, "Total:") {
s := strings.Replace(line, "Total:", "", 1)
size = cacheSize(strings.TrimSpace(s))
size = byteSize(strings.TrimSpace(s))
}
}
return size, nil
Expand Down