Skip to content

Commit

Permalink
proper output of report json blob.
Browse files Browse the repository at this point in the history
  • Loading branch information
thushan committed Dec 16, 2023
1 parent 5f2b72e commit 1703c8b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ go.work
# temporary files
report.json
analysis.json
report-*.json
9 changes: 8 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ func init() {
flags.BoolVarP(&af.HideProgress, "no-progress", "", false, "Disable progress updates")
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", "", "Export as JSON")
flags.StringVarP(&af.OutputFile, "output-file", "o", generateOutputFile(), "Export as JSON")
}

func generateOutputFile() string {
if f, err := os.CreateTemp(".", "report-*.json"); err == nil {
return f.Name()
}
return ""
}
func Main() {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
Expand Down
7 changes: 7 additions & 0 deletions internal/report/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package report

import (
"fmt"
"path/filepath"
"time"

"github.com/thushan/smash/pkg/analysis"
Expand All @@ -11,6 +12,7 @@ import (

type RunSummary struct {
DuplicateFileSizeF string
ReportFilename string
TopFiles []analysis.Item
DuplicateFileSize uint64
TotalFiles int64
Expand All @@ -37,6 +39,11 @@ func PrintRunSummary(rs RunSummary, ignoreEmptyFiles bool) {
if rs.DuplicateFileSize > 0 {
theme.Println(writeCategory("Space Reclaimable:"), theme.ColourFileSizeA(rs.DuplicateFileSizeF), "(approx)")
}
if rs.ReportFilename != "" {
filename := filepath.Clean(rs.ReportFilename)
reportUri := theme.Hyperlink("file://"+filename, filename)
theme.Println(writeCategory("Analysis Report:"), theme.StyleUrl(reportUri), "(json)")
}
}
func calcTotalTime(elapsedNs int64) string {
duration := time.Duration(elapsedNs)
Expand Down
24 changes: 21 additions & 3 deletions internal/smash/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,22 @@ func (app *App) Exec() error {
pap.Stop()

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

exportStats := nerdstats.Snapshot()

app.ExportReport()

endStats := nerdstats.Snapshot()

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

if app.Flags.ShowNerdStats {
theme.StyleHeading.Println("---| Nerd Stats")
report.PrintNerdStats(startStats, "> Initial")
report.PrintNerdStats(midStats, "> Post-Analysis")
report.PrintNerdStats(endStats, "> Post-Cleanup")
report.PrintNerdStats(exportStats, "> Post-Summary")
report.PrintNerdStats(endStats, "> Post-Report")
}
app.Export("report.json")
return nil
}

Expand Down Expand Up @@ -221,3 +226,16 @@ func (app *App) checkTerminal() {
pterm.DisableStyling()
}
}

func (app *App) ExportReport() {
if app.Flags.OutputFile == "" {
theme.Warn.Println("Could not output report.")
return
}

if err := app.Export(app.Flags.OutputFile); err != nil {
theme.Error.Println("Failed to export report because ", err)
} else {
app.Summary.ReportFilename = app.Flags.OutputFile
}
}

0 comments on commit 1703c8b

Please sign in to comment.