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

40
triggers/timeout.go Normal file
View File

@@ -0,0 +1,40 @@
package triggers
import (
"fmt"
"time"
"unknown.com/gokill/actions"
"unknown.com/gokill/internal"
)
type TimeOut struct {
d time.Duration
action actions.Action
}
func (t TimeOut) Listen() {
fmt.Println("TimeOut listens")
time.Sleep(t.d)
fmt.Println("TimeOut fires")
t.action.Execute()
}
// func NewTimeOut(d time.Duration, action actions.Action) TimeOut {
func NewTimeOut(config internal.KillSwitchConfig) (TimeOut, error) {
opts := config.Options
duration, ok := opts["duration"]
if !ok {
return TimeOut{}, internal.OptionMissingError{"duration"}
}
action, err := actions.NewAction(config.Actions)
if err != nil {
return TimeOut{}, err
}
return TimeOut{time.Duration(duration.(float64)) * time.Second, action}, nil
}

View File

@@ -2,64 +2,15 @@ package triggers
import (
"fmt"
"time"
"unknown.com/gokill/actions"
"unknown.com/gokill/internal"
)
type Options map[string]interface{}
type KillSwitchConfig struct {
Name string `json:"name"`
Type string `json:"type"`
Options Options `json:"options"`
Actions []actions.ActionConfig `json:"actions"`
}
type Trigger interface {
Listen()
}
type TimeOut struct {
d time.Duration
action actions.Action
}
func (t TimeOut) Listen() {
fmt.Println("TimeOut listens")
time.Sleep(t.d)
fmt.Println("TimeOut fires")
t.action.Execute()
}
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)
}
// func NewTimeOut(d time.Duration, action actions.Action) TimeOut {
func NewTimeOut(config KillSwitchConfig) (TimeOut, error) {
opts := config.Options
duration, ok := opts["duration"]
if !ok {
return TimeOut{}, OptionMissingError{"duration"}
}
action, err := actions.NewAction(config.Actions)
if err != nil {
return TimeOut{}, err
}
return TimeOut{time.Duration(duration.(float64)) * time.Second, action}, nil
}
func NewTrigger(config KillSwitchConfig) (Trigger, error) {
func NewTrigger(config internal.KillSwitchConfig) (Trigger, error) {
if config.Type == "TimeOut" {
return NewTimeOut(config)
}