diff --git a/internal/controllers/cli/cli.go b/internal/controllers/cli/cli.go index ea830ab..61040d9 100644 --- a/internal/controllers/cli/cli.go +++ b/internal/controllers/cli/cli.go @@ -28,6 +28,7 @@ func NewCLI(ver string, app Application) *CLI { NewRmCommand(app), NewSearchCmd(app), NewExportCmd(app), + NewImportCmd(app), ) return &CLI{ diff --git a/internal/controllers/cli/import.go b/internal/controllers/cli/import.go new file mode 100644 index 0000000..90780fe --- /dev/null +++ b/internal/controllers/cli/import.go @@ -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 +}