feat: export command
This commit is contained in:
1 parent
92cb9267fc
commit
f97e9b0aa9
3 files changed
+78
No files matched your search
@@ -2,7 +2,9 @@ package application
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/abelith/spellbook/internal/adapters/exporter"
|
||||||
"github.com/abelith/spellbook/internal/models"
|
"github.com/abelith/spellbook/internal/models"
|
||||||
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SpellCaster interface {
|
type SpellCaster interface {
|
||||||
@@ -70,3 +72,24 @@ func (a *Application) CastSpell(
|
|||||||
) (models.CastResult, error) {
|
) (models.CastResult, error) {
|
||||||
return a.caster.Cast(ctx, spellCast)
|
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
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"github.com/abelith/spellbook/internal/models"
|
"github.com/abelith/spellbook/internal/models"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Application interface {
|
type Application interface {
|
||||||
@@ -12,6 +13,7 @@ type Application interface {
|
|||||||
GetSpells(ctx context.Context, query models.SearchQuery) ([]models.Spell, error)
|
GetSpells(ctx context.Context, query models.SearchQuery) ([]models.Spell, error)
|
||||||
DeleteSpell(ctx context.Context, spellName string) error
|
DeleteSpell(ctx context.Context, spellName string) error
|
||||||
CastSpell(ctx context.Context, spellCast models.SpellCast) (models.CastResult, error)
|
CastSpell(ctx context.Context, spellCast models.SpellCast) (models.CastResult, error)
|
||||||
|
ExportSpells(ctx context.Context, exp string, w io.Writer) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
@@ -24,6 +26,7 @@ func NewCLI(ver string, app Application) *CLI {
|
|||||||
NewAddCommand(app),
|
NewAddCommand(app),
|
||||||
NewRmCommand(app),
|
NewRmCommand(app),
|
||||||
NewSearchCmd(app),
|
NewSearchCmd(app),
|
||||||
|
NewExportCmd(app),
|
||||||
)
|
)
|
||||||
|
|
||||||
return &CLI{
|
return &CLI{
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in new issue
Block a user