split actions/trigggers into seperate files

This commit is contained in:
2023-07-18 09:34:47 +02:00
parent a2ea3209f1
commit a457a313b7
7 changed files with 132 additions and 121 deletions

View File

@@ -3,57 +3,14 @@ package actions
import (
"fmt"
"sort"
"time"
"unknown.com/gokill/internal"
)
type OptionMissingError struct {
optionName string
}
func (o OptionMissingError) Error() string {
return fmt.Sprintf("Error during config parsing: option %s could not be parsed.", o.optionName)
}
type Options map[string]interface{}
type ActionConfig struct {
Type string `json:"type"`
Options Options `json:"options"`
Stage int `json:"stage"`
}
type KillSwitchConfig struct {
Name string `json:"name"`
Type string `json:"type"`
Options Options `json:"options"`
Actions []ActionConfig `json:"actions"`
}
type Action interface {
Execute()
}
type Printer struct {
Message string
ActionChan chan bool
}
func (p Printer) Execute() {
fmt.Printf("Print action fires. Message: %s", p.Message)
p.ActionChan <- true
}
type TimeOut struct {
Duration time.Duration
ActionChan chan bool
}
func (t TimeOut) Execute() {
fmt.Printf("Waiting %d seconds\n", t.Duration/time.Second)
time.Sleep(t.Duration)
t.ActionChan <- true
}
type Stage struct {
Actions []Action
}
@@ -81,29 +38,7 @@ func (a StagedActions) Execute() {
}
}
func NewPrint(config ActionConfig, c chan bool) (Action, error) {
opts := config.Options
message, ok := opts["message"]
if !ok {
return nil, OptionMissingError{"message"}
}
return Printer{fmt.Sprintf("%v", message), c}, nil
}
func NewTimeOut(config ActionConfig, c chan bool) (Action, error) {
opts := config.Options
duration, ok := opts["duration"]
if !ok {
return nil, OptionMissingError{"message"}
}
return TimeOut{time.Duration(duration.(float64)) * time.Second, c}, nil
}
func NewSingleAction(config ActionConfig, c chan bool) (Action, error) {
func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) {
if config.Type == "Print" {
return NewPrint(config, c)
}
@@ -115,7 +50,7 @@ func NewSingleAction(config ActionConfig, c chan bool) (Action, error) {
return nil, fmt.Errorf("Error parsing config: Action with type %s does not exists", config.Type)
}
func NewAction(config []ActionConfig) (Action, error) {
func NewAction(config []internal.ActionConfig) (Action, error) {
if len(config) == 1 {
return NewSingleAction(config[0], make(chan bool))
}