refactor(storage): standardize size limits to use GB units
This commit is contained in:
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -39,6 +40,46 @@ func parseInt(value string, min int) (int, error) {
|
||||
return int(parsed64), nil
|
||||
}
|
||||
|
||||
const bytesPerGigabyte = 1024 * 1024 * 1024
|
||||
|
||||
func parseGigabytes(value string, min float64) (int64, error) {
|
||||
raw := strings.TrimSpace(value)
|
||||
lower := strings.ToLower(raw)
|
||||
if strings.HasSuffix(lower, "gb") {
|
||||
raw = strings.TrimSpace(raw[:len(raw)-2])
|
||||
}
|
||||
parsed, err := strconv.ParseFloat(raw, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("must be a number of GB")
|
||||
}
|
||||
if parsed < min {
|
||||
return 0, fmt.Errorf("must be at least %s", trimTrailingZeros(min))
|
||||
}
|
||||
bytes := parsed * bytesPerGigabyte
|
||||
if bytes > math.MaxInt64 {
|
||||
return 0, fmt.Errorf("is too large")
|
||||
}
|
||||
return int64(math.Round(bytes)), nil
|
||||
}
|
||||
|
||||
func formatGigabytesFromBytes(bytes int64) string {
|
||||
if bytes <= 0 {
|
||||
return "0"
|
||||
}
|
||||
value := float64(bytes) / bytesPerGigabyte
|
||||
return trimTrailingZeros(value)
|
||||
}
|
||||
|
||||
func trimTrailingZeros(value float64) string {
|
||||
text := strconv.FormatFloat(value, 'f', 3, 64)
|
||||
text = strings.TrimRight(text, "0")
|
||||
text = strings.TrimRight(text, ".")
|
||||
if text == "" {
|
||||
return "0"
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func formatBool(value bool) string {
|
||||
if value {
|
||||
return "true"
|
||||
|
||||
Reference in New Issue
Block a user