minor imporvments

This commit is contained in:
Aleksandr Trushkin
2024-02-06 19:58:57 +03:00
parent 2352ebb942
commit 52f1280c3c
2 changed files with 86 additions and 78 deletions

View File

@ -39,7 +39,7 @@ func GetLogger() (zerolog.Logger, error) {
return do.Invoke[zerolog.Logger](diInjector)
}
func SetupDI(ctx context.Context, cfgpath string) error {
func SetupDI(ctx context.Context, cfgpath string, verbose bool, logAsJSON bool) error {
cfg, err := parseSettings(cfgpath)
if err != nil {
// if no settings provided allow cli to run without them.
@ -57,9 +57,17 @@ func SetupDI(ctx context.Context, cfgpath string) error {
wr.TimeFormat = time.RFC3339
}
log := zerolog.New(zerolog.NewConsoleWriter(tsSet)).With().Timestamp().Str("app", "converter").Logger()
var writer io.Writer = zerolog.NewConsoleWriter(tsSet)
if logAsJSON {
writer = os.Stdout
}
return log, nil
log := zerolog.New(writer).With().Timestamp().Str("app", "converter").Logger()
if verbose {
return log.Level(zerolog.DebugLevel), nil
}
return log.Level(zerolog.InfoLevel), nil
})
do.Provide[eway.Client](diInjector, func(i *do.Injector) (eway.Client, error) {

View File

@ -17,28 +17,26 @@ import (
"os"
"os/signal"
"strconv"
"syscall"
"time"
rooteway "git.loyso.art/frx/eway"
"git.loyso.art/frx/eway/cmd/cli/components"
"git.loyso.art/frx/eway/internal/crypto"
"git.loyso.art/frx/eway/internal/encoding/fbs"
"git.loyso.art/frx/eway/internal/entity"
"git.loyso.art/frx/eway/internal/export"
"git.loyso.art/frx/eway/internal/interconnect/eway"
"github.com/brianvoe/gofakeit/v6"
"github.com/rodaine/table"
"github.com/rs/zerolog"
"github.com/urfave/cli/v3"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer func() {
cancel()
}()
err := runcli(ctx)
if err != nil {
@ -57,8 +55,10 @@ func runcli(ctx context.Context) (err error) {
func setupDI() cli.BeforeFunc {
return func(ctx context.Context, cmd *cli.Command) error {
cfgpath := cmd.String("config")
debugLevel := cmd.Bool("verbose")
jsonFormat := cmd.Bool("json")
err := components.SetupDI(ctx, cfgpath)
err := components.SetupDI(ctx, cfgpath, debugLevel, jsonFormat)
if err != nil {
return fmt.Errorf("setting up di: %w", err)
}
@ -75,7 +75,7 @@ func releaseDI() cli.AfterFunc {
func setupCLI() *cli.Command {
app := &cli.Command{
Name: "cli",
Name: "ewaycli",
Description: "a cli for running eway logic",
Version: fmt.Sprintf("%s (%s) %s", rooteway.Version(), rooteway.Commit(), rooteway.BuildTime()),
Flags: []cli.Flag{
@ -85,6 +85,16 @@ func setupCLI() *cli.Command {
Value: "config.toml",
TakesFile: true,
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"d"},
Usage: "enables verbose logging",
},
&cli.BoolFlag{
Name: "json",
Usage: "enables json log format",
Persistent: true,
},
},
Before: setupDI(),
@ -103,12 +113,6 @@ func setupCLI() *cli.Command {
return app
}
func newVersionCmd() *cli.Command {
return &cli.Command{
Name: "version",
}
}
func newCryptoCmd() *cli.Command {
return &cli.Command{
Name: "crypto",
@ -209,9 +213,10 @@ func newParseEwayGetCmd() *cli.Command {
Usage: "loads information about the product by parsing product's html page",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "cart",
Aliases: []string{"c"},
Usage: "cart of the product",
Name: "cart",
Aliases: []string{"c"},
Usage: "cart of the product",
Required: true,
},
},
Action: decorateAction(parseEwayGetAction),
@ -237,6 +242,10 @@ func newParseEwayListCmd() *cli.Command {
Name: "min-stock",
Usage: "filters by minimum available items in stock",
},
&cli.BoolFlag{
Name: "pretty",
Usage: "pretty prints output",
},
},
Action: decorateAction(parseEwayListAction),
}
@ -311,24 +320,6 @@ func newExportYMLCatalogCmd() *cli.Command {
}
}
func newTestCmd(ctx context.Context) *cli.Command {
return &cli.Command{
Name: "test",
Usage: "various commands for testing",
Commands: []*cli.Command{
newTestFBSCmd(),
},
}
}
func newTestFBSCmd() *cli.Command {
return &cli.Command{
Name: "fbs",
Usage: "serialize and deserialize gooditem entity",
Action: decorateAction(testFBSAction),
}
}
func newViewCmd() *cli.Command {
return &cli.Command{
Name: "view",
@ -803,7 +794,7 @@ func parseEwayListAction(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("getting logger: %w", err)
}
start := page * limit
start := (page - 1) * limit
items, total, err := client.GetGoodsNew(ctx, eway.GetGoodsNewParams{
Draw: 1,
@ -826,8 +817,12 @@ func parseEwayListAction(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("getting remnants: %w", err)
}
tbl := table.New("sku", "category", "cart", "stock", "price", "parameters")
goodsItems := make([]entity.GoodsItem, 0, len(items))
for _, item := range items {
if ctx.Err() != nil {
break
}
pi, err := client.GetProductInfo(ctx, int64(item.Cart))
if err != nil {
log.Warn().Err(err).Msg("unable to get product info")
@ -837,21 +832,55 @@ func parseEwayListAction(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("making goods item: %w", err)
}
parameters, _ := json.MarshalIndent(outGood.Parameters, "", " ")
goodsItems = append(goodsItems, outGood)
tbl.AddRow(
outGood.Articul,
outGood.Type,
outGood.Cart,
outGood.Stock,
outGood.Price,
string(parameters),
)
}
tbl.Print()
var stats = struct {
Handled int `json:"handled"`
Loaded int `json:"loaded"`
Total int `json:"total"`
}{
Handled: len(goodsItems),
Loaded: len(items),
Total: total,
}
if cmd.Bool("json") {
enc := json.NewEncoder(cmd.Writer)
if cmd.Bool("pretty") {
enc.SetIndent("", " ")
_ = enc.Encode(goodsItems)
}
enc.SetIndent("", "")
_ = enc.Encode(stats)
} else {
tbl := table.
New("sku", "category", "cart", "stock", "price", "parameters").
WithWriter(cmd.Writer)
for _, outGood := range goodsItems {
parameters, _ := json.MarshalIndent(outGood.Parameters, "", " ")
tbl.AddRow(
outGood.Articul,
outGood.Type,
outGood.Cart,
outGood.Stock,
outGood.Price,
string(parameters),
)
}
tbl.Print()
table.
New("handled", "loaded", "total").
WithWriter(cmd.Writer).
AddRow(stats.Handled, stats.Loaded, stats.Total).
Print()
}
println("total:", total)
return nil
}
@ -1091,31 +1120,6 @@ func goodsItemAsOffer(in entity.GoodsItem, categoryIDByName map[string]int64) (o
return out
}
func testFBSAction(ctx context.Context, c *cli.Command) error {
var gooditem entity.GoodsItem
err := gofakeit.Struct(&gooditem)
if err != nil {
return fmt.Errorf("faking struct: %w", err)
}
data := fbs.MakeDomainGoodItemFinished(gooditem)
datahexed := hex.EncodeToString(data)
println(datahexed)
got, err := fbs.ParseGoodsItem(data)
if err != nil {
return fmt.Errorf("parsing: %w", err)
}
if got.Articul != gooditem.Articul {
gotStr := fmt.Sprintf("%v", got)
hasStr := fmt.Sprintf("%v", gooditem)
println(gotStr, "\n", hasStr)
}
return nil
}
func appYMLExporterAction(ctx context.Context, cmd *cli.Command) error {
port := cmd.Int("port")
src := cmd.String("src")
@ -1195,10 +1199,6 @@ func appYMLExporterAction(ctx context.Context, cmd *cli.Command) error {
return nil
}
var (
someDumbKey = []byte("9530e001b619e8e98a889055f06821bb")
)
func cryptoDeEncryptAction(encrypt bool) cli.ActionFunc {
return func(ctx context.Context, c *cli.Command) (err error) {
value := c.String("text")