From 8063fc9b2d4f1393e7932ef4c03b20094da3cb02 Mon Sep 17 00:00:00 2001 From: Magnus Kulke Date: Tue, 24 May 2016 17:34:28 +0200 Subject: [PATCH] Added json output option. --- contrib/analyze-local-images/README.md | 12 +++ contrib/analyze-local-images/main.go | 135 +++++++++++++++++-------- 2 files changed, 107 insertions(+), 40 deletions(-) diff --git a/contrib/analyze-local-images/README.md b/contrib/analyze-local-images/README.md index d76b012f..4cc28043 100644 --- a/contrib/analyze-local-images/README.md +++ b/contrib/analyze-local-images/README.md @@ -28,4 +28,16 @@ analyze-local-images -endpoint "http://:6060" -my-address " 0 { err = analyzeLayer(endpoint, tmpPath+"/"+layerIDs[i]+"/layer.tar", layerIDs[i], layerIDs[i-1]) @@ -200,14 +268,18 @@ func AnalyzeLocalImage(imageName string, minSeverity types.Priority, endpoint, m } // Get vulnerabilities. - log.Println("Retrieving image's vulnerabilities") + if !jsonOutput { + log.Println("Retrieving image's vulnerabilities") + } layer, err := getLayer(endpoint, layerIDs[len(layerIDs)-1]) if err != nil { return fmt.Errorf("Could not get layer information: %s", err) } // Print report. - fmt.Printf("Clair report for image %s (%s)\n", imageName, time.Now().UTC()) + if !jsonOutput { + fmt.Printf("Clair report for image %s (%s)\n", imageName, time.Now().UTC()) + } if len(layer.Features) == 0 { fmt.Printf("%s No features have been detected in the image. This usually means that the image isn't supported by Clair.\n", color.YellowString("NOTE:")) @@ -241,35 +313,18 @@ func AnalyzeLocalImage(imageName string, minSeverity types.Priority, endpoint, m By(priority).Sort(vulnerabilities) - for _, vulnerabilityInfo := range vulnerabilities { - vulnerability := vulnerabilityInfo.vulnerability - feature := vulnerabilityInfo.feature - severity := vulnerabilityInfo.severity - - fmt.Printf("%s (%s)\n", vulnerability.Name, coloredSeverity(severity)) - - if vulnerability.Description != "" { - fmt.Printf("%s\n\n", text.Indent(text.Wrap(vulnerability.Description, 80), "\t")) - } - - fmt.Printf("\tPackage: %s @ %s\n", feature.Name, feature.Version) - - if vulnerability.FixedBy != "" { - fmt.Printf("\tFixed version: %s\n", vulnerability.FixedBy) - } - - if vulnerability.Link != "" { - fmt.Printf("\tLink: %s\n", vulnerability.Link) - } - - fmt.Printf("\tLayer: %s\n", feature.AddedBy) - fmt.Println("") + if jsonOutput == true { + displayJson(vulnerabilities) + } else { + displayText(vulnerabilities) } - if isSafe { - fmt.Printf("%s No vulnerabilities were detected in your image\n", color.GreenString("Success!")) - } else if !hasVisibleVulnerabilities { - fmt.Printf("%s No vulnerabilities matching the minimum severity level were detected in your image\n", color.YellowString("NOTE:")) + if !jsonOutput { + if isSafe { + fmt.Printf("%s No vulnerabilities were detected in your image\n", color.GreenString("Success!")) + } else if !hasVisibleVulnerabilities { + fmt.Printf("%s No vulnerabilities matching the minimum severity level were detected in your image\n", color.YellowString("NOTE:")) + } } return nil