-
Notifications
You must be signed in to change notification settings - Fork 23
/
report.go
109 lines (100 loc) · 2.95 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strconv"
"time"
"github.com/m-manu/go-find-duplicates/entity"
"github.com/m-manu/go-find-duplicates/fmte"
)
const bytesPerLineGuess = 500
func reportDuplicates(duplicates *entity.DigestToFiles, outputMode string, allFiles entity.FilePathToMeta,
runID string, reportFile io.Writer) error {
var err error
if outputMode == entity.OutputModeStdOut || outputMode == entity.OutputModeTextFile {
reportBytes := getReportAsText(duplicates)
if outputMode == entity.OutputModeTextFile {
createTextFileReport(reportFile, reportBytes)
} else if outputMode == entity.OutputModeStdOut {
printReportToStdOut(runID, reportBytes)
}
} else if outputMode == entity.OutputModeCsvFile {
err = createCsvReport(duplicates, allFiles, reportFile)
} else if outputMode == entity.OutputModeJSON {
err = createJSONReport(duplicates, reportFile)
}
return err
}
func createTextFileReport(reportFile io.Writer, report bytes.Buffer) {
_, rcErr := reportFile.Write(report.Bytes())
if rcErr != nil {
fmte.PrintfErr("error while write to report file: %+v\n", rcErr)
os.Exit(exitCodeErrorCreatingReport)
}
}
func getReportAsText(duplicates *entity.DigestToFiles) bytes.Buffer {
var bb bytes.Buffer
bb.Grow(duplicates.Size() * bytesPerLineGuess)
for iter := duplicates.Iterator(); iter.HasNext(); {
digest, paths := iter.Next()
sort.Strings(paths)
bb.WriteString(fmt.Sprintf("%s: %d duplicate(s)\n", digest, len(paths)-1))
for _, path := range paths {
bb.WriteString(fmt.Sprintf("\t%s\n", path))
}
}
return bb
}
func printReportToStdOut(runID string, bb bytes.Buffer) {
fmt.Printf(`
==========================
Report (run id %s)
==========================
`, runID)
fmt.Printf(bb.String())
}
func createCsvReport(duplicates *entity.DigestToFiles, allFiles entity.FilePathToMeta, reportFile io.Writer) error {
var bb bytes.Buffer
bb.Grow(duplicates.Size() * bytesPerLineGuess)
cf := csv.NewWriter(&bb)
_ = cf.Write([]string{"file hash", "file size", "last modified", "file path"})
for iter := duplicates.Iterator(); iter.HasNext(); {
digest, paths := iter.Next()
for _, path := range paths {
_ = cf.Write([]string{
digest.FileHash,
strconv.FormatInt(digest.FileSize, 10),
time.Unix(allFiles[path].ModifiedTimestamp, 0).Format("02-Jan-2006 03:04:05 PM"),
path,
})
}
}
cf.Flush()
_, err := reportFile.Write(bb.Bytes())
return err
}
func createJSONReport(duplicates *entity.DigestToFiles, reportFile io.Writer) error {
type duplicateFile struct {
entity.FileDigest
Paths []string `json:"paths"`
}
var duplicatesToMarshall []duplicateFile
for iter := duplicates.Iterator(); iter.HasNext(); {
digest, paths := iter.Next()
duplicatesToMarshall = append(duplicatesToMarshall, duplicateFile{
*digest,
paths,
})
}
jsonBytes, err := json.Marshal(duplicatesToMarshall)
if err != nil {
return err
}
_, err = reportFile.Write(jsonBytes)
return err
}