From aabb537aed66c1e456871e5f0727632bbd5b9f17 Mon Sep 17 00:00:00 2001 From: Joel Watson Date: Thu, 5 Nov 2020 16:25:21 -0600 Subject: [PATCH] Pull sorting into separate function --- command/snapshot/inspect/snapshot_inspect.go | 38 ++++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/command/snapshot/inspect/snapshot_inspect.go b/command/snapshot/inspect/snapshot_inspect.go index c40fe3d762..ee1c87dd05 100644 --- a/command/snapshot/inspect/snapshot_inspect.go +++ b/command/snapshot/inspect/snapshot_inspect.go @@ -145,7 +145,7 @@ func (c *cmd) Run(args []string) int { } //Restructures stats given above to be human readable - formattedStats, formattedStatsKV := generatetypeStats(info) + formattedStats, formattedStatsKV := generateStats(info) in := &OutputFormat{ Meta: metaformat, @@ -171,22 +171,14 @@ type typeStats struct { Count int } -func generatetypeStats(info SnapshotInfo) ([]typeStats, []typeStats) { +func generateStats(info SnapshotInfo) ([]typeStats, []typeStats) { 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 { - if ss[i].Sum == ss[j].Sum { - // sort alphabetically if size is equal - return ss[i].Name < ss[j].Name - } - - return ss[i].Sum > ss[j].Sum - }) + ss = sortTypeStats(ss) if len(info.StatsKV) > 0 { ks := make([]typeStats, 0, len(info.StatsKV)) @@ -195,15 +187,7 @@ func generatetypeStats(info SnapshotInfo) ([]typeStats, []typeStats) { ks = append(ks, s) } - // Sort the kv stat slice - sort.Slice(ks, func(i, j int) bool { - if ks[i].Sum == ks[j].Sum { - // sort alphabetically if size is equal - return ks[i].Name < ks[j].Name - } - - return ks[i].Sum > ks[j].Sum - }) + ks = sortTypeStats(ks) return ss, ks } @@ -211,6 +195,20 @@ func generatetypeStats(info SnapshotInfo) ([]typeStats, []typeStats) { return ss, nil } +// Sort the stat slice +func sortTypeStats(stats []typeStats) []typeStats { + sort.Slice(stats, func(i, j int) bool { + // sort alphabetically if size is equal + if stats[i].Sum == stats[j].Sum { + return stats[i].Name < stats[j].Name + } + + return stats[i].Sum > stats[j].Sum + }) + + return stats +} + // countingReader helps keep track of the bytes we have read // when reading snapshots type countingReader struct {