Skip to content

Commit

Permalink
Merge pull request #45 from thushan/no-report-optino
Browse files Browse the repository at this point in the history
Remove report and add option for no-reports.
  • Loading branch information
thushan authored Dec 16, 2023
2 parents 1eed39b + cde6bd7 commit 6478336
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 38 deletions.
1 change: 1 addition & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func init() {
flags.BoolVarP(&af.Verbose, "verbose", "", false, "Run in verbose mode")
flags.BoolVarP(&af.Profile, "profile", "", false, "Enable Go Profiler - see localhost:1984/debug/pprof")
flags.BoolVarP(&af.HideProgress, "no-progress", "", false, "Disable progress updates")
flags.BoolVarP(&af.HideOutput, "no-output", "", false, "Disable report output")
flags.BoolVarP(&af.ShowNerdStats, "nerd-stats", "", false, "Show nerd stats")
flags.BoolVarP(&af.ShowVersion, "version", "v", false, "Show version information")
flags.StringVarP(&af.OutputFile, "output-file", "o", generateOutputFile(), "Export analysis as JSON (generated automatically otherwise, ./report-*.json)")
Expand Down
29 changes: 15 additions & 14 deletions internal/smash/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (

"golang.org/x/term"

"github.com/thushan/smash/internal/report"

"github.com/pterm/pterm"
"github.com/thushan/smash/internal/theme"

Expand All @@ -28,14 +26,14 @@ type App struct {
Flags *Flags
Session *AppSession
Runtime *AppRuntime
Summary *report.RunSummary
Summary *RunSummary
Args []string
Locations []string
}
type AppSession struct {
Dupes *xsync.MapOf[string, *report.DuplicateFiles]
Dupes *xsync.MapOf[string, *DuplicateFiles]
Fails *xsync.MapOf[string, error]
Empty *report.EmptyFiles
Empty *EmptyFiles
StartTime int64
EndTime int64
}
Expand Down Expand Up @@ -63,10 +61,10 @@ func (app *App) Run() error {
}

app.Session = &AppSession{
Dupes: xsync.NewMapOf[string, *report.DuplicateFiles](),
Dupes: xsync.NewMapOf[string, *DuplicateFiles](),
Fails: xsync.NewMapOf[string, error](),
Empty: &report.EmptyFiles{
Files: []report.SmashFile{},
Empty: &EmptyFiles{
Files: []File{},
RWMutex: sync.RWMutex{},
},
StartTime: time.Now().UnixNano(),
Expand Down Expand Up @@ -161,7 +159,7 @@ func (app *App) Exec() error {
}
_, _ = session.Fails.LoadOrStore(file.Path, err)
} else {
report.SummariseSmashedFile(stats, file, elapsedMs, session.Dupes, session.Empty)
SummariseSmashedFile(stats, file, elapsedMs, session.Dupes, session.Empty)
}
}
}()
Expand Down Expand Up @@ -190,14 +188,14 @@ func (app *App) Exec() error {

endStats := nerdstats.Snapshot()

report.PrintRunSummary(*app.Summary, app.Flags.IgnoreEmpty)
PrintRunSummary(*app.Summary, app.Flags)

if app.Flags.ShowNerdStats {
theme.StyleHeading.Println("---| Nerd Stats")
report.PrintNerdStats(startStats, "> Initial")
report.PrintNerdStats(midStats, "> Post-Analysis")
report.PrintNerdStats(exportStats, "> Post-Summary")
report.PrintNerdStats(endStats, "> Post-Report")
PrintNerdStats(startStats, "> Initial")
PrintNerdStats(midStats, "> Post-Analysis")
PrintNerdStats(exportStats, "> Post-Summary")
PrintNerdStats(endStats, "> Post-Report")
}
return nil
}
Expand Down Expand Up @@ -228,6 +226,9 @@ func (app *App) checkTerminal() {
}

func (app *App) ExportReport() {
if app.Flags.HideOutput {
return
}
if app.Flags.OutputFile == "" {
theme.Warn.Println("Could not output report.")
return
Expand Down
11 changes: 5 additions & 6 deletions internal/smash/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/puzpuzpuz/xsync/v3"
"github.com/thushan/smash/internal/report"
"github.com/thushan/smash/pkg/analysis"
)

Expand Down Expand Up @@ -126,10 +125,10 @@ func summariseSmashedFails(fails *xsync.MapOf[string, error]) []ReportFailSummar
return summary
}

func transformDupes(duplicates *xsync.MapOf[string, *report.DuplicateFiles]) []ReportDuplicateSummary {
func transformDupes(duplicates *xsync.MapOf[string, *DuplicateFiles]) []ReportDuplicateSummary {
dupes := make([]ReportDuplicateSummary, duplicates.Size())
var index = 0
duplicates.Range(func(hash string, dupe *report.DuplicateFiles) bool {
duplicates.Range(func(hash string, dupe *DuplicateFiles) bool {
root := dupe.Files[0]
dupes[index] = ReportDuplicateSummary{
ReportFileSummary: summariseSmashedFile(root),
Expand All @@ -141,14 +140,14 @@ func transformDupes(duplicates *xsync.MapOf[string, *report.DuplicateFiles]) []R
return dupes
}

func summariseSmashedFiles(files []report.SmashFile) []ReportFileSummary {
func summariseSmashedFiles(files []File) []ReportFileSummary {
summary := make([]ReportFileSummary, len(files))
for i, file := range files {
summary[i] = summariseSmashedFile(file)
}
return summary
}
func summariseSmashedFile(file report.SmashFile) ReportFileSummary {
func summariseSmashedFile(file File) ReportFileSummary {
return ReportFileSummary{
Filename: file.Filename,
Location: file.Location,
Expand All @@ -158,7 +157,7 @@ func summariseSmashedFile(file report.SmashFile) ReportFileSummary {
FullHash: file.FullHash,
}
}
func summariseRunSummary(summary *report.RunSummary) ReportSummary {
func summariseRunSummary(summary *RunSummary) ReportSummary {
return ReportSummary{
TopFiles: transformTopFiles(summary.TopFiles),
DuplicateFileSize: summary.DuplicateFileSize,
Expand Down
1 change: 1 addition & 0 deletions internal/smash/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Flags struct {
Silent bool `yaml:"silent"`
HideTopList bool `yaml:"no-top-list"`
HideProgress bool `yaml:"no-progress"`
HideOutput bool `yaml:"no-output"`
Profile bool `yaml:"profile"`
Verbose bool `yaml:"verbose"`
}
Expand Down
11 changes: 5 additions & 6 deletions internal/smash/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package smash

import (
"github.com/dustin/go-humanize"
"github.com/thushan/smash/internal/report"
"github.com/thushan/smash/pkg/analysis"

"github.com/thushan/smash/internal/theme"
Expand Down Expand Up @@ -43,7 +42,7 @@ func (app *App) PrintRunAnalysis(ignoreEmptyFiles bool) {

if app.Flags.ShowDuplicates {
theme.StyleSubHeading.Println("---[ All Duplicates ]---")
duplicates.Range(func(hash string, files *report.DuplicateFiles) bool {
duplicates.Range(func(hash string, files *DuplicateFiles) bool {
displayFiles(files.Files)
return true
})
Expand All @@ -57,7 +56,7 @@ func (app *App) PrintRunAnalysis(ignoreEmptyFiles bool) {

}

func displayFiles(files []report.SmashFile) {
func displayFiles(files []File) {
duplicateFiles := len(files) - 1
if duplicateFiles != 0 {
root := files[0]
Expand All @@ -74,7 +73,7 @@ func displayFiles(files []report.SmashFile) {
}
}

func printSmashHits(files []report.SmashFile) {
func printSmashHits(files []File) {
lastIndex := len(files) - 1
for index, file := range files {
var subTree string
Expand All @@ -101,7 +100,7 @@ func (app *App) generateRunSummary(totalFiles int64) {
totalFailFileCount := int64(session.Fails.Size())
totalEmptyFileCount := int64(len(emptyFiles))

duplicates.Range(func(hash string, df *report.DuplicateFiles) bool {
duplicates.Range(func(hash string, df *DuplicateFiles) bool {
files := df.Files
duplicateFiles := len(files) - 1
if duplicateFiles == 0 {
Expand All @@ -117,7 +116,7 @@ func (app *App) generateRunSummary(totalFiles int64) {
}
return true
})
summary := report.RunSummary{
summary := RunSummary{
TopFiles: topFiles.All(),
TotalFiles: totalFiles,
TotalFileErrors: totalFailFileCount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package report
package smash

import (
"github.com/dustin/go-humanize"
Expand Down
12 changes: 6 additions & 6 deletions internal/report/smashfile.go → internal/smash/smashfile.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package report
package smash

import (
"encoding/hex"
Expand All @@ -12,7 +12,7 @@ import (
"github.com/thushan/smash/pkg/slicer"
)

type SmashFile struct {
type File struct {
Filename string
Location string
Path string
Expand All @@ -25,16 +25,16 @@ type SmashFile struct {
EmptyFile bool
}
type DuplicateFiles struct {
Files []SmashFile
Files []File
sync.RWMutex
}
type EmptyFiles struct {
Files []SmashFile
Files []File
sync.RWMutex
}

func SummariseSmashedFile(stats slicer.SlicerStats, ffs *indexer.FileFS, ms int64, duplicates *xsync.MapOf[string, *DuplicateFiles], empty *EmptyFiles) {
file := SmashFile{
file := File{
Hash: hex.EncodeToString(stats.Hash),
Filename: ffs.Name,
Location: ffs.Location,
Expand All @@ -51,7 +51,7 @@ func SummariseSmashedFile(stats slicer.SlicerStats, ffs *indexer.FileFS, ms int6
empty.Unlock()
} else {
dupes, _ := duplicates.LoadOrStore(file.Hash, &DuplicateFiles{
Files: []SmashFile{},
Files: []File{},
RWMutex: sync.RWMutex{},
})
dupes.Lock()
Expand Down
8 changes: 4 additions & 4 deletions internal/report/summary.go → internal/smash/summary.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package report
package smash

import (
"fmt"
Expand All @@ -23,7 +23,7 @@ type RunSummary struct {
DuplicateFiles int64
}

func PrintRunSummary(rs RunSummary, ignoreEmptyFiles bool) {
func PrintRunSummary(rs RunSummary, flags *Flags) {
theme.StyleHeading.Println("---| Analysis Summary")

theme.Println(writeCategory("Total Time:"), theme.ColourTime(calcTotalTime(rs.ElapsedTime)))
Expand All @@ -33,13 +33,13 @@ func PrintRunSummary(rs RunSummary, ignoreEmptyFiles bool) {
theme.Println(writeCategory("Total Skipped:"), theme.ColourError(rs.TotalFileErrors))
}
theme.Println(writeCategory("Total Duplicates:"), theme.ColourNumber(rs.DuplicateFiles))
if !ignoreEmptyFiles && rs.EmptyFiles > 0 {
if !flags.IgnoreEmpty && rs.EmptyFiles > 0 {
theme.Println(writeCategory("Total Empty Files:"), theme.ColourNumber(rs.EmptyFiles))
}
if rs.DuplicateFileSize > 0 {
theme.Println(writeCategory("Space Reclaimable:"), theme.ColourFileSizeA(rs.DuplicateFileSizeF), "(approx)")
}
if rs.ReportFilename != "" {
if !flags.HideOutput && rs.ReportFilename != "" {
filename := filepath.Clean(rs.ReportFilename)
reportUri := theme.Hyperlink("file://"+filename, filename)
theme.Println(writeCategory("Analysis Report:"), theme.StyleUrl(reportUri), "(json)")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package report
package smash

import (
"strings"
Expand Down

0 comments on commit 6478336

Please sign in to comment.