migrate to cli v3

This commit is contained in:
2024-01-28 17:08:45 +03:00
parent bfa105df95
commit c814f27c54
3 changed files with 123 additions and 109 deletions

View File

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

5
go.mod
View File

@ -17,7 +17,7 @@ require (
require ( require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.0.0 // indirect github.com/golang/glog v1.0.0 // indirect
@ -29,6 +29,9 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/urfave/cli/v3 v3.0.0-alpha8 // indirect
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
go.opencensus.io v0.22.5 // indirect go.opencensus.io v0.22.5 // indirect
golang.org/x/net v0.17.0 // indirect golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect golang.org/x/sys v0.13.0 // indirect

8
go.sum
View File

@ -11,6 +11,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -82,6 +84,12 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk= github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA= github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/urfave/cli/v3 v3.0.0-alpha8 h1:H+qxFPoCkGzdF8KUMs2fEOZl5io/1QySgUiGfar8occ=
github.com/urfave/cli/v3 v3.0.0-alpha8/go.mod h1:0kK/RUFHyh+yIKSfWxwheGndfnrvYSmYFVeKCh03ZUc=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=