From b7418d6bbaa535725d36da2787672ae7840fba6b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 5 Oct 2022 05:12:20 +0000 Subject: [PATCH] build(deps): bump github.com/labstack/gommon from 0.3.1 to 0.4.0 Bumps [github.com/labstack/gommon](https://github.com/labstack/gommon) from 0.3.1 to 0.4.0. - [Release notes](https://github.com/labstack/gommon/releases) - [Commits](https://github.com/labstack/gommon/compare/v0.3.1...v0.4.0) --- updated-dependencies: - dependency-name: github.com/labstack/gommon dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 3 +- .../github.com/labstack/gommon/bytes/bytes.go | 185 ++++++++++++++---- vendor/github.com/labstack/gommon/log/log.go | 6 +- vendor/modules.txt | 2 +- 5 files changed, 154 insertions(+), 44 deletions(-) diff --git a/go.mod b/go.mod index ce744142c..d006e9f3d 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( github.com/julienschmidt/httprouter v1.3.0 github.com/kolo/xmlrpc v0.0.0-20201022064351-38db28db192b github.com/labstack/echo/v4 v4.9.0 - github.com/labstack/gommon v0.3.1 + github.com/labstack/gommon v0.4.0 github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.0.3-0.20220114050600-8b9d41f48198 github.com/openshift-online/ocm-sdk-go v0.1.287 diff --git a/go.sum b/go.sum index 1c73a5d64..92e502b0c 100644 --- a/go.sum +++ b/go.sum @@ -1164,8 +1164,9 @@ github.com/labstack/echo/v4 v4.3.0/go.mod h1:PvmtTvhVqKDzDQy4d3bWzPjZLzom4iQbAZy github.com/labstack/echo/v4 v4.9.0 h1:wPOF1CE6gvt/kmbMR4dGzWvHMPT+sAEUJOwOTtvITVY= github.com/labstack/echo/v4 v4.9.0/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks= github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= -github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o= github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= +github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= +github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= github.com/ldez/gomoddirectives v0.2.2/go.mod h1:cpgBogWITnCfRq2qGoDkKMEVSaarhdBr6g8G04uz6d0= github.com/ldez/tagliatelle v0.2.0/go.mod h1:8s6WJQwEYHbKZDsp/LjArytKOG8qaMrKQQ3mFukHs88= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= diff --git a/vendor/github.com/labstack/gommon/bytes/bytes.go b/vendor/github.com/labstack/gommon/bytes/bytes.go index 2f6bcec6e..b07e31cdb 100644 --- a/vendor/github.com/labstack/gommon/bytes/bytes.go +++ b/vendor/github.com/labstack/gommon/bytes/bytes.go @@ -12,19 +12,31 @@ type ( Bytes struct{} ) +// binary units (IEC 60027) const ( _ = 1.0 << (10 * iota) // ignore first value by assigning to blank identifier - KB - MB - GB - TB - PB - EB + KiB + MiB + GiB + TiB + PiB + EiB +) + +// decimal units (SI international system of units) +const ( + KB = 1000 + MB = KB * 1000 + GB = MB * 1000 + TB = GB * 1000 + PB = TB * 1000 + EB = PB * 1000 ) var ( - pattern = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]B?|B?)$`) - global = New() + patternBinary = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]iB?)$`) + patternDecimal = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]B?|B?)$`) + global = New() ) // New creates a Bytes instance. @@ -32,44 +44,97 @@ func New() *Bytes { return &Bytes{} } -// Format formats bytes integer to human readable string. +// Format formats bytes integer to human readable string according to IEC 60027. // For example, 31323 bytes will return 30.59KB. -func (*Bytes) Format(b int64) string { +func (b *Bytes) Format(value int64) string { + return b.FormatBinary(value) +} + +// FormatBinary formats bytes integer to human readable string according to IEC 60027. +// For example, 31323 bytes will return 30.59KB. +func (*Bytes) FormatBinary(value int64) string { multiple := "" - value := float64(b) + val := float64(value) switch { - case b >= EB: - value /= EB - multiple = "EB" - case b >= PB: - value /= PB - multiple = "PB" - case b >= TB: - value /= TB - multiple = "TB" - case b >= GB: - value /= GB - multiple = "GB" - case b >= MB: - value /= MB - multiple = "MB" - case b >= KB: - value /= KB - multiple = "KB" - case b == 0: + case value >= EiB: + val /= EiB + multiple = "EiB" + case value >= PiB: + val /= PiB + multiple = "PiB" + case value >= TiB: + val /= TiB + multiple = "TiB" + case value >= GiB: + val /= GiB + multiple = "GiB" + case value >= MiB: + val /= MiB + multiple = "MiB" + case value >= KiB: + val /= KiB + multiple = "KiB" + case value == 0: return "0" default: - return strconv.FormatInt(b, 10) + "B" + return strconv.FormatInt(value, 10) + "B" } - return fmt.Sprintf("%.2f%s", value, multiple) + return fmt.Sprintf("%.2f%s", val, multiple) +} + +// FormatDecimal formats bytes integer to human readable string according to SI international system of units. +// For example, 31323 bytes will return 31.32KB. +func (*Bytes) FormatDecimal(value int64) string { + multiple := "" + val := float64(value) + + switch { + case value >= EB: + val /= EB + multiple = "EB" + case value >= PB: + val /= PB + multiple = "PB" + case value >= TB: + val /= TB + multiple = "TB" + case value >= GB: + val /= GB + multiple = "GB" + case value >= MB: + val /= MB + multiple = "MB" + case value >= KB: + val /= KB + multiple = "KB" + case value == 0: + return "0" + default: + return strconv.FormatInt(value, 10) + "B" + } + + return fmt.Sprintf("%.2f%s", val, multiple) } // Parse parses human readable bytes string to bytes integer. -// For example, 6GB (6G is also valid) will return 6442450944. -func (*Bytes) Parse(value string) (i int64, err error) { - parts := pattern.FindStringSubmatch(value) +// For example, 6GiB (6Gi is also valid) will return 6442450944, and +// 6GB (6G is also valid) will return 6000000000. +func (b *Bytes) Parse(value string) (int64, error) { + + i, err := b.ParseBinary(value) + if err == nil { + return i, err + } + + return b.ParseDecimal(value) +} + +// ParseBinary parses human readable bytes string to bytes integer. +// For example, 6GiB (6Gi is also valid) will return 6442450944. +func (*Bytes) ParseBinary(value string) (i int64, err error) { + parts := patternBinary.FindStringSubmatch(value) if len(parts) < 3 { return 0, fmt.Errorf("error parsing value=%s", value) } @@ -81,8 +146,38 @@ func (*Bytes) Parse(value string) (i int64, err error) { } switch multiple { + case "KI", "KIB": + return int64(bytes * KiB), nil + case "MI", "MIB": + return int64(bytes * MiB), nil + case "GI", "GIB": + return int64(bytes * GiB), nil + case "TI", "TIB": + return int64(bytes * TiB), nil + case "PI", "PIB": + return int64(bytes * PiB), nil + case "EI", "EIB": + return int64(bytes * EiB), nil default: return int64(bytes), nil + } +} + +// ParseDecimal parses human readable bytes string to bytes integer. +// For example, 6GB (6G is also valid) will return 6000000000. +func (*Bytes) ParseDecimal(value string) (i int64, err error) { + parts := patternDecimal.FindStringSubmatch(value) + if len(parts) < 3 { + return 0, fmt.Errorf("error parsing value=%s", value) + } + bytesString := parts[1] + multiple := strings.ToUpper(parts[2]) + bytes, err := strconv.ParseFloat(bytesString, 64) + if err != nil { + return + } + + switch multiple { case "K", "KB": return int64(bytes * KB), nil case "M", "MB": @@ -95,15 +190,27 @@ func (*Bytes) Parse(value string) (i int64, err error) { return int64(bytes * PB), nil case "E", "EB": return int64(bytes * EB), nil + default: + return int64(bytes), nil } } // Format wraps global Bytes's Format function. -func Format(b int64) string { - return global.Format(b) +func Format(value int64) string { + return global.Format(value) +} + +// FormatBinary wraps global Bytes's FormatBinary function. +func FormatBinary(value int64) string { + return global.FormatBinary(value) +} + +// FormatDecimal wraps global Bytes's FormatDecimal function. +func FormatDecimal(value int64) string { + return global.FormatDecimal(value) } // Parse wraps global Bytes's Parse function. -func Parse(val string) (int64, error) { - return global.Parse(val) +func Parse(value string) (int64, error) { + return global.Parse(value) } diff --git a/vendor/github.com/labstack/gommon/log/log.go b/vendor/github.com/labstack/gommon/log/log.go index 06fa37e0e..25f719aa2 100644 --- a/vendor/github.com/labstack/gommon/log/log.go +++ b/vendor/github.com/labstack/gommon/log/log.go @@ -391,7 +391,7 @@ func (l *Logger) log(level Lvl, format string, args ...interface{}) { if err == nil { s := buf.String() i := buf.Len() - 1 - if s[i] == '}' { + if i >= 0 && s[i] == '}' { // JSON header buf.Truncate(i) buf.WriteByte(',') @@ -404,7 +404,9 @@ func (l *Logger) log(level Lvl, format string, args ...interface{}) { } } else { // Text header - buf.WriteByte(' ') + if len(s) > 0 { + buf.WriteByte(' ') + } buf.WriteString(message) } buf.WriteByte('\n') diff --git a/vendor/modules.txt b/vendor/modules.txt index eeb0d1f1c..912e0dd5e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -412,7 +412,7 @@ github.com/kr/text ## explicit github.com/labstack/echo/v4 github.com/labstack/echo/v4/middleware -# github.com/labstack/gommon v0.3.1 +# github.com/labstack/gommon v0.4.0 ## explicit github.com/labstack/gommon/bytes github.com/labstack/gommon/color