Added --update param to games

This commit is contained in:
Óscar M. Lage 2024-10-11 22:54:29 +02:00
parent c9a30a3767
commit 2ec7e6cd85
2 changed files with 27 additions and 2 deletions

View File

@ -25,12 +25,29 @@ func LoadGames() ([]Game, error) {
return games, nil
}
func ProcessGames(games []Game) error {
func ProcessGames(games []Game, update bool) error {
fmt.Printf(" G A M E S\n")
utils.Sep()
for _, game := range games {
fmt.Printf("Title: %s\n", game.Title)
// If we're updating, the process is a bit different
if update {
outputDir := os.Getenv("MARKDOWN_OUTPUT_GAMES_DIR")
mdFilePath := filepath.Join(outputDir, fmt.Sprintf("%s.md", utils.Sluggify(game.Title)))
frontmatter, content, err := utils.LoadMarkdown(mdFilePath)
if err != nil {
fmt.Printf(" ! Error loading markdown frontmatter for game %s: %v\n", game.Title, err)
continue
}
updatedFrontmatter := updateFrontmatterWithYAML(frontmatter, game)
err = utils.SaveUpdatedMarkdown(mdFilePath, updatedFrontmatter, content)
if err != nil {
fmt.Printf(" ! Error saving updated markdown for game %s: %v\n", game.Title, err)
continue
}
}
// If we dont have ID, search movie by Title and get the ID
if game.ID == 0 {
err := SearchGameByTitle(game.Title, &game)
@ -102,3 +119,11 @@ func generateGameMarkdown(game Game) error {
return utils.GenerateMarkdown(templatePath, outputPath, data)
}
// Helper function to update only YAML fields that exist in both the frontmatter and YAML data
func updateFrontmatterWithYAML(frontmatter utils.FrontMatter, game Game) utils.FrontMatter {
frontmatter.Progress = game.Progress
frontmatter.Date = game.Date
frontmatter.Rate = game.Rate
return frontmatter
}

View File

@ -91,7 +91,7 @@ func processGames(update bool) {
fmt.Printf("Error reading games file: %v\n", err)
return
}
err = games.ProcessGames(gamesList)
err = games.ProcessGames(gamesList, update)
if err != nil {
fmt.Printf("Error processing games: %v\n", err)
}