From cd35e666f87b736704669dcda2ffa4db81a83e80 Mon Sep 17 00:00:00 2001 From: Thushan Fernando Date: Sat, 16 Dec 2023 13:38:35 +1100 Subject: [PATCH 1/2] Remove report and add option for no-reports. --- internal/cli/cli.go | 1 + internal/smash/app.go | 29 +++++++++++----------- internal/smash/export.go | 11 ++++---- internal/smash/flags.go | 1 + internal/smash/formatter.go | 11 ++++---- internal/{report => smash}/nerdstats.go | 2 +- internal/{report => smash}/smashfile.go | 2 +- internal/{report => smash}/summary.go | 8 +++--- internal/{report => smash}/summary_test.go | 2 +- 9 files changed, 34 insertions(+), 33 deletions(-) rename internal/{report => smash}/nerdstats.go (98%) rename internal/{report => smash}/smashfile.go (98%) rename internal/{report => smash}/summary.go (92%) rename internal/{report => smash}/summary_test.go (97%) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index f10a4e5..fbf0567 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -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)") diff --git a/internal/smash/app.go b/internal/smash/app.go index 731f14b..02befa2 100644 --- a/internal/smash/app.go +++ b/internal/smash/app.go @@ -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" @@ -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 } @@ -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: []SmashFile{}, RWMutex: sync.RWMutex{}, }, StartTime: time.Now().UnixNano(), @@ -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) } } }() @@ -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 } @@ -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 diff --git a/internal/smash/export.go b/internal/smash/export.go index f19fbce..9cc4ad4 100644 --- a/internal/smash/export.go +++ b/internal/smash/export.go @@ -8,7 +8,6 @@ import ( "time" "github.com/puzpuzpuz/xsync/v3" - "github.com/thushan/smash/internal/report" "github.com/thushan/smash/pkg/analysis" ) @@ -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), @@ -141,14 +140,14 @@ func transformDupes(duplicates *xsync.MapOf[string, *report.DuplicateFiles]) []R return dupes } -func summariseSmashedFiles(files []report.SmashFile) []ReportFileSummary { +func summariseSmashedFiles(files []SmashFile) []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 SmashFile) ReportFileSummary { return ReportFileSummary{ Filename: file.Filename, Location: file.Location, @@ -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, diff --git a/internal/smash/flags.go b/internal/smash/flags.go index 8f37608..2d56d6c 100644 --- a/internal/smash/flags.go +++ b/internal/smash/flags.go @@ -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"` } diff --git a/internal/smash/formatter.go b/internal/smash/formatter.go index 3bfebea..a144cb3 100644 --- a/internal/smash/formatter.go +++ b/internal/smash/formatter.go @@ -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" @@ -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 }) @@ -57,7 +56,7 @@ func (app *App) PrintRunAnalysis(ignoreEmptyFiles bool) { } -func displayFiles(files []report.SmashFile) { +func displayFiles(files []SmashFile) { duplicateFiles := len(files) - 1 if duplicateFiles != 0 { root := files[0] @@ -74,7 +73,7 @@ func displayFiles(files []report.SmashFile) { } } -func printSmashHits(files []report.SmashFile) { +func printSmashHits(files []SmashFile) { lastIndex := len(files) - 1 for index, file := range files { var subTree string @@ -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 { @@ -117,7 +116,7 @@ func (app *App) generateRunSummary(totalFiles int64) { } return true }) - summary := report.RunSummary{ + summary := RunSummary{ TopFiles: topFiles.All(), TotalFiles: totalFiles, TotalFileErrors: totalFailFileCount, diff --git a/internal/report/nerdstats.go b/internal/smash/nerdstats.go similarity index 98% rename from internal/report/nerdstats.go rename to internal/smash/nerdstats.go index 7c4f563..14bead9 100644 --- a/internal/report/nerdstats.go +++ b/internal/smash/nerdstats.go @@ -1,4 +1,4 @@ -package report +package smash import ( "github.com/dustin/go-humanize" diff --git a/internal/report/smashfile.go b/internal/smash/smashfile.go similarity index 98% rename from internal/report/smashfile.go rename to internal/smash/smashfile.go index 9c365c1..b9a19ba 100644 --- a/internal/report/smashfile.go +++ b/internal/smash/smashfile.go @@ -1,4 +1,4 @@ -package report +package smash import ( "encoding/hex" diff --git a/internal/report/summary.go b/internal/smash/summary.go similarity index 92% rename from internal/report/summary.go rename to internal/smash/summary.go index 6e810ea..6a3059f 100644 --- a/internal/report/summary.go +++ b/internal/smash/summary.go @@ -1,4 +1,4 @@ -package report +package smash import ( "fmt" @@ -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))) @@ -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)") diff --git a/internal/report/summary_test.go b/internal/smash/summary_test.go similarity index 97% rename from internal/report/summary_test.go rename to internal/smash/summary_test.go index eeb88e8..bb3377c 100644 --- a/internal/report/summary_test.go +++ b/internal/smash/summary_test.go @@ -1,4 +1,4 @@ -package report +package smash import ( "strings" From cde6bd7fca44437138e8d015e9eeb65986b53ea2 Mon Sep 17 00:00:00 2001 From: Thushan Fernando Date: Sat, 16 Dec 2023 13:43:05 +1100 Subject: [PATCH 2/2] call it a file. --- internal/smash/app.go | 2 +- internal/smash/export.go | 4 ++-- internal/smash/formatter.go | 4 ++-- internal/smash/smashfile.go | 10 +++++----- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/smash/app.go b/internal/smash/app.go index 02befa2..edc6d0a 100644 --- a/internal/smash/app.go +++ b/internal/smash/app.go @@ -64,7 +64,7 @@ func (app *App) Run() error { Dupes: xsync.NewMapOf[string, *DuplicateFiles](), Fails: xsync.NewMapOf[string, error](), Empty: &EmptyFiles{ - Files: []SmashFile{}, + Files: []File{}, RWMutex: sync.RWMutex{}, }, StartTime: time.Now().UnixNano(), diff --git a/internal/smash/export.go b/internal/smash/export.go index 9cc4ad4..54e7bba 100644 --- a/internal/smash/export.go +++ b/internal/smash/export.go @@ -140,14 +140,14 @@ func transformDupes(duplicates *xsync.MapOf[string, *DuplicateFiles]) []ReportDu return dupes } -func summariseSmashedFiles(files []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 SmashFile) ReportFileSummary { +func summariseSmashedFile(file File) ReportFileSummary { return ReportFileSummary{ Filename: file.Filename, Location: file.Location, diff --git a/internal/smash/formatter.go b/internal/smash/formatter.go index a144cb3..e6f486c 100644 --- a/internal/smash/formatter.go +++ b/internal/smash/formatter.go @@ -56,7 +56,7 @@ func (app *App) PrintRunAnalysis(ignoreEmptyFiles bool) { } -func displayFiles(files []SmashFile) { +func displayFiles(files []File) { duplicateFiles := len(files) - 1 if duplicateFiles != 0 { root := files[0] @@ -73,7 +73,7 @@ func displayFiles(files []SmashFile) { } } -func printSmashHits(files []SmashFile) { +func printSmashHits(files []File) { lastIndex := len(files) - 1 for index, file := range files { var subTree string diff --git a/internal/smash/smashfile.go b/internal/smash/smashfile.go index b9a19ba..cb8ff82 100644 --- a/internal/smash/smashfile.go +++ b/internal/smash/smashfile.go @@ -12,7 +12,7 @@ import ( "github.com/thushan/smash/pkg/slicer" ) -type SmashFile struct { +type File struct { Filename string Location string Path string @@ -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, @@ -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()