-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
cli: Add JSON and Pretty Print formatting for consul snapshot inspect
#9006
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
cli: snapshot inspect command supports JSON output | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package inspect | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"sort" | ||
"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 | ||
// For the enhanced stats | ||
ss := make([]typeStats, 0, len(info.Stats)) | ||
|
||
for _, s := range info.Stats { | ||
ss = append(ss, s) | ||
} | ||
|
||
// Sort the stat slice | ||
sort.Slice(ss, func(i, j int) bool { return ss[i].Sum > ss[j].Sum }) | ||
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\t") | ||
fmt.Fprintf(tw, " %s\t%s\t%s\t", "----", "----", "----") | ||
// For each different type generate new output | ||
for _, s := range ss { | ||
fmt.Fprintf(tw, "\n %s\t%d\t%s\t", s.Name, s.Count, ByteSize(uint64(s.Sum))) | ||
} | ||
fmt.Fprintf(tw, "\n %s\t%s\t%s\t", "----", "----", "----") | ||
fmt.Fprintf(tw, "\n Total\t\t%s\t", ByteSize(uint64(info.TotalSize))) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package inspect | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/agent/structs" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
m := make(map[structs.MessageType]typeStats) | ||
m[1] = typeStats{ | ||
Name: "msg", | ||
Sum: 1, | ||
Count: 2, | ||
} | ||
info := OutputFormat{ | ||
Meta: &MetadataInfo{ | ||
ID: "one", | ||
Size: 2, | ||
Index: 3, | ||
Term: 4, | ||
Version: 1, | ||
}, | ||
Stats: m, | ||
TotalSize: 1, | ||
} | ||
|
||
formatters := map[string]Formatter{ | ||
"pretty": newPrettyFormatter(), | ||
// the JSON formatter ignores the showMeta | ||
"json": newJSONFormatter(), | ||
} | ||
|
||
for fmtName, formatter := range formatters { | ||
t.Run(fmtName, func(t *testing.T) { | ||
actual, err := formatter.Format(&info) | ||
require.NoError(t, err) | ||
|
||
gName := fmt.Sprintf("%s", fmtName) | ||
|
||
expected := golden(t, gName, actual) | ||
require.Equal(t, expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
command/snapshot/inspect/testdata/TestSnapshotInspectCommand.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether this field should be
Stats []typeStats
. In the pretty output I see that you are creating a slice of these and then sorting. For the JSON output I wonder whether it would be better to see:Having the MessageType (
int
) as a map key in JSON doesn't seem that useful. Also having it in an array would more easily allow us to aggregate stats for namespaces without having the map keys conflict.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I didn't even notice that, good catch.