Skip to content

Commit

Permalink
Handle error in report.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dineshba authored and svishwanath-tw committed Oct 11, 2019
1 parent 04aa9e8 commit 75cd5b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
30 changes: 20 additions & 10 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,57 @@ const jsonFileName string = "report.json"
const htmlReportDir string = "talisman_html_report"

// GenerateReport generates a talisman scan report in html format
func GenerateReport(r *detector.DetectionResults, directory string) string {
func GenerateReport(r *detector.DetectionResults, directory string) (string, error) {

var path string
var jsonFilePath string
var homeDir string
var baseReportDirPath string

usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("error getting current user: %v", err.Error())
}
homeDir = usr.HomeDir

if directory == htmlReportDir {
path = directory
baseReportDirPath = filepath.Join(homeDir, ".talisman", htmlReportDir)
jsonFilePath = filepath.Join(path, "/data", jsonFileName)
os.MkdirAll(path, 0755)
err = utility.Dir(baseReportDirPath, htmlReportDir)
if err != nil {
generateErrorMsg()
return "", fmt.Errorf("error copying reports: %v", err)
}
} else {
path = filepath.Join(directory, "talisman_reports", "/data")
jsonFilePath = filepath.Join(path, jsonFileName)
}

os.MkdirAll(path, 0755)
err = os.MkdirAll(path, 0755)
if err != nil {
return "", fmt.Errorf("error creating path %s: %v", path, err)
}

jsonFile, err := os.Create(jsonFilePath)
defer func() {
err = jsonFile.Close()
log.Fatalf("error closing file %s: %v", jsonFilePath, err)
}()

if err != nil {
fmt.Printf("\n")
log.Fatal("Cannot create report.json file\n", err)
return "", fmt.Errorf("error creating file %s: %v", jsonFilePath, err)
}

jsonString, err := json.Marshal(r)
if err != nil {
log.Fatal("Unable to marshal JSON")
return "", fmt.Errorf("error while marshal the report: %v", err)
}
jsonFile.Write(jsonString)
jsonFile.Close()
return path
_, err = jsonFile.Write(jsonString)
if err != nil {
return "", fmt.Errorf("error while writing report to file: %v", err)
}
return path, nil
}

func generateErrorMsg() {
Expand All @@ -69,5 +80,4 @@ func generateErrorMsg() {
color.Red("Failed: Unable to perform Scan")
fmt.Printf("\n")
log.Fatalln("Run Status: Failed")
fmt.Printf("\n")
}
7 changes: 6 additions & 1 deletion runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"os"
"talisman/checksumcalculator"
"talisman/detector"
Expand Down Expand Up @@ -48,7 +49,11 @@ func (r *Runner) Scan(reportDirectory string) int {
additions := scanner.GetAdditions()
ignores := detector.TalismanRCIgnore{}
detector.DefaultChain().Test(additions, ignores, r.results)
reportsPath := report.GenerateReport(r.results, reportDirectory)
reportsPath, err := report.GenerateReport(r.results, reportDirectory)
if err != nil {
log.Printf("error while generating report: %v", err)
return CompletedWithErrors
}
fmt.Printf("\nPlease check '%s' folder for the talisman scan report\n", reportsPath)
fmt.Printf("\n")
return r.exitStatus()
Expand Down

0 comments on commit 75cd5b2

Please sign in to comment.