From b2f45d6addf89057293e696c1781dc45ffb62049 Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Sun, 2 Oct 2016 02:29:52 +0200 Subject: [PATCH] Moves default clean file checks to the helper package --- compiler/clean.go | 27 +++------------------------ helpers/file_utils.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/compiler/clean.go b/compiler/clean.go index 187f641..22b228e 100644 --- a/compiler/clean.go +++ b/compiler/clean.go @@ -32,26 +32,12 @@ func Clean(path string, opts *settings.Settings) { // Remove score files for i := range scores { scores[i].GenerateOutputPath(opts) - if helpers.Exists(scores[i].OutputPath) { - if err := os.Remove(scores[i].OutputPath); err != nil { - log.WithFields(log.Fields{ - "error": err, - "score": scores[i], - }).Error("unable to delete file") - } - } + helpers.CleanFile(scores[i].OutputPath) } // Remove LaTeX resources texPath := filepath.Join(opts.OutputDir, opts.Name+".tex") - if helpers.Exists(texPath) { - if err := os.Remove(texPath); err != nil { - log.WithFields(log.Fields{ - "error": err, - "file": texPath, - }).Error("unable to delete file") - } - } + helpers.CleanFile(texPath) for i := range opts.LatexResources { path := filepath.Join(opts.OutputDir, filepath.Base(opts.LatexResources[i])) err := os.RemoveAll(path) @@ -65,12 +51,5 @@ func Clean(path string, opts *settings.Settings) { // Remove target songbook songbookPath := filepath.Join(opts.OutputDir, opts.Name+".pdf") - if helpers.Exists(songbookPath) { - if err := os.Remove(songbookPath); err != nil { - log.WithFields(log.Fields{ - "error": err, - "songbook": songbookPath, - }).Error("unable to delete file") - } - } + helpers.CleanFile(songbookPath) } diff --git a/helpers/file_utils.go b/helpers/file_utils.go index b830c57..2c35881 100644 --- a/helpers/file_utils.go +++ b/helpers/file_utils.go @@ -99,3 +99,16 @@ func AbsolutePath(path, root string) string { } return filepath.Join(root, path) } + +// CleanFile removes the file on the given path if it exists. In case of +// failure, an error will be logged. +func CleanFile(path string) { + if Exists(path) { + if err := os.Remove(path); err != nil { + log.WithFields(log.Fields{ + "error": err, + "file": path, + }).Error("unable to delete file") + } + } +}