diff options
Diffstat (limited to '')
-rw-r--r-- | template.go | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/template.go b/template.go index ce1127a..1e43f08 100644 --- a/template.go +++ b/template.go @@ -10,11 +10,12 @@ import ( ) const verbatimPath = "templates/verbatim" +const participantsName = "tireur.txt" +const clubsName = "club.txt" //go:embed templates/verbatim var verbatim embed.FS -//goland:noinspection GoUnhandledErrorResult func writeVerbatimFile(outputDir string, entry fs.DirEntry) error { inName := path.Join(verbatimPath, entry.Name()) @@ -53,7 +54,7 @@ func writeVerbatim(outputDir string) error { return nil } -func write(config EngardeConfig) error { +func write(config EngardeConfig, participants []participant, clubs []club) error { if err := os.MkdirAll(config.outputDir, 0755); err != nil { return fmt.Errorf("creating output directory '%s': %w", config.outputDir, err) } @@ -62,5 +63,54 @@ func write(config EngardeConfig) error { return fmt.Errorf("copying default files: %w", err) } + if err := writeParticipants(config.outputDir, participants); err != nil { + return fmt.Errorf("writing participant data: %w", err) + } + + if err := writeClubs(config.outputDir, clubs); err != nil { + return fmt.Errorf("writing club data: %w", err) + } + + return nil +} + +func writeClubs(outputDir string, clubs []club) error { + entries := make([]engarde, len(clubs)) + for i := range clubs { + entries[i] = clubs[i] + } + + return writeEngarde(outputDir, clubsName, entries) +} + +func writeParticipants(outputDir string, participants []participant) error { + entries := make([]engarde, len(participants)) + for i := range participants { + entries[i] = participants[i] + } + + return writeEngarde(outputDir, participantsName, entries) +} + +func writeEngarde(outputDir, fileName string, entries []engarde) error { + outName := path.Join(outputDir, fileName) + output, err := os.Create(outName) + if err != nil { + return fmt.Errorf("creating '%s': %w", outName, err) + } + defer output.Close() + + encodedOutput := encodedWriter(output) + + for _, entry := range entries { + str, err := entry.engarde() + if err != nil { + return err + } + if _, err = encodedOutput.Write([]byte(str)); err != nil { + return fmt.Errorf("writing to '%s': %w", outName, err) + } + } + return nil } |