minor rework and filter dimensions
This commit is contained in:
55
cmd/cli/commands/helper.go
Normal file
55
cmd/cli/commands/helper.go
Normal file
@ -0,0 +1,55 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.loyso.art/frx/eway/cmd/cli/components"
|
||||
"git.loyso.art/frx/eway/internal/entity"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
type empty entity.Empty
|
||||
|
||||
type action func(ctx context.Context, c *cli.Command) error
|
||||
|
||||
func decorateAction(a action) cli.ActionFunc {
|
||||
return func(ctx context.Context, c *cli.Command) error {
|
||||
var data [3]byte
|
||||
_, _ = rand.Read(data[:])
|
||||
reqid := hex.EncodeToString(data[:])
|
||||
|
||||
log, err := components.GetLogger()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting logger: %w", err)
|
||||
}
|
||||
|
||||
log = log.With().Str("reqid", reqid).Logger()
|
||||
rctx := log.WithContext(ctx)
|
||||
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
log.Info().Float64("elapsed", time.Since(start).Seconds()).Msg("command completed")
|
||||
}()
|
||||
|
||||
log.Info().Msg("command execution started")
|
||||
return a(rctx, c)
|
||||
}
|
||||
}
|
||||
|
||||
func cmdWithAction(cmd cli.Command, a action) *cli.Command {
|
||||
if a == nil {
|
||||
a = notImplementedAction
|
||||
}
|
||||
|
||||
cmd.Action = decorateAction(a)
|
||||
return &cmd
|
||||
}
|
||||
|
||||
func notImplementedAction(_ context.Context, _ *cli.Command) error {
|
||||
return entity.ErrNotImplemented
|
||||
}
|
||||
Reference in New Issue
Block a user