mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 13:26:07 +00:00
5fb9df1640
* Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
131 lines
3.0 KiB
Go
131 lines
3.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package inspect
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"text/tabwriter"
|
|
)
|
|
|
|
const (
|
|
PrettyFormat string = "pretty"
|
|
JSONFormat string = "json"
|
|
)
|
|
|
|
type Formatter interface {
|
|
Format(*OutputFormat) (string, error)
|
|
}
|
|
|
|
func GetSupportedFormats() []string {
|
|
return []string{PrettyFormat, JSONFormat}
|
|
}
|
|
|
|
type prettyFormatter struct{}
|
|
|
|
func newPrettyFormatter() Formatter {
|
|
return &prettyFormatter{}
|
|
}
|
|
func NewFormatter(format string) (Formatter, error) {
|
|
switch format {
|
|
case PrettyFormat:
|
|
return newPrettyFormatter(), nil
|
|
case JSONFormat:
|
|
return newJSONFormatter(), nil
|
|
default:
|
|
return nil, fmt.Errorf("Unknown format: %s", format)
|
|
}
|
|
}
|
|
|
|
func (_ *prettyFormatter) Format(info *OutputFormat) (string, error) {
|
|
var b bytes.Buffer
|
|
tw := tabwriter.NewWriter(&b, 8, 8, 6, ' ', 0)
|
|
|
|
fmt.Fprintf(tw, " ID\t%s", info.Meta.ID)
|
|
fmt.Fprintf(tw, "\n Size\t%d", info.Meta.Size)
|
|
fmt.Fprintf(tw, "\n Index\t%d", info.Meta.Index)
|
|
fmt.Fprintf(tw, "\n Term\t%d", info.Meta.Term)
|
|
fmt.Fprintf(tw, "\n Version\t%d", info.Meta.Version)
|
|
fmt.Fprintf(tw, "\n")
|
|
fmt.Fprintln(tw, "\n Type\tCount\tSize")
|
|
fmt.Fprintf(tw, " %s\t%s\t%s", "----", "----", "----")
|
|
// For each different type generate new output
|
|
for _, s := range info.Stats {
|
|
fmt.Fprintf(tw, "\n %s\t%d\t%s", s.Name, s.Count, ByteSize(uint64(s.Sum)))
|
|
}
|
|
fmt.Fprintf(tw, "\n %s\t%s\t%s", "----", "----", "----")
|
|
fmt.Fprintf(tw, "\n Total\t\t%s", ByteSize(uint64(info.TotalSize)))
|
|
|
|
if info.StatsKV != nil {
|
|
fmt.Fprintf(tw, "\n")
|
|
fmt.Fprintln(tw, "\n Key Name\tCount\tSize")
|
|
fmt.Fprintf(tw, " %s\t%s\t%s", "----", "----", "----")
|
|
// For each different type generate new output
|
|
for _, s := range info.StatsKV {
|
|
fmt.Fprintf(tw, "\n %s\t%d\t%s", s.Name, s.Count, ByteSize(uint64(s.Sum)))
|
|
}
|
|
fmt.Fprintf(tw, "\n %s\t%s\t%s", "----", "----", "----")
|
|
fmt.Fprintf(tw, "\n Total\t\t%s", ByteSize(uint64(info.TotalSizeKV)))
|
|
}
|
|
|
|
if err := tw.Flush(); err != nil {
|
|
return b.String(), err
|
|
}
|
|
|
|
return b.String(), nil
|
|
}
|
|
|
|
type jsonFormatter struct{}
|
|
|
|
func newJSONFormatter() Formatter {
|
|
return &jsonFormatter{}
|
|
}
|
|
|
|
func (_ *jsonFormatter) Format(info *OutputFormat) (string, error) {
|
|
b, err := json.MarshalIndent(info, "", " ")
|
|
if err != nil {
|
|
return "", fmt.Errorf("Failed to marshal original snapshot stats: %v", err)
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
const (
|
|
BYTE = 1 << (10 * iota)
|
|
KILOBYTE
|
|
MEGABYTE
|
|
GIGABYTE
|
|
TERABYTE
|
|
)
|
|
|
|
func ByteSize(bytes uint64) string {
|
|
unit := ""
|
|
value := float64(bytes)
|
|
|
|
switch {
|
|
case bytes >= TERABYTE:
|
|
unit = "TB"
|
|
value = value / TERABYTE
|
|
case bytes >= GIGABYTE:
|
|
unit = "GB"
|
|
value = value / GIGABYTE
|
|
case bytes >= MEGABYTE:
|
|
unit = "MB"
|
|
value = value / MEGABYTE
|
|
case bytes >= KILOBYTE:
|
|
unit = "KB"
|
|
value = value / KILOBYTE
|
|
case bytes >= BYTE:
|
|
unit = "B"
|
|
case bytes == 0:
|
|
return "0"
|
|
}
|
|
|
|
result := strconv.FormatFloat(value, 'f', 1, 64)
|
|
result = strings.TrimSuffix(result, ".0")
|
|
return result + unit
|
|
}
|