From 2ec7e6cd8554f1c2664752f20c00e96aa9d6885e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=81scar=20M=2E=20Lage?= Date: Fri, 11 Oct 2024 22:54:29 +0200 Subject: [PATCH] Added --update param to games --- internal/games/controller.go | 27 ++++++++++++++++++++++++++- main.go | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/internal/games/controller.go b/internal/games/controller.go index 189e02f..081885d 100644 --- a/internal/games/controller.go +++ b/internal/games/controller.go @@ -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 +} diff --git a/main.go b/main.go index fd00151..0722163 100644 --- a/main.go +++ b/main.go @@ -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) }