From 0553532e00498dfc575770d6c931c91c593ed588 Mon Sep 17 00:00:00 2001 From: Joel Watson Date: Tue, 10 Nov 2020 10:29:49 -0600 Subject: [PATCH] Break KV portion of enchance into separate func --- command/snapshot/inspect/snapshot_inspect.go | 79 +++++++++++--------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/command/snapshot/inspect/snapshot_inspect.go b/command/snapshot/inspect/snapshot_inspect.go index 1717e1d8b..be4241dba 100644 --- a/command/snapshot/inspect/snapshot_inspect.go +++ b/command/snapshot/inspect/snapshot_inspect.go @@ -258,41 +258,7 @@ func (c *cmd) enhance(file io.Reader) (SnapshotInfo, error) { info.TotalSize = cr.read info.Stats[msg] = s - if c.detailed { - if s.Name == "KVS" { - switch val := val.(type) { - case map[string]interface{}: - for k, v := range val { - if k == "Key" { - // check for whether a filter is specified. if it is, skip - // any keys that don't match. - if len(c.filter) > 0 && !strings.HasPrefix(v.(string), c.filter) { - break - } - - split := strings.Split(v.(string), "/") - - // handle the situation where the key is shorter than - // the specified depth. - actualDepth := c.depth - if c.depth > len(split) { - actualDepth = len(split) - } - prefix := strings.Join(split[0:actualDepth], "/") - kvs := info.StatsKV[prefix] - if kvs.Name == "" { - kvs.Name = prefix - } - - kvs.Sum += size - kvs.Count++ - info.TotalSizeKV += size - info.StatsKV[prefix] = kvs - } - } - } - } - } + c.kvEnhance(s.Name, val, size, &info) return nil } @@ -303,6 +269,49 @@ func (c *cmd) enhance(file io.Reader) (SnapshotInfo, error) { } +func (c *cmd) kvEnhance(keyType string, val interface{}, size int, info *SnapshotInfo) { + if c.detailed { + if keyType != "KVS" { + return + } + + // have to coerce this into a usable type here or this won't work + keyVal := val.(map[string]interface{}) + for k, v := range keyVal { + // we only care about the entry on the key specifically + // related to the key name, so skip all others + if k != "Key" { + continue + } + + // check for whether a filter is specified. if it is, skip + // any keys that don't match. + if len(c.filter) > 0 && !strings.HasPrefix(v.(string), c.filter) { + break + } + + split := strings.Split(v.(string), "/") + + // handle the situation where the key is shorter than + // the specified depth. + actualDepth := c.depth + if c.depth > len(split) { + actualDepth = len(split) + } + prefix := strings.Join(split[0:actualDepth], "/") + kvs := info.StatsKV[prefix] + if kvs.Name == "" { + kvs.Name = prefix + } + + kvs.Sum += size + kvs.Count++ + info.TotalSizeKV += size + info.StatsKV[prefix] = kvs + } + } +} + func (c *cmd) Synopsis() string { return synopsis }