From ff28b0f5d320bba3aebadab6a85398ea9fb758ce Mon Sep 17 00:00:00 2001 From: Tomas Hozza Date: Wed, 31 Aug 2022 10:23:55 +0200 Subject: [PATCH] common: define storage units as constants Define all used storage units as constants. Use them in `DataSizeToUint64()`, instead of literal multiples. --- internal/common/constants.go | 11 +++++++++++ internal/common/helpers.go | 16 ++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/internal/common/constants.go b/internal/common/constants.go index 57dc40843..7c570a708 100644 --- a/internal/common/constants.go +++ b/internal/common/constants.go @@ -2,6 +2,17 @@ package common import "fmt" +const ( + KiloByte = 1000 // kB + KibiByte = 1024 // KiB + MegaByte = 1000 * 1000 // MB + MebiByte = 1024 * 1024 // MiB + GigaByte = 1000 * 1000 * 1000 // GB + GibiByte = 1024 * 1024 * 1024 // GiB + TeraByte = 1000 * 1000 * 1000 * 1000 // TB + TebiByte = 1024 * 1024 * 1024 * 1024 // TiB +) + // These constants are set during buildtime using additional // compiler flags. Not all of them are necessarily defined // because RPMs can be build from a tarball and spec file without diff --git a/internal/common/helpers.go b/internal/common/helpers.go index 339e5b73f..838a4117d 100644 --- a/internal/common/helpers.go +++ b/internal/common/helpers.go @@ -65,14 +65,14 @@ func DataSizeToUint64(size string) (uint64, error) { re *regexp.Regexp multiple uint64 }{ - {regexp.MustCompile(`^\s*[[:digit:]]+\s*kB$`), 1000}, - {regexp.MustCompile(`^\s*[[:digit:]]+\s*KiB$`), 1024}, - {regexp.MustCompile(`^\s*[[:digit:]]+\s*MB$`), 1000 * 1000}, - {regexp.MustCompile(`^\s*[[:digit:]]+\s*MiB$`), 1024 * 1024}, - {regexp.MustCompile(`^\s*[[:digit:]]+\s*GB$`), 1000 * 1000 * 1000}, - {regexp.MustCompile(`^\s*[[:digit:]]+\s*GiB$`), 1024 * 1024 * 1024}, - {regexp.MustCompile(`^\s*[[:digit:]]+\s*TB$`), 1000 * 1000 * 1000 * 1000}, - {regexp.MustCompile(`^\s*[[:digit:]]+\s*TiB$`), 1024 * 1024 * 1024 * 1024}, + {regexp.MustCompile(`^\s*[[:digit:]]+\s*kB$`), KiloByte}, + {regexp.MustCompile(`^\s*[[:digit:]]+\s*KiB$`), KibiByte}, + {regexp.MustCompile(`^\s*[[:digit:]]+\s*MB$`), MegaByte}, + {regexp.MustCompile(`^\s*[[:digit:]]+\s*MiB$`), MebiByte}, + {regexp.MustCompile(`^\s*[[:digit:]]+\s*GB$`), GigaByte}, + {regexp.MustCompile(`^\s*[[:digit:]]+\s*GiB$`), GibiByte}, + {regexp.MustCompile(`^\s*[[:digit:]]+\s*TB$`), TeraByte}, + {regexp.MustCompile(`^\s*[[:digit:]]+\s*TiB$`), TebiByte}, {regexp.MustCompile(`^\s*[[:digit:]]+$`), 1}, }