Documentation
¶
Index ¶
- Variables
- func GetRegisteredCommands() map[string]func(ectx *ExecutorContext) Command
- func MaybeEvalValue(val any, variables map[string]any) any
- func NewIncludeCommand(storage fs.ReadFileFS) func(ectx *ExecutorContext) Command
- func NewIncludeCommandWithBasePath(storage fs.ReadFileFS, basepath string) func(ectx *ExecutorContext) Command
- func RegisterCommand(name string, cmd func(ectx *ExecutorContext) Command)
- func RegisterValueFunc(name string, fn any)
- func ShellEscape(cmd string) string
- func WithCommandTypes(types map[string]func(ectx *ExecutorContext) Command) func(ex *Executor)
- func WithCommands(cmds []Command) func(ex *Executor)
- func WithDefaultEvaluatorFunctions() func(ex *Executor)
- func WithEvaluatorFunction(name string, fn govaluate.ExpressionFunction) func(ex *Executor)
- func WithEvaluatorFunctions(funcs map[string]govaluate.ExpressionFunction) func(ex *Executor)
- func WithFS(fs afero.Fs) func(ex *Executor)
- func WithHookAfter(name string, hook func(variables map[string]any) error) func(ex *Executor)
- func WithHooksAfter(hooks HooksAfter) func(ex *Executor)
- func WithLogger(log Logger) func(ex *Executor)
- func WithRegisteredCommandTypes() func(ex *Executor)
- func WithStderr(stderr io.Writer) func(ex *Executor)
- func WithStdout(stdout io.Writer) func(ex *Executor)
- func WithStepNameSuffix(suffix string) func(ex *Executor)
- type BaseCommand
- func (r *BaseCommand) DebugInfo() *CommandDebugInfo
- func (r *BaseCommand) GetDescription(variables map[string]any) string
- func (r *BaseCommand) GetHookAfter() string
- func (r *BaseCommand) GetRequires() string
- func (r *BaseCommand) GetStepName() string
- func (r *BaseCommand) SetDebugInfo(di *CommandDebugInfo)
- type BeforeCommandExecuteCallback
- type Buffer
- type CombinedWriter
- type Command
- func NewExecCommand(ectx *ExecutorContext) Command
- func NewForeachCommand(ectx *ExecutorContext) Command
- func NewMessageCommand(ectx *ExecutorContext) Command
- func NewPassword(ectx *ExecutorContext) Command
- func NewSleepCommand(ectx *ExecutorContext) Command
- func NewSubExecuteCommand(ectx *ExecutorContext) Command
- func NewVariableCommand(ectx *ExecutorContext) Command
- func NewWriterFileCommand(ectx *ExecutorContext) Command
- type CommandAwareError
- type CommandDebugInfo
- type DebugInfoer
- type ExecCommand
- type Executor
- func (ex *Executor) AppendScenario(scenario string) error
- func (ex *Executor) CommandTypeFn(typ string) (func(ectx *ExecutorContext) Command, bool)
- func (ex *Executor) CommandTypes() map[string]func(ectx *ExecutorContext) Command
- func (ex *Executor) Execute(variables map[string]any) (err error)
- func (ex *Executor) RegisterEvaluatorFunction(name string, fn func(args ...any) (any, error)) *Executor
- func (ex *Executor) SetBeforeCommandExecuteCallback(cb BeforeCommandExecuteCallback) *Executor
- func (ex *Executor) WithCommands(cmds []Command, opts ...Option) *Executor
- func (ex *Executor) WithScenario(scenario string, opts ...Option) (*Executor, error)
- type ExecutorContext
- type ForeachCommand
- type HooksAfter
- type IncludeCommand
- type Logger
- type MessageCommand
- type Option
- type PasswordCommand
- type RawScenario
- type SleepCommand
- type SubExecuteCommand
- type VariableCommand
- type WriteFileCommand
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ExecCommandFn = exec.Command
var TimeSleep = time.Sleep
Functions ¶
func GetRegisteredCommands ¶
func GetRegisteredCommands() map[string]func(ectx *ExecutorContext) Command
func NewIncludeCommand ¶
func NewIncludeCommand(storage fs.ReadFileFS) func(ectx *ExecutorContext) Command
func NewIncludeCommandWithBasePath ¶
func NewIncludeCommandWithBasePath(storage fs.ReadFileFS, basepath string) func(ectx *ExecutorContext) Command
func RegisterCommand ¶
func RegisterCommand(name string, cmd func(ectx *ExecutorContext) Command)
func RegisterValueFunc ¶
RegisterValueFunc registers template value functions. Not safe for concurrent usage.
func ShellEscape ¶
func WithCommandTypes ¶
func WithCommandTypes(types map[string]func(ectx *ExecutorContext) Command) func(ex *Executor)
func WithCommands ¶
func WithDefaultEvaluatorFunctions ¶
func WithDefaultEvaluatorFunctions() func(ex *Executor)
WithDefaultEvaluatorFunctions registers value evaluator functions.
There are 3 of them available: - `file_exists(filename string) (bool, error)` - returns true if file exists - `strlen(str string) (float64, error)` - returns the length of the string as a number - `shell_escape(str string) (string, error)` - returns the shell-escaped version of the string
func WithEvaluatorFunction ¶
func WithEvaluatorFunction(name string, fn govaluate.ExpressionFunction) func(ex *Executor)
func WithEvaluatorFunctions ¶
func WithEvaluatorFunctions(funcs map[string]govaluate.ExpressionFunction) func(ex *Executor)
func WithHookAfter ¶
func WithHooksAfter ¶
func WithHooksAfter(hooks HooksAfter) func(ex *Executor)
func WithLogger ¶
func WithRegisteredCommandTypes ¶
func WithRegisteredCommandTypes() func(ex *Executor)
func WithStderr ¶
func WithStdout ¶
func WithStepNameSuffix ¶
Types ¶
type BaseCommand ¶
type BaseCommand struct {
Type string
StepName string
Description string
Requires string
CallsAfter string
Ectx *ExecutorContext
// contains filtered or unexported fields
}
func (*BaseCommand) DebugInfo ¶
func (r *BaseCommand) DebugInfo() *CommandDebugInfo
func (*BaseCommand) GetDescription ¶
func (r *BaseCommand) GetDescription(variables map[string]any) string
func (*BaseCommand) GetHookAfter ¶
func (r *BaseCommand) GetHookAfter() string
func (*BaseCommand) GetRequires ¶
func (r *BaseCommand) GetRequires() string
func (*BaseCommand) GetStepName ¶
func (r *BaseCommand) GetStepName() string
func (*BaseCommand) SetDebugInfo ¶
func (r *BaseCommand) SetDebugInfo(di *CommandDebugInfo)
type CombinedWriter ¶
type CombinedWriter struct {
// contains filtered or unexported fields
}
func NewCombinedWriter ¶
func NewCombinedWriter(writers []io.Writer) *CombinedWriter
type Command ¶
type Command interface {
GetRequires() string
GetStepName() string
GetHookAfter() string
GetDescription(variables map[string]any) string
Execute(variables map[string]any) error
}
func NewExecCommand ¶
func NewExecCommand(ectx *ExecutorContext) Command
func NewForeachCommand ¶
func NewForeachCommand(ectx *ExecutorContext) Command
func NewMessageCommand ¶
func NewMessageCommand(ectx *ExecutorContext) Command
func NewPassword ¶
func NewPassword(ectx *ExecutorContext) Command
func NewSleepCommand ¶
func NewSleepCommand(ectx *ExecutorContext) Command
func NewSubExecuteCommand ¶
func NewSubExecuteCommand(ectx *ExecutorContext) Command
func NewVariableCommand ¶
func NewVariableCommand(ectx *ExecutorContext) Command
func NewWriterFileCommand ¶
func NewWriterFileCommand(ectx *ExecutorContext) Command
type CommandAwareError ¶
type CommandAwareError struct {
// contains filtered or unexported fields
}
func NewCommandAwareError ¶
func NewCommandAwareError(err error, cmd Command, variables map[string]any) *CommandAwareError
func (*CommandAwareError) Cause ¶
func (e *CommandAwareError) Cause() error
func (*CommandAwareError) Command ¶
func (e *CommandAwareError) Command() Command
func (*CommandAwareError) Error ¶
func (e *CommandAwareError) Error() string
func (*CommandAwareError) Unwrap ¶
func (e *CommandAwareError) Unwrap() error
func (*CommandAwareError) Variables ¶
func (e *CommandAwareError) Variables() map[string]any
type CommandDebugInfo ¶
type DebugInfoer ¶
type DebugInfoer interface {
DebugInfo() *CommandDebugInfo
SetDebugInfo(*CommandDebugInfo)
}
type ExecCommand ¶
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
func NewWithScenario ¶
Example ¶
ExampleNewWithScenario demonstrates creating and running a minimal scenario with default evaluator functions, and reading a variable set by the script.
package main
import (
"fmt"
"github.com/go-extras/godexer"
)
func main() {
const script = `
commands:
- type: message
stepName: hello
description: 'Hello, {{ index . "name" }}'
- type: variable
stepName: set
variable: greeting
value: 'Hi, {{ index . "name" }}'
`
ex, err := godexer.NewWithScenario(script, godexer.WithDefaultEvaluatorFunctions())
if err != nil {
fmt.Println("error:", err)
return
}
vars := map[string]any{"name": "John"}
_ = ex.Execute(vars)
fmt.Println("Greeting:", vars["greeting"])
}
Output: Greeting: Hi, John
func (*Executor) AppendScenario ¶
func (*Executor) CommandTypeFn ¶
func (ex *Executor) CommandTypeFn(typ string) (func(ectx *ExecutorContext) Command, bool)
func (*Executor) CommandTypes ¶
func (ex *Executor) CommandTypes() map[string]func(ectx *ExecutorContext) Command
func (*Executor) Execute ¶
Execute runs installation script commands/actions according to the provided params map.
func (*Executor) RegisterEvaluatorFunction ¶
func (ex *Executor) RegisterEvaluatorFunction(name string, fn func(args ...any) (any, error)) *Executor
RegisterEvaluatorFunction registers an evaluator function. The function must follow the signature: `func(args ...any) (any, error)`.
func (*Executor) SetBeforeCommandExecuteCallback ¶
func (ex *Executor) SetBeforeCommandExecuteCallback(cb BeforeCommandExecuteCallback) *Executor
func (*Executor) WithCommands ¶
type ExecutorContext ¶
type ForeachCommand ¶
type ForeachCommand struct {
BaseCommand
RawCommands []json.RawMessage `json:"commands"`
// Value that contains slice or map
Iterable any `json:"iterable"`
// Script variable that contains slice or map (unused if iterable is set)
Variable string `json:"variable"`
// Script variable that will be created for the key at the iteration (default: key)
KeyVar string `json:"keyVar"`
// Script variable that will be created for the value at the iteration (default: value)
ValueVar string `json:"valueVar"`
// Script variable that will be created for the parent vars at the iteration (default: parent)
ParentVar string `json:"parentVar"`
// contains filtered or unexported fields
}
ForeachCommand allows defining a set of commands that will be run for each item in the given map or slice contained in the `variable`.
type IncludeCommand ¶
type IncludeCommand struct {
SubExecuteCommand
// File to include.
File string `json:"file"`
// Variables to be passed to the included script.
Variables map[string]any `json:"variables"`
// If noMergeVars is true, variables that will be set in the included script
// will be stored in a newly created "%step_name%_vars" var.
// Otherwise, they will be placed in the global script namespace.
NoMergeVars bool `json:"noMergeVars"`
// contains filtered or unexported fields
}
type Logger ¶
type Logger interface {
Debugf(format string, args ...any)
Infof(format string, args ...any)
Printf(format string, args ...any)
Warnf(format string, args ...any)
Warningf(format string, args ...any)
Errorf(format string, args ...any)
Fatalf(format string, args ...any)
Panicf(format string, args ...any)
Debug(args ...any)
Info(args ...any)
Print(args ...any)
Warn(args ...any)
Warning(args ...any)
Error(args ...any)
Fatal(args ...any)
Panic(args ...any)
Tracef(format string, args ...any)
Trace(args ...any)
}
Logger is an interface that is compatible with logrus, but doesn't require logrus to be used
type MessageCommand ¶
type MessageCommand struct {
BaseCommand
}
type PasswordCommand ¶
type PasswordCommand struct {
BaseCommand
Variable string
Length int
Charset string
}
type RawScenario ¶
type RawScenario struct {
Commands []json.RawMessage `json:"commands"`
}
type SleepCommand ¶
type SleepCommand struct {
BaseCommand
Seconds int
}
type SubExecuteCommand ¶
type SubExecuteCommand struct {
BaseCommand
RawCommands []json.RawMessage `json:"commands"`
}
SubExecuteCommand is designed to be used by other command types one of the possible usages is an implementation of an include command
type VariableCommand ¶
type VariableCommand struct {
BaseCommand
Variable string
Value any
}
type WriteFileCommand ¶
type WriteFileCommand struct {
BaseCommand
Contents string
File string
Permissions string
}