able to list products
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user