Skip to content

Commit

Permalink
fix random output files being left in root.
Browse files Browse the repository at this point in the history
  • Loading branch information
thushan committed Dec 19, 2023
1 parent 6478336 commit 9d36cf1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
8 changes: 1 addition & 7 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,9 @@ func init() {
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)")
flags.StringVarP(&af.OutputFile, "output-file", "o", "", "Export analysis as JSON (generated automatically like ./report-*.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))
log.SetOutput(os.Stdout)
Expand Down
10 changes: 4 additions & 6 deletions internal/smash/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type AppRuntime struct {
Files chan *indexer.FileFS
}

const ReportOutputTemplate = "report-*.json"

func (app *App) Run() error {

af := app.Flags
Expand Down Expand Up @@ -229,14 +231,10 @@ func (app *App) ExportReport() {
if app.Flags.HideOutput {
return
}
if app.Flags.OutputFile == "" {
theme.Warn.Println("Could not output report.")
return
}

if err := app.Export(app.Flags.OutputFile); err != nil {
if filename, err := app.Export(app.Flags.OutputFile); err != nil {
theme.Error.Println("Failed to export report because ", err)
} else {
app.Summary.ReportFilename = app.Flags.OutputFile
app.Summary.ReportFilename = filename
}
}
27 changes: 22 additions & 5 deletions internal/smash/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package smash

import (
"encoding/json"
"fmt"
"os"
user2 "os/user"
"path/filepath"
Expand Down Expand Up @@ -61,14 +62,30 @@ type ReportDuplicateSummary struct {
ReportFileSummary
}

func (app *App) Export(filePath string) error {
f, err := os.Create(filePath)
if err != nil {
return err
func (app *App) Export(filePath string) (string, error) {

var fs *os.File
var err error

if filePath == "" {
if fs, err = os.CreateTemp(".", ReportOutputTemplate); err != nil {
return "", fmt.Errorf("failed to report output: %w", err)
}
} else {
if fs, err = os.Create(filePath); err != nil {
return "", fmt.Errorf("failed to report output: %w", err)
}
}
defer f.Close()

defer fs.Close()

return fs.Name(), app.ExportFile(fs)
}

func (app *App) ExportFile(f *os.File) error {
return json.NewEncoder(f).Encode(app.GenerateReportOutput())
}

func (app *App) GenerateReportOutput() ReportOutput {
return ReportOutput{
Summary: summariseRunSummary(app.Summary),
Expand Down

0 comments on commit 9d36cf1

Please sign in to comment.