From 89d6e3d79f4f97a9b49d1b90eae84b00fecd592e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Mon, 24 Jan 2022 00:42:01 +0100 Subject: Write participants and clubs --- main.go | 30 +++++++++++++++++------------- template.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index b671b15..d0fd2b4 100644 --- a/main.go +++ b/main.go @@ -53,8 +53,9 @@ func (c club) engarde() (string, error) { {[classe club] [nom "%s"] [modifie vrai] [date_oed "332"] [cle %d]}`, c.name, c.id), nil } -func prepareParticipants(participants []participant) { - clubs := map[string]club{} +func prepareParticipants(participants []participant) []club { + clubMap := map[string]club{} + var clubs []club var clubId, participantId uint for i := range participants { @@ -68,27 +69,30 @@ func prepareParticipants(participants []participant) { pClub = strings.Split(pClub, ", ")[0] } - if c, ok := clubs[pClub]; ok { + if c, ok := clubMap[pClub]; ok { p.club = c } else { clubId++ c = club{pClub, clubId} p.club = c - clubs[p.Club] = c + clubMap[p.Club] = c + clubs = append(clubs, c) } } + + return clubs } -func parseOphardtInput(fileName string) ([]participant, error) { +func parseOphardtInput(fileName string) ([]participant, []club, error) { f, err := os.Open(fileName) if err != nil { - return nil, fmt.Errorf("opening input file '%s': %w", fileName, err) + return nil, nil, fmt.Errorf("opening input file '%s': %w", fileName, err) } encReader, err := encodedReader(f) if err != nil { - return nil, fmt.Errorf("cannot determine encoding of file '%s': %w", fileName, err) + return nil, nil, fmt.Errorf("cannot determine encoding of file '%s': %w", fileName, err) } csvReader := csv.NewReader(encReader) @@ -96,17 +100,17 @@ func parseOphardtInput(fileName string) ([]participant, error) { dec, err := csvutil.NewDecoder(csvReader) if err != nil { - return nil, fmt.Errorf("reading from file '%s': %w", fileName, err) + return nil, nil, fmt.Errorf("reading from file '%s': %w", fileName, err) } dec.DisallowMissingColumns = true var participants []participant if err = dec.Decode(&participants); err != nil { - return nil, fmt.Errorf("decoding file '%s': %w", fileName, err) + return nil, nil, fmt.Errorf("decoding file '%s': %w", fileName, err) } - prepareParticipants(participants) - return participants, nil + clubs := prepareParticipants(participants) + return participants, clubs, nil } func usage() string { @@ -155,12 +159,12 @@ func run() error { return err } - _, err = parseOphardtInput(cfg.inputFile) + participants, clubs, err := parseOphardtInput(cfg.inputFile) if err != nil { return err } - return write(cfg) + return write(cfg, participants, clubs) } func main() { 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