Skip to content

Commit

Permalink
Improve removal of temporary files in case of errors (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
laenzlinger authored Apr 9, 2024
1 parent 4f2b19d commit 5129dc8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cmd/generate_setlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"html/template"
"os"
"path/filepath"

"github.com/laenzlinger/setlist/internal/config"
Expand Down Expand Up @@ -85,6 +86,7 @@ func generateSetlist(gigName string) error {
if err != nil {
return err
}
defer os.Remove(filename)

return convert.HTMLToPDF(filename, filepath.Join(config.Target(), fmt.Sprintf("Set List %s.pdf", gig.Name)))
}
2 changes: 2 additions & 0 deletions cmd/generate_suisa.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"html/template"
"os"
"path/filepath"

"github.com/laenzlinger/setlist/internal/config"
Expand Down Expand Up @@ -85,6 +86,7 @@ func generateSuisalist(gigName string) error {
if err != nil {
return err
}
defer os.Remove(filename)

return convert.HTMLToPDF(filename, filepath.Join(config.Target(), fmt.Sprintf("Suisa List %s.pdf", gig.Name)))
}
2 changes: 1 addition & 1 deletion internal/html/pdf/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func HTMLToPDF(in, out string) error {
if err = os.WriteFile(out, buf, 0o600); err != nil {
return fmt.Errorf("failed to write PDF: %w", err)
}
return os.Remove(in)
return nil
}

const (
Expand Down
10 changes: 6 additions & 4 deletions internal/sheet/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (sh sectionHeaders) filename(value string) string {
}

func ForGig(band config.Band, gig gig.Gig) error {
if err := os.MkdirAll(config.PlaceholderDir(), os.ModePerm); err != nil {
return fmt.Errorf("failed to create temporary directory: %w", err)
}
defer os.RemoveAll(config.PlaceholderDir())

sheets := []Sheet{}
sh := sectionHeaders{}
for _, section := range gig.Sections {
Expand Down Expand Up @@ -116,7 +121,7 @@ func merge(sheets []Sheet, outputFileName string) error {
return fmt.Errorf("failed cleanup bookmarks: %w", err)
}

return os.RemoveAll(config.PlaceholderDir())
return nil
}

// Create or update the pdf from the source.
Expand Down Expand Up @@ -189,9 +194,6 @@ func (s *Sheet) sourceDir() string {

func (s *Sheet) generatePlaceholder() error {
s.placeholder = true
if err := os.MkdirAll(s.pdfDir(), os.ModePerm); err != nil {
return fmt.Errorf("failed to create out directory: %w", err)
}
//nolint: gosec // content does not contain html
filename, err := tmpl.CreatePlaceholder(&tmpl.Data{Content: template.HTML(s.content), Title: s.name})
if err != nil {
Expand Down

0 comments on commit 5129dc8

Please sign in to comment.