Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 9 days 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 (9 months ago)
- Default Branch: main
- Last Pushed: 2024-05-21T12:57:10.000Z (9 months ago)
- Last Synced: 2024-11-25T14:15:20.853Z (2 months 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
- ✨ 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
```bash
go get -u github.com/revenkroz/cr
```## Usage
See the [examples](./example) for more information.
### 1. Create runner
We will use `github.com/rs/zerolog/log` as logger.
```go
package runnerimport (
"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
```go
package mydomainimport (
"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
```go
// runnerService := NewRunner()
runnerService.Register(&mydomain.Echo{})
```### 4. Run
```go
result := runnerService.RunOne(runner.NewContext(), &runner.Command{
Name: "MyDomain.Echo",
Params: map[string]interface{}{
"A": 4,
},
})
```