package cmd import ( "bufio" "errors" _ "github.com/abelith/spellbook/internal/adapters/json" "github.com/spf13/cobra" "io" "os" ) func NewExportCmd(app Application) *cobra.Command { var format string var out string cmd := &cobra.Command{ Use: "export", Short: "Export spellbook", Long: `WIP`, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() var w io.Writer = os.Stdout if out != "" { f, err := os.OpenFile(out, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) if err != nil { return err } defer func() { if innerErr := f.Close(); innerErr != nil { err = errors.Join(err, innerErr) } }() w = f } buf := bufio.NewWriter(w) defer func() { if innerErr := buf.Flush(); innerErr != nil { err = errors.Join(err, innerErr) } }() return app.ExportSpells(ctx, format, buf) }, } cmd.Flags().StringVarP(&format, "format", "f", "jsonl", "Output format") cmd.Flags().StringVarP(&out, "out", "o", "", "Output") return cmd }