Shell generator built on top of GO Prompt (a library for building powerful interactive prompts). Griffin allows to bind command and actions by configuration in order to reduce boilerplate code and let developer focus on the action handling.
package main
import (
"fmt"
"github.com/margostino/griffin/pkg/griffin"
)
var Actions = map[string]func(){
"ExecuteDoSomething": ExecuteDoSomething,
}
var InputActions = map[string]func([]string){
"ExecuteDoSomethingWithInput": ExecuteDoSomethingWithInput,
}
func ExecuteDoSomething() {
fmt.Println("do something")
}
func ExecuteSelectSomething(args []string) {
fmt.Println("select something %s", args[0])
}
func main() {
powershell := griffin.New().
SetActions(Actions).
SetActionsStrings(InputActions).
LoadConfiguration("commands.yml")
powershell.Start()
}
Example:
commands:
- id: select something
description: "Select something amazing"
action: ExecuteDoSomething
- id: select input
args: 1
description: "Select input"
pattern: "^select something [a-z-A-Z]+$"
action: ExecuteSelectSomething
A shell can be created with Actions and Commands. A command might be: run job
.
An action is a function name without params: ExecuteSomething()
or a function name with string
params: ExecuteSomething(name string)
. Currently, only string params are supported.
Configuration might be loaded by YML file (see example) or sending a list
of CommandConfiguration
:
type CommandConfiguration struct {
Id string `yaml:"id"`
Description string `yaml:"description"`
Args int `yaml:"args"`
Action string `yaml:"action"`
Pattern string `yaml:"pattern"`
}
ID
: the command keys to trigger an action.
Example: run job
, help
, show users
Description
: short description of the command and action. This will be visible in as prompt suggestion.
Args
: amount of parameters that the action needs to be executed. Default 0
Action
: name of the function handler to serve the command ID.
Pattern
: if Args > 1
a regex pattern is used to match the command with action.