From c60d13e539f1dcd0a544d3edad4ab4b0ce69dd77 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 9 May 2017 17:24:40 -0400 Subject: [PATCH] Fix cyclomatic complexity of two formatters Signed-off-by: Daniel Nephin --- cli/command/formatter/disk_usage.go | 81 +++++++++++++++-------------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/cli/command/formatter/disk_usage.go b/cli/command/formatter/disk_usage.go index 9a3b4b9fe6..1713a9cde7 100644 --- a/cli/command/formatter/disk_usage.go +++ b/cli/command/formatter/disk_usage.go @@ -65,49 +65,52 @@ reclaimable: {{.Reclaimable}} } func (ctx *DiskUsageContext) Write() (err error) { - if !ctx.Verbose { - ctx.buffer = bytes.NewBufferString("") - ctx.preFormat() - - tmpl, err := ctx.parseFormat() - if err != nil { - return err - } - - err = ctx.contextFormat(tmpl, &diskUsageImagesContext{ - totalSize: ctx.LayersSize, - images: ctx.Images, - }) - if err != nil { - return err - } - err = ctx.contextFormat(tmpl, &diskUsageContainersContext{ - containers: ctx.Containers, - }) - if err != nil { - return err - } - - err = ctx.contextFormat(tmpl, &diskUsageVolumesContext{ - volumes: ctx.Volumes, - }) - if err != nil { - return err - } - - diskUsageContainersCtx := diskUsageContainersContext{containers: []*types.Container{}} - diskUsageContainersCtx.header = map[string]string{ - "Type": typeHeader, - "TotalCount": totalHeader, - "Active": activeHeader, - "Size": sizeHeader, - "Reclaimable": reclaimableHeader, - } - ctx.postFormat(tmpl, &diskUsageContainersCtx) + if ctx.Verbose { + return ctx.verboseWrite() + } + ctx.buffer = bytes.NewBufferString("") + ctx.preFormat() + tmpl, err := ctx.parseFormat() + if err != nil { return err } + err = ctx.contextFormat(tmpl, &diskUsageImagesContext{ + totalSize: ctx.LayersSize, + images: ctx.Images, + }) + if err != nil { + return err + } + err = ctx.contextFormat(tmpl, &diskUsageContainersContext{ + containers: ctx.Containers, + }) + if err != nil { + return err + } + + err = ctx.contextFormat(tmpl, &diskUsageVolumesContext{ + volumes: ctx.Volumes, + }) + if err != nil { + return err + } + + diskUsageContainersCtx := diskUsageContainersContext{containers: []*types.Container{}} + diskUsageContainersCtx.header = map[string]string{ + "Type": typeHeader, + "TotalCount": totalHeader, + "Active": activeHeader, + "Size": sizeHeader, + "Reclaimable": reclaimableHeader, + } + ctx.postFormat(tmpl, &diskUsageContainersCtx) + + return err +} + +func (ctx *DiskUsageContext) verboseWrite() (err error) { // First images tmpl, err := ctx.startSubsection(defaultDiskUsageImageTableFormat) if err != nil {