summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--main.go85
-rw-r--r--template.go81
-rw-r--r--templates/club.txt.tpl4
-rw-r--r--templates/tireur.txt.tpl6
4 files changed, 63 insertions, 113 deletions
diff --git a/main.go b/main.go
index d8a0f8d..d8adf3a 100644
--- a/main.go
+++ b/main.go
@@ -12,79 +12,54 @@ import (
"github.com/jszwec/csvutil"
)
-//goland:noinspection GoUnusedType
-type engarde interface {
- Engarde() (string, error)
-}
-
-type participant struct {
+type Participant struct {
LastName string `csv:"lastname"`
FirstName string `csv:"firstname"`
DateOfBirth string `csv:"dateofbirth"`
Gender Gender `csv:"gender"`
Nation string `csv:"nation"`
Region string `csv:"region"`
- Club string `csv:"club"`
- id uint
- club club
-}
-
-type club struct {
- name string
- id uint
-}
-
-func (p participant) Engarde() (string, error) {
- name := strings.ToUpper(p.LastName)
- gender, err := p.Gender.Engarde()
- if err != nil {
- return "", err
- }
-
- return fmt.Sprintf(`
-{[classe tireur] [sexe %s] [presence present] [carton_coach non] [status normal]
- [medical non] [lateralite droite] [nom " %s "] [prenom " %s "]
- [points 1.0] [date_oed "340"] [cle %d] [club1 %d]}
-`, gender, name, p.FirstName, p.id, p.club.id), nil
+ ClubStr string `csv:"club"`
+ Id uint `csv:"-"`
+ ClubId uint `csv:"-"`
}
-func (c club) Engarde() (string, error) {
- return fmt.Sprintf(`
-{[classe club] [nom "%s"] [modifie vrai] [date_oed "332"] [cle %d]}`, c.name, c.id), nil
+type Club struct {
+ Name string
+ Id uint
}
-func prepareParticipants(participants []participant) []club {
- clubMap := map[string]club{}
- var clubs []club
+func prepareParticipants(participants []Participant) []Club {
+ clubMap := map[string]uint{}
+ var clubs []Club
var clubId, participantId uint
for i := range participants {
p := &participants[i]
participantId++
- p.id = participantId
+ p.Id = participantId
- pClub := p.Club
+ pClub := p.ClubStr
if strings.Contains(pClub, ", ") {
pClub = strings.Split(pClub, ", ")[0]
}
- if c, ok := clubMap[pClub]; ok {
- p.club = c
+ if cId, ok := clubMap[pClub]; ok {
+ p.ClubId = cId
} else {
clubId++
- c = club{pClub, clubId}
- p.club = c
- clubMap[p.Club] = c
- clubs = append(clubs, c)
+ p.ClubId = clubId
+ clubMap[pClub] = clubId
+ clubs = append(clubs, Club{pClub, clubId})
}
}
return clubs
}
-func parseOphardtInput(fileName string) ([]participant, []club, error) {
+func parseOphardtInput(fileName string) ([]Participant, []Club, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, nil, fmt.Errorf("opening input file '%s': %w", fileName, err)
@@ -104,7 +79,7 @@ func parseOphardtInput(fileName string) ([]participant, []club, error) {
}
dec.DisallowMissingColumns = true
- var participants []participant
+ var participants []Participant
if err = dec.Decode(&participants); err != nil {
return nil, nil, fmt.Errorf("decoding file '%s': %w", fileName, err)
}
@@ -118,14 +93,16 @@ func usage() string {
}
type EngardeConfig struct {
- inputFile string
- outputDir string
- Name string
- Description string
- Gender Gender
- AgeGroup AgeGroup
- Weapon Weapon
- Date time.Time
+ inputFile string
+ outputDir string
+ Name string
+ Description string
+ Gender Gender
+ AgeGroup AgeGroup
+ Weapon Weapon
+ Date time.Time
+ Participants []Participant
+ Clubs []Club
}
func parseArgs() (config EngardeConfig, err error) {
@@ -160,12 +137,12 @@ func run() error {
return err
}
- participants, clubs, err := parseOphardtInput(cfg.inputFile)
+ cfg.Participants, cfg.Clubs, err = parseOphardtInput(cfg.inputFile)
if err != nil {
return err
}
- return write(cfg, participants, clubs)
+ return write(cfg)
}
func main() {
diff --git a/template.go b/template.go
index 036f806..0b3ee32 100644
--- a/template.go
+++ b/template.go
@@ -7,24 +7,21 @@ import (
"io/fs"
"os"
"path"
+ "strings"
"text/template"
)
-const verbatimPath = "templates/verbatim"
-const participantsName = "tireur.txt"
-const clubsName = "club.txt"
-const competitionName = "competition.egw"
+const templatePath = "templates/"
+const verbatimPath = templatePath + "verbatim"
+const templateGlob = templatePath + "*.tpl"
-//go:embed templates/verbatim
-var verbatim embed.FS
-
-//go:embed templates/competition.egw.tpl
-var competition string
+//go:embed templates
+var templates embed.FS
func writeVerbatimFile(outputDir string, entry fs.DirEntry) error {
inName := path.Join(verbatimPath, entry.Name())
- input, err := verbatim.Open(inName)
+ input, err := templates.Open(inName)
if err != nil {
return fmt.Errorf("reading bundled file '%s': %w", entry.Name(), err)
}
@@ -45,7 +42,7 @@ func writeVerbatimFile(outputDir string, entry fs.DirEntry) error {
}
func writeVerbatim(outputDir string) error {
- entries, err := verbatim.ReadDir(verbatimPath)
+ entries, err := templates.ReadDir(verbatimPath)
if err != nil {
return err
}
@@ -59,7 +56,7 @@ func writeVerbatim(outputDir string) error {
return nil
}
-func write(config EngardeConfig, participants []participant, clubs []club) error {
+func write(config EngardeConfig) error {
if err := os.MkdirAll(config.outputDir, 0755); err != nil {
return fmt.Errorf("creating output directory '%s': %w", config.outputDir, err)
}
@@ -68,69 +65,35 @@ func write(config EngardeConfig, participants []participant, clubs []club) 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)
- }
-
- if err := writeCompetition(config.outputDir, config); err != nil {
- return fmt.Errorf("writing competition file: %w", err)
+ if err := writeTemplates(config.outputDir, config); err != nil {
+ return fmt.Errorf("writing template files: %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)
+var funcMap = template.FuncMap{
+ "upper": strings.ToUpper,
}
-func writeEngarde(outputDir, fileName string, entries []engarde) error {
- outName := path.Join(outputDir, fileName)
- output, err := os.Create(outName)
+func writeTemplates(outputDir string, config EngardeConfig) error {
+ tpls, err := template.New("root").Funcs(funcMap).ParseFS(templates, templateGlob)
if err != nil {
- return fmt.Errorf("creating '%s': %w", outName, err)
+ return fmt.Errorf("parsing templates: %w", err)
}
- defer output.Close()
-
- encodedOutput := encodedWriter(output)
- for _, entry := range entries {
- str, err := entry.Engarde()
- if err != nil {
+ for _, tpl := range tpls.Templates() {
+ if err = writeTemplate(tpl, outputDir, config); err != nil {
return err
}
- if _, err = encodedOutput.Write([]byte(str)); err != nil {
- return fmt.Errorf("writing to '%s': %w", outName, err)
- }
}
return nil
}
-func writeCompetition(outputDir string, config EngardeConfig) error {
- tpl, err := template.New(competitionName).Parse(competition)
- if err != nil {
- return fmt.Errorf("parsing template '%s': %w", competitionName, err)
- }
-
- outName := path.Join(outputDir, competitionName)
+func writeTemplate(tpl *template.Template, outputDir string, config EngardeConfig) error {
+ resultName := strings.TrimSuffix(tpl.Name(), ".tpl")
+ outName := path.Join(outputDir, resultName)
output, err := os.Create(outName)
if err != nil {
return fmt.Errorf("creating '%s': %w", outName, err)
@@ -140,7 +103,7 @@ func writeCompetition(outputDir string, config EngardeConfig) error {
encodedOutput := encodedWriter(output)
if err = tpl.Execute(encodedOutput, config); err != nil {
- return fmt.Errorf("executing template '%s': %w", competitionName, err)
+ return fmt.Errorf("executing template '%s': %w", tpl.Name(), err)
}
return nil
}
diff --git a/templates/club.txt.tpl b/templates/club.txt.tpl
new file mode 100644
index 0000000..d38201c
--- /dev/null
+++ b/templates/club.txt.tpl
@@ -0,0 +1,4 @@
+{{- /*gotype:github.com/Necoro/engarde-importer.EngardeConfig*/ -}}
+{{ range .Clubs -}}
+ {[classe club] [nom "{{.Name}}"] [modifie vrai] [date_oed "332"] [cle {{.Id}}]}
+{{ end }} \ No newline at end of file
diff --git a/templates/tireur.txt.tpl b/templates/tireur.txt.tpl
new file mode 100644
index 0000000..980b60a
--- /dev/null
+++ b/templates/tireur.txt.tpl
@@ -0,0 +1,6 @@
+{{- /*gotype:github.com/Necoro/engarde-importer.EngardeConfig*/ -}}
+{{ range .Participants -}}
+ {[classe tireur] [sexe {{.Gender.Engarde}}] [presence present] [carton_coach non] [status normal]
+ [medical non] [lateralite droite] [nom " {{.LastName | upper}} "] [prenom " {{.FirstName}} "]
+ [points 1.0] [date_oed "340"] [cle {{.Id}}] [club1 {{.ClubId}}]}
+{{ end }} \ No newline at end of file