Files
psalms/internal/fetcher/savechapter.go
2026-03-24 18:39:59 +01:00

60 lines
1.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package fetcher
import (
"log"
"project.hechon.fr/internal/config"
"project.hechon.fr/internal/models"
"gopkg.in/yaml.v3"
"os"
"path"
"strconv"
)
func SaveChapter(cfg config.Cfg, chapter *config.Chapter) (error){
var chapterYaml models.Yaml
for i, verse:= range chapter.Verses{
poeticLine := models.PoeticLine{
Who: "",
To: "",
Of: "",
}
cola := SplitInPart(verse, cfg.ProperName)
for _, colon := range cola{
poeticLine.Cola = append(poeticLine.Cola, models.Colon{
Verse: i+1,
Text: colon,
})
}
chapterYaml.PoeticLines = append(chapterYaml.PoeticLines, poeticLine)
}
yamlData, err := yaml.Marshal(chapterYaml)
if err != nil{
log.Printf("failed unpacking this yaml struct:%v", chapterYaml)
return err
}
fileName := path.Join(cfg.YamlPath, cfg.Book.Name + strconv.Itoa(chapter.Number) + ".yaml")
_,err = os.Stat(fileName)
if err == nil {
err = os.Remove(fileName)
if err != nil {
log.Printf("cant remove %v: %v", fileName, err)
return err
}
}
err = os.WriteFile(fileName, yamlData, 0600)
if err != nil{
log.Printf("cant write into %v", fileName)
return err
}
return nil
}