From 89d6e3d79f4f97a9b49d1b90eae84b00fecd592e Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Mon, 24 Jan 2022 00:42:01 +0100 Subject: Write participants and clubs --- template.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'template.go') 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 } -- cgit v1.2.3-70-g09d2