create product at ydb
This commit is contained in:
@ -15,14 +15,13 @@ import (
|
||||
"github.com/teris-io/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
debugOptName = "verbose"
|
||||
jsonOptName = "json"
|
||||
var (
|
||||
limitOption = cli.NewOption("limit", "Limits amount of items to return").WithType(cli.TypeInt)
|
||||
offsetOption = cli.NewOption("offset", "Offsets items to return").WithType(cli.TypeInt)
|
||||
debugOption = cli.NewOption("verbose", "Enables debug logging").WithChar('v').WithType(cli.TypeBool)
|
||||
jsonOption = cli.NewOption("json", "Sets output as json").WithType(cli.TypeBool)
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
type actionWrapper func(next cli.Action) cli.Action
|
||||
|
||||
func buildCLICommand(f func() cli.Command) cliCommand {
|
||||
@ -92,9 +91,29 @@ func actionWrapperParseConfig(next cli.Action) cli.Action {
|
||||
}
|
||||
}
|
||||
|
||||
func isJSONFormatEnabled(options map[string]string) bool {
|
||||
_, ok := options[jsonOption.Key()]
|
||||
return ok
|
||||
}
|
||||
|
||||
type outputEncoderF func(any) error
|
||||
|
||||
func makeOutputEncoder(options map[string]string) outputEncoderF {
|
||||
if isJSONFormatEnabled(options) {
|
||||
out := json.NewEncoder(defaultOutput)
|
||||
out.SetIndent("", " ")
|
||||
return out.Encode
|
||||
}
|
||||
|
||||
return outputEncoderF(func(a any) error {
|
||||
_, err := fmt.Fprintf(defaultOutput, "%#v", a)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func makeLogger(options map[string]string) *slog.Logger {
|
||||
level := slog.LevelInfo
|
||||
if _, ok := options[debugOptName]; ok {
|
||||
if _, ok := options[debugOption.Key()]; ok {
|
||||
level = slog.LevelDebug
|
||||
}
|
||||
|
||||
@ -103,7 +122,7 @@ func makeLogger(options map[string]string) *slog.Logger {
|
||||
}
|
||||
|
||||
var h slog.Handler
|
||||
if _, ok := options[jsonOptName]; ok {
|
||||
if isJSONFormatEnabled(options) {
|
||||
h = slog.NewJSONHandler(os.Stdout, &opts)
|
||||
} else {
|
||||
h = slog.NewTextHandler(os.Stdout, &opts)
|
||||
@ -113,7 +132,7 @@ func makeLogger(options map[string]string) *slog.Logger {
|
||||
}
|
||||
|
||||
func makeSravniClient(ctx context.Context, log *slog.Logger, options map[string]string) (sravni.Client, error) {
|
||||
_, isDebug := options[debugOptName]
|
||||
_, isDebug := options[debugOption.Key()]
|
||||
client, err := sravni.NewClient(ctx, log, isDebug)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("making new client: %w", err)
|
||||
@ -125,6 +144,8 @@ func makeSravniClient(ctx context.Context, log *slog.Logger, options map[string]
|
||||
type baseAction struct {
|
||||
ctx context.Context
|
||||
log *slog.Logger
|
||||
|
||||
out outputEncoderF
|
||||
}
|
||||
|
||||
func (ba *baseAction) getYDBConnection() (*adapters.YDBConnection, error) {
|
||||
@ -142,6 +163,7 @@ func (ba *baseAction) getYDBConnection() (*adapters.YDBConnection, error) {
|
||||
|
||||
func (ba *baseAction) parse(_ []string, options map[string]string) (err error) {
|
||||
ba.log = makeLogger(options).With(slog.String("component", "action"))
|
||||
ba.out = makeOutputEncoder(options)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user