95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
|
|
"git.loyso.art/frx/kurious"
|
|
|
|
"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())
|
|
}
|
|
|
|
return a
|
|
},
|
|
}))
|
|
|
|
ec, err := app(ctx, log)
|
|
if err != nil {
|
|
slog.ErrorContext(ctx, "unable to run app", slog.Any("error", err))
|
|
}
|
|
|
|
os.Exit(ec)
|
|
}
|
|
|
|
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, "unable to make client", slog.Any("err", err))
|
|
return -1
|
|
}
|
|
|
|
state, err := client.GetMainPageState()
|
|
if err != nil {
|
|
log.ErrorContext(ctx, "unable to make page state", slog.Any("err", err))
|
|
return -1
|
|
}
|
|
|
|
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))
|
|
|
|
return 0
|
|
})
|
|
|
|
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
|
|
}
|