Skip to content
/ cr Public

Golang Command Runner. Run any commands/actions/procedures through one interface

License

Notifications You must be signed in to change notification settings

revenkroz/cr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CR — Go Command Runner

CR is a simple and extensible command runner for Go that allows you to run commands/actions/procedures in a more convenient way.

Features

  • ✨ 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

Installation

go get -u github.com/revenkroz/cr

Usage

See the examples for more information.

1. Create runner

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()),
		),
	}
}

2. Create command

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
}

3. Register command

// runnerService := NewRunner()
runnerService.Register(&mydomain.Echo{})

4. Run

result := runnerService.RunOne(runner.NewContext(), &runner.Command{
    Name: "MyDomain.Echo",
    Params: map[string]interface{}{
        "A": 4,
    },
})

About

Golang Command Runner. Run any commands/actions/procedures through one interface

Topics

Resources

License

Stars

Watchers

Forks

Languages