diff --git a/internal/music/controller.go b/internal/music/controller.go index 1404b3c..55512b2 100644 --- a/internal/music/controller.go +++ b/internal/music/controller.go @@ -25,12 +25,32 @@ func LoadAlbums() ([]Album, error) { return albums, nil } -func ProcessAlbum(albumList []Album) error { +func ProcessAlbum(albumList []Album, update bool) error { fmt.Printf(" M U S I C\n") utils.Sep() for _, album := range albumList { fmt.Printf("Title: %s\n", album.Title) + // If we're updating, the process is a bit different + if update { + outputDir := os.Getenv("MARKDOWN_OUTPUT_MUSIC_DIR") + mdFilePath := filepath.Join(outputDir, fmt.Sprintf("%s.md", utils.Sluggify(album.Title))) + frontmatter, content, err := utils.LoadMarkdown(mdFilePath) + if err != nil { + fmt.Printf(" ! Error loading markdown frontmatter for album %s: %v\n", album.Title, err) + continue + } + updatedFrontmatter := updateFrontmatterWithYAML(frontmatter, album) + err = utils.SaveUpdatedMarkdown(mdFilePath, updatedFrontmatter, content) + if err != nil { + fmt.Printf(" ! Error saving updated markdown for album %s: %v\n", album.Title, err) + continue + } + // We want to continue the loop here, in update's cases we don't + // want to do any api calls for info nor for images + continue + } + // If we dont have ID, search album by Title and get the ID if album.ID == "" { err := SearchAlbumByTitle(album.Title, &album) @@ -83,3 +103,10 @@ func generateAlbumMarkdown(album Album) 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, album Album) utils.FrontMatter { + frontmatter.Date = album.Date + frontmatter.Rate = album.Rate + return frontmatter +} diff --git a/main.go b/main.go index d90b068..b8b5eb1 100644 --- a/main.go +++ b/main.go @@ -103,7 +103,7 @@ func processMusic(update bool) { fmt.Printf("Error reading music file: %v\n", err) return } - err = music.ProcessAlbum(albumList) + err = music.ProcessAlbum(albumList, update) if err != nil { fmt.Printf("Error processing music: %v\n", err) }