chore: cli refactor
This commit is contained in:
1 parent
181642172a
commit
99264ca438
8 files changed
+27
-50
No files matched your search
@@ -0,0 +1,52 @@
|
||||
package cmd
|
||||
|
||||
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