chore: cli refactor
This commit is contained in:
1 parent
181642172a
commit
99264ca438
8 files changed
+27
-50
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
package cli
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cli
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cli
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cli
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -1,14 +1,25 @@
|
|||||||
package cli
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/abelith/spellbook/internal/models"
|
"github.com/abelith/spellbook/internal/models"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Application interface {
|
||||||
|
AddSpell(ctx context.Context, spell models.Spell) error
|
||||||
|
GetSpell(ctx context.Context, spellName string) (models.Spell, error)
|
||||||
|
GetSpells(ctx context.Context, query models.SearchQuery) ([]models.Spell, error)
|
||||||
|
DeleteSpell(ctx context.Context, spellName string) error
|
||||||
|
CastSpell(ctx context.Context, spellCast models.SpellCast) (models.CastResult, error)
|
||||||
|
ExportSpells(ctx context.Context, exp string, w io.Writer) error
|
||||||
|
ImportSpells(ctx context.Context, exp string, r io.Reader) error
|
||||||
|
}
|
||||||
|
|
||||||
func rootCmd(app Application) func(*cobra.Command, []string) error {
|
func rootCmd(app Application) func(*cobra.Command, []string) error {
|
||||||
return func(cmd *cobra.Command, args []string) error {
|
return func(cmd *cobra.Command, args []string) error {
|
||||||
ctx := cmd.Context()
|
ctx := cmd.Context()
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cli
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"github.com/abelith/spellbook/internal/models"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Application interface {
|
|
||||||
AddSpell(ctx context.Context, spell models.Spell) error
|
|
||||||
GetSpell(ctx context.Context, spellName string) (models.Spell, error)
|
|
||||||
GetSpells(ctx context.Context, query models.SearchQuery) ([]models.Spell, error)
|
|
||||||
DeleteSpell(ctx context.Context, spellName string) error
|
|
||||||
CastSpell(ctx context.Context, spellCast models.SpellCast) (models.CastResult, error)
|
|
||||||
ExportSpells(ctx context.Context, exp string, w io.Writer) error
|
|
||||||
ImportSpells(ctx context.Context, exp string, r io.Reader) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type CLI struct {
|
|
||||||
root *cobra.Command
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLI(ver string, app Application) *CLI {
|
|
||||||
root := NewRootCmd(ver, app)
|
|
||||||
root.AddCommand(
|
|
||||||
NewAddCommand(app),
|
|
||||||
NewRmCommand(app),
|
|
||||||
NewSearchCmd(app),
|
|
||||||
NewExportCmd(app),
|
|
||||||
NewImportCmd(app),
|
|
||||||
)
|
|
||||||
|
|
||||||
return &CLI{
|
|
||||||
root: root,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CLI) Run(_ context.Context) error {
|
|
||||||
return c.root.Execute()
|
|
||||||
}
|
|
||||||
+10
-3
@@ -4,11 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/abelith/spellbook/cmd"
|
||||||
"github.com/abelith/spellbook/internal/adapters/caster"
|
"github.com/abelith/spellbook/internal/adapters/caster"
|
||||||
"github.com/abelith/spellbook/internal/adapters/sqlite"
|
"github.com/abelith/spellbook/internal/adapters/sqlite"
|
||||||
"github.com/abelith/spellbook/internal/application"
|
"github.com/abelith/spellbook/internal/application"
|
||||||
"github.com/abelith/spellbook/internal/config"
|
"github.com/abelith/spellbook/internal/config"
|
||||||
"github.com/abelith/spellbook/internal/controllers/cli"
|
|
||||||
_ "modernc.org/sqlite"
|
_ "modernc.org/sqlite"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@@ -44,9 +44,16 @@ func main() {
|
|||||||
|
|
||||||
app := application.NewApplication(&caster.SpellCaster{}, repo, repo)
|
app := application.NewApplication(&caster.SpellCaster{}, repo, repo)
|
||||||
|
|
||||||
c := cli.NewCLI(Version, app)
|
root := cmd.NewRootCmd(Version, app)
|
||||||
|
root.AddCommand(
|
||||||
|
cmd.NewAddCommand(app),
|
||||||
|
cmd.NewRmCommand(app),
|
||||||
|
cmd.NewSearchCmd(app),
|
||||||
|
cmd.NewExportCmd(app),
|
||||||
|
cmd.NewImportCmd(app),
|
||||||
|
)
|
||||||
|
|
||||||
if err := c.Run(ctx); err != nil {
|
if err := root.Execute(); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user