adding fetching and saving an entire book
This commit is contained in:
59
internal/fetcher/savechapter.go
Normal file
59
internal/fetcher/savechapter.go
Normal file
@@ -0,0 +1,59 @@
|
||||
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{
|
||||
strophe := models.Strophe{}
|
||||
parts := SplitInPart(verse, cfg.ProperName)
|
||||
for _, part := range parts{
|
||||
line := models.Line{
|
||||
Verse: i+1,
|
||||
Text: part,
|
||||
Who: "",
|
||||
To: "",
|
||||
Of: "",
|
||||
}
|
||||
|
||||
strophe.Lines = append(strophe.Lines, line)
|
||||
}
|
||||
chapterYaml.Strophes = append(chapterYaml.Strophes, strophe)
|
||||
log.Printf("%v", parts)
|
||||
}
|
||||
|
||||
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("can’t remove %v: %v", fileName, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = os.WriteFile(fileName, yamlData, 0600)
|
||||
if err != nil{
|
||||
log.Printf("can’t write into %v", fileName)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user