From b1c89fbe485a5b5191bb7ba428ce43ccbe1b7e8f Mon Sep 17 00:00:00 2001 From: Rui Azevedo Date: Tue, 30 Jul 2024 16:06:48 +0100 Subject: [PATCH] refactor, Use the built-in Golang function to open report files, since it can error if the file already exists Part of #296 --- cmd/eval-dev-quality/cmd/report.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/cmd/eval-dev-quality/cmd/report.go b/cmd/eval-dev-quality/cmd/report.go index f71fd01c..6e4f3616 100644 --- a/cmd/eval-dev-quality/cmd/report.go +++ b/cmd/eval-dev-quality/cmd/report.go @@ -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. @@ -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 = os.OpenFile(filepath.Join(command.ResultPath, "evaluation.csv"), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0755); err != nil { + command.logger.Panicf("ERROR: %s", err) } + defer evaluationCSVFile.Close() // Collect all evaluation CSV file paths. allEvaluationPaths := map[string]bool{}