migrate to cli v3
This commit is contained in:
219
cmd/cli/main.go
219
cmd/cli/main.go
@ -25,7 +25,7 @@ import (
|
||||
"github.com/brianvoe/gofakeit/v6"
|
||||
"github.com/rodaine/table"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/urfave/cli"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -45,13 +45,13 @@ func main() {
|
||||
}
|
||||
|
||||
func runcli(ctx context.Context) (err error) {
|
||||
app := setupCLI(ctx)
|
||||
return app.Run(os.Args)
|
||||
app := setupCLI()
|
||||
return app.Run(ctx, os.Args)
|
||||
}
|
||||
|
||||
func setupDI(ctx context.Context) cli.BeforeFunc {
|
||||
return func(c *cli.Context) error {
|
||||
cfgpath := c.String("config")
|
||||
func setupDI() cli.BeforeFunc {
|
||||
return func(ctx context.Context, cmd *cli.Command) error {
|
||||
cfgpath := cmd.String("config")
|
||||
|
||||
err := components.SetupDI(ctx, cfgpath)
|
||||
if err != nil {
|
||||
@ -62,81 +62,85 @@ func setupDI(ctx context.Context) cli.BeforeFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func releaseDI(c *cli.Context) error {
|
||||
log, err := components.GetLogger()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting logger: %w", err)
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
since := time.Since(start)
|
||||
if err == nil {
|
||||
return
|
||||
func releaseDI() cli.AfterFunc {
|
||||
return func(ctx context.Context, c *cli.Command) error {
|
||||
log, err := components.GetLogger()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting logger: %w", err)
|
||||
}
|
||||
|
||||
log.Err(err).Dur("elapsed", since).Msg("shutdown finished")
|
||||
}()
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
since := time.Since(start)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
return components.Shutdown()
|
||||
log.Err(err).Dur("elapsed", since).Msg("shutdown finished")
|
||||
}()
|
||||
|
||||
return components.Shutdown()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func setupCLI(ctx context.Context) *cli.App {
|
||||
app := cli.NewApp()
|
||||
app.Flags = append(
|
||||
app.Flags,
|
||||
cli.StringFlag{
|
||||
Name: "config",
|
||||
Usage: "path to config in TOML format",
|
||||
Value: "config.toml",
|
||||
TakesFile: true,
|
||||
func setupCLI() *cli.Command {
|
||||
app := &cli.Command{
|
||||
Name: "cli",
|
||||
Description: "a cli for running eway logic",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "config",
|
||||
Usage: "path to config in TOML format",
|
||||
Value: "config.toml",
|
||||
TakesFile: true,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
app.Before = setupDI(ctx)
|
||||
app.After = releaseDI
|
||||
app.Commands = cli.Commands{
|
||||
newParseCmd(ctx),
|
||||
newImportCmd(ctx),
|
||||
newExportCmd(ctx),
|
||||
newViewCmd(ctx),
|
||||
Before: setupDI(),
|
||||
After: releaseDI(),
|
||||
|
||||
Commands: []*cli.Command{
|
||||
newParseCmd(),
|
||||
newImportCmd(),
|
||||
newExportCmd(),
|
||||
newViewCmd(),
|
||||
},
|
||||
}
|
||||
app.EnableBashCompletion = true
|
||||
app.BashComplete = cli.DefaultAppComplete
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func newParseCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newParseCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "parse",
|
||||
Usage: "category for parsing items from various sources",
|
||||
Subcommands: cli.Commands{
|
||||
newParseEwayCmd(ctx),
|
||||
Commands: []*cli.Command{
|
||||
newParseEwayCmd(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newParseEwayCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newParseEwayCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "eway",
|
||||
Usage: "parse all available eway goods",
|
||||
Action: decorateAction(ctx, parseEwayAction),
|
||||
Action: decorateAction(parseEwayAction),
|
||||
}
|
||||
}
|
||||
|
||||
func newImportCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newImportCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "import",
|
||||
Usage: "category for importing data from sources",
|
||||
Subcommands: cli.Commands{
|
||||
newImportFromFileCmd(ctx),
|
||||
Commands: []*cli.Command{
|
||||
newImportFromFileCmd(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newImportFromFileCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newImportFromFileCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "file",
|
||||
Usage: "imports from file into db",
|
||||
Flags: []cli.Flag{
|
||||
@ -147,29 +151,28 @@ func newImportFromFileCmd(ctx context.Context) cli.Command {
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
Action: decorateAction(ctx, importFromFileAction),
|
||||
Action: decorateAction(importFromFileAction),
|
||||
}
|
||||
}
|
||||
|
||||
func newExportCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newExportCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "export",
|
||||
Usage: "category for exporting stored data",
|
||||
Subcommands: cli.Commands{
|
||||
newExportYMLCatalogCmd(ctx),
|
||||
Commands: []*cli.Command{
|
||||
newExportYMLCatalogCmd(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newExportYMLCatalogCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newExportYMLCatalogCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "yml_catalog",
|
||||
Usage: "export data into as yml_catalog in xml format",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "dst",
|
||||
Name: "out",
|
||||
Usage: "destination path",
|
||||
Required: true,
|
||||
Value: "yml_catalog.xml",
|
||||
TakesFile: true,
|
||||
},
|
||||
@ -182,70 +185,70 @@ func newExportYMLCatalogCmd(ctx context.Context) cli.Command {
|
||||
Usage: "prettify output",
|
||||
},
|
||||
},
|
||||
Action: decorateAction(ctx, exportYMLCatalogAction),
|
||||
Action: decorateAction(exportYMLCatalogAction),
|
||||
}
|
||||
}
|
||||
|
||||
func newTestCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newTestCmd(ctx context.Context) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "test",
|
||||
Usage: "various commands for testing",
|
||||
Subcommands: cli.Commands{
|
||||
newTestFBSCmd(ctx),
|
||||
Commands: []*cli.Command{
|
||||
newTestFBSCmd(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newTestFBSCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newTestFBSCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "fbs",
|
||||
Usage: "serialize and deserialize gooditem entity",
|
||||
Action: decorateAction(ctx, testFBSAction),
|
||||
Action: decorateAction(testFBSAction),
|
||||
}
|
||||
}
|
||||
|
||||
func newViewCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newViewCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "view",
|
||||
Usage: "Set of commands to view the data inside db",
|
||||
Subcommands: []cli.Command{
|
||||
newViewCategoriesCmd(ctx),
|
||||
newViewItemsCmd(ctx),
|
||||
Commands: []*cli.Command{
|
||||
newViewCategoriesCmd(),
|
||||
newViewItemsCmd(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newViewCategoriesCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newViewCategoriesCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "categories",
|
||||
Usage: "Set of commands to work with categories",
|
||||
Subcommands: []cli.Command{
|
||||
newViewCategoriesListCmd(ctx),
|
||||
Commands: []*cli.Command{
|
||||
newViewCategoriesListCmd(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newViewItemsCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newViewItemsCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "items",
|
||||
Usage: "Set of command to work with items",
|
||||
Subcommands: cli.Commands{
|
||||
newViewItemsGetCmd(ctx),
|
||||
newViewItemsCountCmd(ctx),
|
||||
Commands: []*cli.Command{
|
||||
newViewItemsGetCmd(),
|
||||
newViewItemsCountCmd(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newViewItemsCountCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newViewItemsCountCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "count",
|
||||
Usage: "iterates over collection and counts number of items",
|
||||
Action: decorateAction(ctx, viewItemsCountAction),
|
||||
Action: decorateAction(viewItemsCountAction),
|
||||
}
|
||||
}
|
||||
|
||||
func newViewItemsGetCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newViewItemsGetCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "get",
|
||||
Usage: "gets goods item by its id",
|
||||
Flags: []cli.Flag{
|
||||
@ -253,17 +256,17 @@ func newViewItemsGetCmd(ctx context.Context) cli.Command {
|
||||
Name: "id",
|
||||
Usage: "id of the goods item. Either id or cart-id should be set",
|
||||
},
|
||||
&cli.Int64Flag{
|
||||
&cli.IntFlag{
|
||||
Name: "cart-id",
|
||||
Usage: "cart-id of the item. Either cart-id or id should be set",
|
||||
},
|
||||
},
|
||||
Action: decorateAction(ctx, viewItemsGetAction),
|
||||
Action: decorateAction(viewItemsGetAction),
|
||||
}
|
||||
}
|
||||
|
||||
func newViewCategoriesListCmd(ctx context.Context) cli.Command {
|
||||
return cli.Command{
|
||||
func newViewCategoriesListCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "list",
|
||||
Usage: "lists stored categories stored in database",
|
||||
Flags: []cli.Flag{
|
||||
@ -282,22 +285,22 @@ func newViewCategoriesListCmd(ctx context.Context) cli.Command {
|
||||
Usage: "prints total count of categories",
|
||||
},
|
||||
},
|
||||
Action: decorateAction(ctx, viewCategoriesListAction),
|
||||
Action: decorateAction(viewCategoriesListAction),
|
||||
}
|
||||
}
|
||||
|
||||
func viewItemsGetAction(ctx context.Context, c *cli.Context) error {
|
||||
func viewItemsGetAction(ctx context.Context, c *cli.Command) error {
|
||||
r, err := components.GetRepository()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting repository: %w", err)
|
||||
}
|
||||
|
||||
id := c.String("id")
|
||||
cartID := c.Int64("cart-id")
|
||||
cartID := c.Int("cart-id")
|
||||
if id == "" && cartID == 0 {
|
||||
return cli.NewExitError("oneof: id or cart-id should be set", 1)
|
||||
return cli.Exit("oneof: id or cart-id should be set", 1)
|
||||
} else if id != "" && cartID != 0 {
|
||||
return cli.NewExitError("oneof: id or cart-id should be set", 1)
|
||||
return cli.Exit("oneof: id or cart-id should be set", 1)
|
||||
}
|
||||
|
||||
var item entity.GoodsItem
|
||||
@ -321,7 +324,7 @@ func viewItemsGetAction(ctx context.Context, c *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func viewItemsCountAction(ctx context.Context, c *cli.Context) error {
|
||||
func viewItemsCountAction(ctx context.Context, c *cli.Command) error {
|
||||
r, err := components.GetRepository()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting repository: %w", err)
|
||||
@ -360,7 +363,7 @@ func viewItemsCountAction(ctx context.Context, c *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func viewCategoriesListAction(ctx context.Context, c *cli.Context) error {
|
||||
func viewCategoriesListAction(ctx context.Context, c *cli.Command) error {
|
||||
r, err := components.GetRepository()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting repository: %w", err)
|
||||
@ -371,8 +374,8 @@ func viewCategoriesListAction(ctx context.Context, c *cli.Context) error {
|
||||
return fmt.Errorf("listing categories: %w", err)
|
||||
}
|
||||
|
||||
limit := c.Int("limit")
|
||||
page := c.Int("page")
|
||||
limit := int(c.Int("limit"))
|
||||
page := int(c.Int("page"))
|
||||
total := len(categories)
|
||||
|
||||
if page == 0 {
|
||||
@ -410,7 +413,7 @@ func viewCategoriesListAction(ctx context.Context, c *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func importFromFileAction(ctx context.Context, c *cli.Context) error {
|
||||
func importFromFileAction(ctx context.Context, c *cli.Command) error {
|
||||
const maxBatch = 2000
|
||||
|
||||
r, err := components.GetRepository()
|
||||
@ -526,10 +529,10 @@ func importFromFileAction(ctx context.Context, c *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type action func(ctx context.Context, c *cli.Context) error
|
||||
type action func(ctx context.Context, c *cli.Command) error
|
||||
|
||||
func decorateAction(ctx context.Context, a action) cli.ActionFunc {
|
||||
return func(c *cli.Context) error {
|
||||
func decorateAction(a action) cli.ActionFunc {
|
||||
return func(ctx context.Context, c *cli.Command) error {
|
||||
var data [3]byte
|
||||
_, _ = rand.Read(data[:])
|
||||
reqid := hex.EncodeToString(data[:])
|
||||
@ -552,7 +555,7 @@ func decorateAction(ctx context.Context, a action) cli.ActionFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func exportYMLCatalogAction(ctx context.Context, c *cli.Context) error {
|
||||
func exportYMLCatalogAction(ctx context.Context, c *cli.Command) error {
|
||||
path := c.String("dst")
|
||||
limit := c.Int("limit")
|
||||
pretty := c.Bool("pretty")
|
||||
@ -639,7 +642,7 @@ func exportYMLCatalogAction(ctx context.Context, c *cli.Context) error {
|
||||
return enc.Encode(container)
|
||||
}
|
||||
|
||||
func parseEwayAction(ctx context.Context, c *cli.Context) error {
|
||||
func parseEwayAction(ctx context.Context, c *cli.Command) error {
|
||||
client, err := components.GetEwayClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting eway client: %w", err)
|
||||
@ -806,7 +809,7 @@ func goodsItemAsOffer(in entity.GoodsItem, categoryIDByName map[string]int64) (o
|
||||
return out
|
||||
}
|
||||
|
||||
func testFBSAction(ctx context.Context, c *cli.Context) error {
|
||||
func testFBSAction(ctx context.Context, c *cli.Command) error {
|
||||
var gooditem entity.GoodsItem
|
||||
err := gofakeit.Struct(&gooditem)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user