able to list products
This commit is contained in:
50
cmd/dev/sravnicli/core.go
Normal file
50
cmd/dev/sravnicli/core.go
Normal file
@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"git.loyso.art/frx/kurious/internal/infrastructure/interfaceadapters/courses/sravni"
|
||||
|
||||
"github.com/teris-io/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
debugOptName = "verbose"
|
||||
jsonOptName = "json"
|
||||
)
|
||||
|
||||
var limitOption = cli.NewOption("limit", "Limits amount of items to return").WithType(cli.TypeInt)
|
||||
var offsetOption = cli.NewOption("offset", "Offsets items to return").WithType(cli.TypeInt)
|
||||
|
||||
func makeLogger(options map[string]string) *slog.Logger {
|
||||
level := slog.LevelInfo
|
||||
if _, ok := options[debugOptName]; ok {
|
||||
level = slog.LevelDebug
|
||||
}
|
||||
|
||||
opts := slog.HandlerOptions{
|
||||
Level: level,
|
||||
}
|
||||
|
||||
var h slog.Handler
|
||||
if _, ok := options[jsonOptName]; ok {
|
||||
h = slog.NewJSONHandler(os.Stdout, &opts)
|
||||
} else {
|
||||
h = slog.NewTextHandler(os.Stdout, &opts)
|
||||
}
|
||||
|
||||
return slog.New(h)
|
||||
}
|
||||
|
||||
func makeSravniClient(ctx context.Context, log *slog.Logger, options map[string]string) (sravni.Client, error) {
|
||||
_, isDebug := options[debugOptName]
|
||||
client, err := sravni.NewClient(ctx, log, isDebug)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("making new client: %w", err)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
@ -2,59 +2,88 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"git.loyso.art/frx/kurious"
|
||||
"git.loyso.art/frx/kurious/internal/infrastructure/interfaceadapters/courses/sravni"
|
||||
|
||||
"github.com/teris-io/cli"
|
||||
)
|
||||
|
||||
var defaultOutput = os.Stdout
|
||||
|
||||
func main() {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelDebug,
|
||||
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == slog.TimeKey {
|
||||
a.Value = slog.Int64Value(a.Value.Time().Unix())
|
||||
}
|
||||
Level: slog.LevelDebug,
|
||||
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == slog.TimeKey {
|
||||
a.Value = slog.Int64Value(a.Value.Time().Unix())
|
||||
}
|
||||
|
||||
return a
|
||||
},
|
||||
}))
|
||||
return a
|
||||
},
|
||||
}))
|
||||
|
||||
version, commit, bt := kurious.Version(), kurious.Commit(), kurious.BuildTime()
|
||||
pid := os.Getpid()
|
||||
ec, err := app(ctx, log)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "unable to run app", slog.Any("error", err))
|
||||
}
|
||||
|
||||
log.InfoContext(
|
||||
ctx, "running app",
|
||||
slog.Int("pid", pid),
|
||||
slog.String("version", version),
|
||||
slog.String("commit", commit),
|
||||
slog.Time("build_time", bt),
|
||||
)
|
||||
|
||||
err := app(ctx, log)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "unable to run app", slog.Any("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(ec)
|
||||
}
|
||||
|
||||
func app(ctx context.Context, log *slog.Logger) error {
|
||||
client, err := sravni.NewClient(ctx, log, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("making new client: %w", err)
|
||||
}
|
||||
func setupCLI(ctx context.Context) cli.App {
|
||||
mainPageState := cli.NewCommand("state", "Loads redux state").
|
||||
WithOption(cli.NewOption("part", "Prints part of the model [all,config,dicts,learning,courses]").WithType(cli.TypeString)).
|
||||
WithAction(func(args []string, options map[string]string) int {
|
||||
log := makeLogger(options)
|
||||
client, err := makeSravniClient(ctx, log, options)
|
||||
if err != nil {
|
||||
log.ErrorContext(ctx, "making client", slog.Any("err", err))
|
||||
return -1
|
||||
}
|
||||
state := client.GetMainPageState()
|
||||
|
||||
meta := client.GetMainPageState()
|
||||
var out any
|
||||
switch options["part"] {
|
||||
case "", "all":
|
||||
out = state
|
||||
case "config":
|
||||
out = state.RuntimeConfig
|
||||
case "dicts":
|
||||
out = state.Props.InitialReduxState.Dictionaries
|
||||
case "learning":
|
||||
out = state.Props.InitialReduxState.Dictionaries.Data.LearningType
|
||||
case "courses":
|
||||
out = state.Props.InitialReduxState.Dictionaries.Data.CourseThematics
|
||||
}
|
||||
log.InfoContext(ctx, "loaded state", slog.Any("state", out))
|
||||
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
return 0
|
||||
})
|
||||
|
||||
return enc.Encode(meta)
|
||||
mainCategory := cli.NewCommand("main", "Main page interaction").
|
||||
WithCommand(mainPageState)
|
||||
|
||||
apiCategory := setupAPICommand(ctx)
|
||||
description := fmt.Sprintf("sravni dev cli %s (%s)", kurious.Version(), kurious.Commit())
|
||||
cliApp := cli.New(description).
|
||||
WithOption(cli.NewOption("verbose", "Verbose execution").WithChar('v').WithType(cli.TypeBool)).
|
||||
WithOption(cli.NewOption("json", "JSON outpu format").WithType(cli.TypeBool)).
|
||||
WithCommand(mainCategory).
|
||||
WithCommand(apiCategory)
|
||||
|
||||
return cliApp
|
||||
}
|
||||
|
||||
func app(ctx context.Context, log *slog.Logger) (exitCode int, err error) {
|
||||
devCLI := setupCLI(ctx)
|
||||
exitCode = devCLI.Run(os.Args, defaultOutput)
|
||||
|
||||
return exitCode, nil
|
||||
}
|
||||
|
||||
160
cmd/dev/sravnicli/products.go
Normal file
160
cmd/dev/sravnicli/products.go
Normal file
@ -0,0 +1,160 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
|
||||
"git.loyso.art/frx/kurious/internal/domain"
|
||||
"git.loyso.art/frx/kurious/internal/infrastructure/interfaceadapters/courses/sravni"
|
||||
|
||||
"github.com/teris-io/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
learningTypeOptName = "learning_type"
|
||||
courseThematicOptName = "course_thematic"
|
||||
)
|
||||
|
||||
func setupAPICommand(ctx context.Context) cli.Command {
|
||||
learningTypeOpt := cli.NewOption(learningTypeOptName, "Specify learning type").
|
||||
WithChar('l').
|
||||
WithType(cli.TypeString)
|
||||
courseThematic := cli.NewOption(courseThematicOptName, "Specify course thematic").
|
||||
WithChar('t').
|
||||
WithType(cli.TypeString)
|
||||
|
||||
apiEducationListProducts := cli.NewCommand("list_products", "List products by some filters").
|
||||
WithOption(learningTypeOpt).
|
||||
WithOption(courseThematic).
|
||||
WithOption(limitOption).
|
||||
WithOption(offsetOption).
|
||||
WithAction(newListProductAction(ctx))
|
||||
|
||||
apiEducation := cli.NewCommand("education", "Education related category").
|
||||
WithCommand(apiEducationListProducts)
|
||||
|
||||
return cli.NewCommand("api", "Interaction with API").
|
||||
WithCommand(apiEducation)
|
||||
}
|
||||
|
||||
type action interface {
|
||||
context() context.Context
|
||||
parse(args []string, options map[string]string) error
|
||||
handle() error
|
||||
}
|
||||
|
||||
func asCLIAction(a action) cli.Action {
|
||||
return func(args []string, options map[string]string) int {
|
||||
ctx := a.context()
|
||||
log := makeLogger(options)
|
||||
err := a.parse(args, options)
|
||||
if err != nil {
|
||||
log.ErrorContext(ctx, "unable to parse args and opts", slog.Any("err", err))
|
||||
return -1
|
||||
}
|
||||
err = a.handle()
|
||||
if err != nil {
|
||||
log.ErrorContext(ctx, "unable to handle action", slog.Any("err", err))
|
||||
return -1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
type baseAction struct {
|
||||
ctx context.Context
|
||||
client sravni.Client
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func (ba *baseAction) parse(_ []string, options map[string]string) (err error) {
|
||||
ba.log = makeLogger(options).With(slog.String("component", "action"))
|
||||
ba.client, err = makeSravniClient(ba.ctx, ba.log, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ba *baseAction) handle() error {
|
||||
return domain.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (ba baseAction) context() context.Context {
|
||||
return ba.ctx
|
||||
}
|
||||
|
||||
func newBaseAction(ctx context.Context) *baseAction {
|
||||
return &baseAction{
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
type listProductsActionParams struct {
|
||||
learningType string
|
||||
courseThematic string
|
||||
limit int
|
||||
offset int
|
||||
}
|
||||
|
||||
type listProductsAction struct {
|
||||
*baseAction
|
||||
|
||||
params listProductsActionParams
|
||||
}
|
||||
|
||||
func newListProductAction(ctx context.Context) cli.Action {
|
||||
action := &listProductsAction{
|
||||
baseAction: newBaseAction(ctx),
|
||||
}
|
||||
|
||||
return asCLIAction(action)
|
||||
}
|
||||
|
||||
func (a *listProductsAction) parse(args []string, options map[string]string) error {
|
||||
err := a.baseAction.parse(args, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var ok bool
|
||||
|
||||
a.params.learningType, ok = options[learningTypeOptName]
|
||||
if !ok {
|
||||
return domain.SimpleError("learning_type is empty")
|
||||
}
|
||||
a.params.courseThematic, ok = options[courseThematicOptName]
|
||||
if !ok {
|
||||
return domain.SimpleError("course_thematic is empty")
|
||||
}
|
||||
|
||||
if value, ok := options[limitOption.Key()]; ok {
|
||||
a.params.limit, _ = strconv.Atoi(value)
|
||||
}
|
||||
if value, ok := options[offsetOption.Key()]; ok {
|
||||
a.params.offset, _ = strconv.Atoi(value)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *listProductsAction) handle() error {
|
||||
params := sravni.ListEducationProductsParams{
|
||||
LearningType: a.params.learningType,
|
||||
CoursesThematics: a.params.courseThematic,
|
||||
Limit: a.params.limit,
|
||||
Offset: a.params.offset,
|
||||
}
|
||||
result, err := a.client.ListEducationalProducts(a.ctx, params)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listing education products: %w", err)
|
||||
}
|
||||
|
||||
a.log.InfoContext(a.ctx, "list education products result", slog.Any("result", result))
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user