https://github.com/revenkroz/cr
Golang Command Runner. Run any commands/actions/procedures through one interface
https://github.com/revenkroz/cr
golang rpc runner
Last synced: about 2 months ago
JSON representation
Golang Command Runner. Run any commands/actions/procedures through one interface
- Host: GitHub
- URL: https://github.com/revenkroz/cr
- Owner: revenkroz
- License: mit
- Created: 2024-05-18T11:18:10.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-21T12:57:10.000Z (almost 2 years ago)
- Last Synced: 2025-01-25T10:24:13.428Z (about 1 year ago)
- Topics: golang, rpc, runner
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
- Zero dependencies
- Middleware support
- Context validators
- Stamps to pass data from middleware to command
- Parallel command execution
- Type-safe generic helpers
## Installation
```bash
go get -u github.com/revenkroz/cr/v2
```
## Usage
See the [examples](./example) for more information.
### 1. Create runner
```go
import (
"github.com/revenkroz/cr/v2"
"github.com/revenkroz/cr/v2/middleware"
)
r := cr.New(
cr.WithLogger(cr.NewStdLogger()),
cr.WithMiddleware(middleware.Logger(cr.NewStdLogger())),
)
```
### 2. Create command
```go
package mydomain
import (
"github.com/revenkroz/cr/v2"
)
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() cr.HandlerFunc {
return cr.H(h.Echo)
}
func (h *Echo) Echo(ctx cr.Context, args *EchoArgs) (*EchoResponse, error) {
return &EchoResponse{C: args.A * 2}, nil
}
```
### 3. Register command
```go
r.MustRegister(&mydomain.Echo{})
```
### 4. Run
```go
result := r.RunOne(cr.NewContext(), &cr.Command{
Name: "MyDomain.Echo",
Params: json.RawMessage(`{"a": 4}`),
})
```
### Parallel execution
```go
commands := []*cr.Command{
{Name: "MyDomain.Echo", Params: json.RawMessage(`{"a": 1}`)},
{Name: "MyDomain.Echo", Params: json.RawMessage(`{"a": 2}`)},
}
results := r.Run(cr.NewContext(), commands, true)
```