From f97e9b0aa99d9d2f6b5aec31ce9f08d06eef8fa8 Mon Sep 17 00:00:00 2001 From: Kirillkriv Date: Sun, 16 Aug 2026 22:39:51 +0500 Subject: [PATCH] feat: export command --- internal/application/application.go | 23 +++++++++++++ internal/controllers/cli/cli.go | 3 ++ internal/controllers/cli/export.go | 52 +++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 internal/controllers/cli/export.go diff --git a/internal/application/application.go b/internal/application/application.go index 813a0be..2c4c85c 100644 --- a/internal/application/application.go +++ b/internal/application/application.go @@ -2,7 +2,9 @@ package application import ( "context" + "github.com/abelith/spellbook/internal/adapters/exporter" "github.com/abelith/spellbook/internal/models" + "io" ) type SpellCaster interface { @@ -70,3 +72,24 @@ func (a *Application) CastSpell( ) (models.CastResult, error) { return a.caster.Cast(ctx, spellCast) } + +func (a *Application) ExportSpells( + ctx context.Context, + name string, + w io.Writer, +) error { + ch, errCh, err := a.spellQuery.Export(ctx) + if err != nil { + return err + } + + if err := exporter.Export(ctx, name, ch, w); err != nil { + return err + } + + if err := <-errCh; err != nil { + return err + } + + return nil +} diff --git a/internal/controllers/cli/cli.go b/internal/controllers/cli/cli.go index bcb2404..bde69c4 100644 --- a/internal/controllers/cli/cli.go +++ b/internal/controllers/cli/cli.go @@ -4,6 +4,7 @@ import ( "context" "github.com/abelith/spellbook/internal/models" "github.com/spf13/cobra" + "io" ) type Application interface { @@ -12,6 +13,7 @@ type Application interface { GetSpells(ctx context.Context, query models.SearchQuery) ([]models.Spell, error) DeleteSpell(ctx context.Context, spellName string) error CastSpell(ctx context.Context, spellCast models.SpellCast) (models.CastResult, error) + ExportSpells(ctx context.Context, exp string, w io.Writer) error } type CLI struct { @@ -24,6 +26,7 @@ func NewCLI(ver string, app Application) *CLI { NewAddCommand(app), NewRmCommand(app), NewSearchCmd(app), + NewExportCmd(app), ) return &CLI{ diff --git a/internal/controllers/cli/export.go b/internal/controllers/cli/export.go new file mode 100644 index 0000000..234be49 --- /dev/null +++ b/internal/controllers/cli/export.go @@ -0,0 +1,52 @@ +package cli + +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 +}