feat: import command
This commit is contained in:
1 parent
c41d13bb77
commit
8c683e87ab
2 files changed
+47
No files matched your search
@@ -28,6 +28,7 @@ func NewCLI(ver string, app Application) *CLI {
|
||||
NewRmCommand(app),
|
||||
NewSearchCmd(app),
|
||||
NewExportCmd(app),
|
||||
NewImportCmd(app),
|
||||
)
|
||||
|
||||
return &CLI{
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package cli
|
||||
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user