CR is a simple and extensible command runner for Go that allows you to run commands/actions/procedures in a more convenient way.
- ✨ Easy to use, almost dependency-free
- 📦 Ability to use middlewares for your commands
- 👌 You can use validators for your commands
- 📩 Stamps to pass any data from middleware to command
go get -u github.com/revenkroz/cr
See the examples for more information.
We will use github.com/rs/zerolog/log
as logger.
package runner
import (
"github.com/revenkroz/cr/runner"
"github.com/rs/zerolog/log"
)
type Logger struct {
}
func (l *Logger) Logf(format string, args ...interface{}) {
log.Info().Msgf(format, args...)
}
func CreateLogger() *Logger {
return &Logger{}
}
type Runner struct {
*runner.Runner
}
func NewRunner() *Runner {
return &Runner{
Runner: runner.New(
runner.WithLogger(CreateLogger()),
),
}
}
package mydomain
import (
"errors"
"github.com/revenkroz/cr/runner"
)
type EchoArgs struct {
A int `json:"a"`
}
type EchoResponse struct {
C int `json:"c"`
}
type Echo struct {
}
func (h *Echo) Name() string {
return "MyDomain.Echo"
}
func (h *Echo) Handler() runner.HandlerFunc {
return runner.H(h.Echo)
}
func (h *Echo) Echo(ctx runner.Context, args *EchoArgs) (*EchoResponse, error) {
quo := &EchoResponse{
C: args.A * 2,
}
return quo, nil
}
// runnerService := NewRunner()
runnerService.Register(&mydomain.Echo{})
result := runnerService.RunOne(runner.NewContext(), &runner.Command{
Name: "MyDomain.Echo",
Params: map[string]interface{}{
"A": 4,
},
})