47 lines
881 B
Go
47 lines
881 B
Go
package cmd
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
_ "github.com/abelith/spellbook/internal/adapters/json"
|
|
"github.com/spf13/cobra"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
func NewImportCmd(app Application) *cobra.Command {
|
|
var format string
|
|
var in string
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "import",
|
|
Short: "Import spellbook",
|
|
Long: `WIP`,
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
ctx := cmd.Context()
|
|
|
|
var r io.Reader = os.Stdin
|
|
if in != "" {
|
|
f, err := os.Open(in)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if innerErr := f.Close(); innerErr != nil {
|
|
err = errors.Join(err, innerErr)
|
|
}
|
|
}()
|
|
r = f
|
|
}
|
|
|
|
buf := bufio.NewReader(r)
|
|
return app.ImportSpells(ctx, format, buf)
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&format, "format", "f", "jsonl", "Input format")
|
|
cmd.Flags().StringVarP(&in, "input", "i", "", "Input")
|
|
|
|
return cmd
|
|
}
|