mirror of https://github.com/status-im/consul.git
Refactor to reduce how many vars are being passed around
This commit is contained in:
parent
1a50aa023a
commit
3ee20d500a
|
@ -15,7 +15,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Formatter interface {
|
type Formatter interface {
|
||||||
Format(*OutputFormat, bool) (string, error)
|
Format(*OutputFormat) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSupportedFormats() []string {
|
func GetSupportedFormats() []string {
|
||||||
|
@ -38,7 +38,7 @@ func NewFormatter(format string) (Formatter, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ *prettyFormatter) Format(info *OutputFormat, detailed bool) (string, error) {
|
func (_ *prettyFormatter) Format(info *OutputFormat) (string, error) {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
tw := tabwriter.NewWriter(&b, 8, 8, 6, ' ', 0)
|
tw := tabwriter.NewWriter(&b, 8, 8, 6, ' ', 0)
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ func (_ *prettyFormatter) Format(info *OutputFormat, detailed bool) (string, err
|
||||||
fmt.Fprintf(tw, "\n %s\t%s\t%s\t", "----", "----", "----")
|
fmt.Fprintf(tw, "\n %s\t%s\t%s\t", "----", "----", "----")
|
||||||
fmt.Fprintf(tw, "\n Total\t\t%s\t", ByteSize(uint64(info.TotalSize)))
|
fmt.Fprintf(tw, "\n Total\t\t%s\t", ByteSize(uint64(info.TotalSize)))
|
||||||
|
|
||||||
if detailed {
|
if len(info.StatsKV) > 0 {
|
||||||
fmt.Fprintf(tw, "\n")
|
fmt.Fprintf(tw, "\n")
|
||||||
fmt.Fprintln(tw, "\n Key Name\tCount\tSize\t")
|
fmt.Fprintln(tw, "\n Key Name\tCount\tSize\t")
|
||||||
fmt.Fprintf(tw, " %s\t%s\t%s\t", "----", "----", "----")
|
fmt.Fprintf(tw, " %s\t%s\t%s\t", "----", "----", "----")
|
||||||
|
@ -82,7 +82,7 @@ func newJSONFormatter() Formatter {
|
||||||
return &jsonFormatter{}
|
return &jsonFormatter{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ *jsonFormatter) Format(info *OutputFormat, detailed bool) (string, error) {
|
func (_ *jsonFormatter) Format(info *OutputFormat) (string, error) {
|
||||||
b, err := json.MarshalIndent(info, "", " ")
|
b, err := json.MarshalIndent(info, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Failed to marshal original snapshot stats: %v", err)
|
return "", fmt.Errorf("Failed to marshal original snapshot stats: %v", err)
|
||||||
|
|
|
@ -63,6 +63,16 @@ type MetadataInfo struct {
|
||||||
Version raft.SnapshotVersion
|
Version raft.SnapshotVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SnapshotInfo is used for passing snapshot stat
|
||||||
|
// information between functions
|
||||||
|
type SnapshotInfo struct {
|
||||||
|
Meta MetadataInfo
|
||||||
|
Stats map[structs.MessageType]typeStats
|
||||||
|
StatsKV map[string]typeStats
|
||||||
|
TotalSize int
|
||||||
|
TotalSizeKV int
|
||||||
|
}
|
||||||
|
|
||||||
// OutputFormat is used for passing information
|
// OutputFormat is used for passing information
|
||||||
// through the formatter
|
// through the formatter
|
||||||
type OutputFormat struct {
|
type OutputFormat struct {
|
||||||
|
@ -114,7 +124,7 @@ func (c *cmd) Run(args []string) int {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
stats, statsKV, totalSize, totalSizeKV, err := enhance(readFile, c.detailed, c.depth, c.filter)
|
info, err := c.enhance(readFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.UI.Error(fmt.Sprintf("Error extracting snapshot data: %s", err))
|
c.UI.Error(fmt.Sprintf("Error extracting snapshot data: %s", err))
|
||||||
return 1
|
return 1
|
||||||
|
@ -135,17 +145,17 @@ func (c *cmd) Run(args []string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Restructures stats given above to be human readable
|
//Restructures stats given above to be human readable
|
||||||
formattedStats, formattedStatsKV := generatetypeStats(stats, statsKV, c.detailed)
|
formattedStats, formattedStatsKV := generatetypeStats(info)
|
||||||
|
|
||||||
in := &OutputFormat{
|
in := &OutputFormat{
|
||||||
Meta: metaformat,
|
Meta: metaformat,
|
||||||
Stats: formattedStats,
|
Stats: formattedStats,
|
||||||
StatsKV: formattedStatsKV,
|
StatsKV: formattedStatsKV,
|
||||||
TotalSize: totalSize,
|
TotalSize: info.TotalSize,
|
||||||
TotalSizeKV: totalSizeKV,
|
TotalSizeKV: info.TotalSizeKV,
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := formatter.Format(in, c.detailed)
|
out, err := formatter.Format(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.UI.Error(err.Error())
|
c.UI.Error(err.Error())
|
||||||
return 1
|
return 1
|
||||||
|
@ -161,10 +171,10 @@ type typeStats struct {
|
||||||
Count int
|
Count int
|
||||||
}
|
}
|
||||||
|
|
||||||
func generatetypeStats(info map[structs.MessageType]typeStats, kvInfo map[string]typeStats, detailed bool) ([]typeStats, []typeStats) {
|
func generatetypeStats(info SnapshotInfo) ([]typeStats, []typeStats) {
|
||||||
ss := make([]typeStats, 0, len(info))
|
ss := make([]typeStats, 0, len(info.Stats))
|
||||||
|
|
||||||
for _, s := range info {
|
for _, s := range info.Stats {
|
||||||
ss = append(ss, s)
|
ss = append(ss, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,10 +188,10 @@ func generatetypeStats(info map[structs.MessageType]typeStats, kvInfo map[string
|
||||||
return ss[i].Sum > ss[j].Sum
|
return ss[i].Sum > ss[j].Sum
|
||||||
})
|
})
|
||||||
|
|
||||||
if detailed {
|
if len(info.StatsKV) > 0 {
|
||||||
ks := make([]typeStats, 0, len(kvInfo))
|
ks := make([]typeStats, 0, len(info.StatsKV))
|
||||||
|
|
||||||
for _, s := range kvInfo {
|
for _, s := range info.StatsKV {
|
||||||
ks = append(ks, s)
|
ks = append(ks, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,15 +228,17 @@ func (r *countingReader) Read(p []byte) (n int, err error) {
|
||||||
|
|
||||||
// enhance utilizes ReadSnapshot to populate the struct with
|
// enhance utilizes ReadSnapshot to populate the struct with
|
||||||
// all of the snapshot's itemized data
|
// all of the snapshot's itemized data
|
||||||
func enhance(file io.Reader, detailed bool, depth int, filter string) (map[structs.MessageType]typeStats, map[string]typeStats, int, int, error) {
|
func (c *cmd) enhance(file io.Reader) (SnapshotInfo, error) {
|
||||||
stats := make(map[structs.MessageType]typeStats)
|
info := SnapshotInfo{
|
||||||
statsKV := make(map[string]typeStats)
|
Stats: make(map[structs.MessageType]typeStats),
|
||||||
|
StatsKV: make(map[string]typeStats),
|
||||||
|
TotalSize: 0,
|
||||||
|
TotalSizeKV: 0,
|
||||||
|
}
|
||||||
cr := &countingReader{wrappedReader: file}
|
cr := &countingReader{wrappedReader: file}
|
||||||
totalSize := 0
|
|
||||||
totalSizeKV := 0
|
|
||||||
handler := func(header *fsm.SnapshotHeader, msg structs.MessageType, dec *codec.Decoder) error {
|
handler := func(header *fsm.SnapshotHeader, msg structs.MessageType, dec *codec.Decoder) error {
|
||||||
name := structs.MessageType.String(msg)
|
name := structs.MessageType.String(msg)
|
||||||
s := stats[msg]
|
s := info.Stats[msg]
|
||||||
if s.Name == "" {
|
if s.Name == "" {
|
||||||
s.Name = name
|
s.Name = name
|
||||||
}
|
}
|
||||||
|
@ -237,13 +249,13 @@ func enhance(file io.Reader, detailed bool, depth int, filter string) (map[struc
|
||||||
return fmt.Errorf("failed to decode msg type %v, error %v", name, err)
|
return fmt.Errorf("failed to decode msg type %v, error %v", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
size := cr.read - totalSize
|
size := cr.read - info.TotalSize
|
||||||
s.Sum += size
|
s.Sum += size
|
||||||
s.Count++
|
s.Count++
|
||||||
totalSize = cr.read
|
info.TotalSize = cr.read
|
||||||
stats[msg] = s
|
info.Stats[msg] = s
|
||||||
|
|
||||||
if detailed {
|
if c.detailed {
|
||||||
if s.Name == "KVS" {
|
if s.Name == "KVS" {
|
||||||
switch val := val.(type) {
|
switch val := val.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
|
@ -251,7 +263,7 @@ func enhance(file io.Reader, detailed bool, depth int, filter string) (map[struc
|
||||||
if k == "Key" {
|
if k == "Key" {
|
||||||
// check for whether a filter is specified. if it is, skip
|
// check for whether a filter is specified. if it is, skip
|
||||||
// any keys that don't match.
|
// any keys that don't match.
|
||||||
if len(filter) > 0 && !strings.HasPrefix(v.(string), filter) {
|
if len(c.filter) > 0 && !strings.HasPrefix(v.(string), c.filter) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,20 +271,20 @@ func enhance(file io.Reader, detailed bool, depth int, filter string) (map[struc
|
||||||
|
|
||||||
// handle the situation where the key is shorter than
|
// handle the situation where the key is shorter than
|
||||||
// the specified depth.
|
// the specified depth.
|
||||||
actualDepth := depth
|
actualDepth := c.depth
|
||||||
if depth > len(split) {
|
if c.depth > len(split) {
|
||||||
actualDepth = len(split)
|
actualDepth = len(split)
|
||||||
}
|
}
|
||||||
prefix := strings.Join(split[0:actualDepth], "/")
|
prefix := strings.Join(split[0:actualDepth], "/")
|
||||||
kvs := statsKV[prefix]
|
kvs := info.StatsKV[prefix]
|
||||||
if kvs.Name == "" {
|
if kvs.Name == "" {
|
||||||
kvs.Name = prefix
|
kvs.Name = prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
kvs.Sum += size
|
kvs.Sum += size
|
||||||
kvs.Count++
|
kvs.Count++
|
||||||
totalSizeKV += size
|
info.TotalSizeKV += size
|
||||||
statsKV[prefix] = kvs
|
info.StatsKV[prefix] = kvs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -282,9 +294,9 @@ func enhance(file io.Reader, detailed bool, depth int, filter string) (map[struc
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := fsm.ReadSnapshot(cr, handler); err != nil {
|
if err := fsm.ReadSnapshot(cr, handler); err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return info, err
|
||||||
}
|
}
|
||||||
return stats, statsKV, totalSize, totalSizeKV, nil
|
return info, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue