/* Copyright © 2026 Hector Alfaro */ package cmd import ( "fmt" "os" "strings" "github.com/spf13/cobra" "tangled.org/hectorsector.com/books/cli/internal/apperrors" "tangled.org/hectorsector.com/books/cli/internal/store" "tangled.org/hectorsector.com/books/cli/internal/storygraph" ) // importCmd represents the import command var importCmd = &cobra.Command{ Use: "import [file]", Short: "Import from a StoryGraph CSV", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { filename := args[0] _, csvSuffix := strings.CutSuffix(filename, ".csv") if !csvSuffix { fmt.Fprintln(os.Stderr, "file isn't a csv") os.Exit(1) } books, err := storygraph.ParseCSV(filename) if err != nil { fmt.Fprintf(os.Stderr, "parsing csv %s:\n%s", filename, apperrors.FormatError(err)) os.Exit(1) } booksWithStatuses := store.FromStorygraph(books) storePath, _ := cmd.Root().PersistentFlags().GetString("store") err = store.Save(storePath, booksWithStatuses) if err != nil { fmt.Fprintf(os.Stderr, "saving store:\n%s", apperrors.FormatError(err)) os.Exit(1) } fmt.Println("💾 Store updated.") }, } func init() { rootCmd.AddCommand(importCmd) // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: // importCmd.PersistentFlags().String("foo", "", "A help for foo") // Cobra supports local flags which will only run when this command // is called directly, e.g.: // importCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") importCmd.Flags().String("source", "storygraph", "Import source, defaults to storygraph.") }