/* Copyright © 2026 Hector Alfaro */ package cmd import ( "fmt" "os" "github.com/spf13/cobra" "tangled.org/hectorsector.com/books/cli/internal/apperrors" "tangled.org/hectorsector.com/books/cli/internal/openlibrary" "tangled.org/hectorsector.com/books/cli/internal/store" ) var enrichCmd = &cobra.Command{ Use: "enrich", Short: "looks up OL IDs for each book", Run: func(cmd *cobra.Command, args []string) { storePath, _ := cmd.Root().PersistentFlags().GetString("store") err := runEnrich(storePath) if err != nil { fmt.Fprintf(os.Stderr, "enriching with OLWorkIDs:\n%s", apperrors.FormatError(err)) os.Exit(1) } }, } func runEnrich(storePath string) error { books, err := store.Load(storePath) if err != nil { return fmt.Errorf("loading books from store: %w", err) } fmt.Println("📂 Loaded from store.") for i := range books { fmt.Printf("Enriching: %s\n", books[i].Title) if books[i].OLWorkID == "" && books[i].ISBN != "" { books[i].OLWorkID, err = openlibrary.LookupByISBN(books[i].ISBN) if err != nil { fmt.Fprintf(os.Stderr, "warning: Couldn't resolve ISBN %s to a OLWorkID\n", books[i].ISBN) } } if books[i].OLWorkID == "" && books[i].Title != "" && books[i].Author != "" { books[i].OLWorkID, err = openlibrary.LookupByTitle(books[i].Title, books[i].Author) if err != nil { fmt.Fprintf(os.Stderr, "warning: Couldn't resolve to a OLWorkID. Searched for:\n Title: %s\n Author: %s\n", books[i].Title, books[i].Author) } } // couldn't match via either search if books[i].OLWorkID == "" { fmt.Fprintf(os.Stderr, "warning: Couldn't resolve to a OLWorkID. Searched for:\n ISBN: %s\n Title: %s\n Author: %s\n", books[i].ISBN, books[i].Title, books[i].Author) } } err = store.Save(storePath, books) if err != nil { return fmt.Errorf("saving to store: %w", err) } fmt.Println("💾 Store updated.") fmt.Println("👀 You can review books that couldn't be matched with:\n books list --no-olworkid") return nil } func init() { rootCmd.AddCommand(enrichCmd) }