Skip to content

Commit

Permalink
refactor, Move the logic that creates report files into a helper, so …
Browse files Browse the repository at this point in the history
…it can be reused

Part of #296
  • Loading branch information
ruiAzevedo19 committed Jul 30, 2024
1 parent 8ae0ab8 commit b968df6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
17 changes: 5 additions & 12 deletions cmd/eval-dev-quality/cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/symflower/eval-dev-quality/evaluate"
"github.com/symflower/eval-dev-quality/evaluate/report"
"github.com/symflower/eval-dev-quality/log"
"github.com/symflower/eval-dev-quality/util"
)

// Report holds the "report" command.
Expand Down Expand Up @@ -41,19 +42,11 @@ func (command *Report) Execute(args []string) (err error) {
if err = osutil.MkdirAll(filepath.Dir(command.ResultPath)); err != nil {
command.logger.Panicf("ERROR: %s", err)
}
if _, err := os.Stat(filepath.Join(command.ResultPath, "evaluation.csv")); err != nil {
if os.IsNotExist(err) {
evaluationCSVFile, err = os.Create(filepath.Join(command.ResultPath, "evaluation.csv"))
if err != nil {
command.logger.Panicf("ERROR: %s", err)
}
defer evaluationCSVFile.Close()
} else {
command.logger.Panicf("ERROR: %s", err)
}
} else {
command.logger.Panicf("ERROR: an evaluation CSV file already exists in %s", command.ResultPath)

if evaluationCSVFile, err = util.CreateFileIfNotExists(filepath.Join(command.ResultPath, "evaluation.csv")); err != nil {
command.logger.Panicf("ERROR: %s", err)
}
defer evaluationCSVFile.Close()

// Collect all evaluation CSV file paths.
allEvaluationPaths := map[string]bool{}
Expand Down
18 changes: 18 additions & 0 deletions util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ func UniqueDirectory(path string) (uniquePath string, err error) {
return true, nil
})
}

// CreateFileIfNotExists creates a file if it does not already exists.
func CreateFileIfNotExists(filePath string) (file *os.File, err error) {
if _, err := os.Stat(filePath); err != nil {
if os.IsNotExist(err) {
file, err = os.Create(filePath)
if err != nil {
return nil, pkgerrors.WithStack(err)
}

return file, nil
} else {
return nil, pkgerrors.WithStack(err)
}
} else {
return nil, pkgerrors.Errorf("the file %q already exists", filePath)
}
}

0 comments on commit b968df6

Please sign in to comment.