split actions/trigggers into seperate files
This commit is contained in:
@@ -3,57 +3,14 @@ package actions
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"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 {
|
type Action interface {
|
||||||
Execute()
|
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 {
|
type Stage struct {
|
||||||
Actions []Action
|
Actions []Action
|
||||||
}
|
}
|
||||||
@@ -81,29 +38,7 @@ func (a StagedActions) Execute() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPrint(config ActionConfig, c chan bool) (Action, error) {
|
func NewSingleAction(config internal.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) {
|
|
||||||
if config.Type == "Print" {
|
if config.Type == "Print" {
|
||||||
return NewPrint(config, c)
|
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)
|
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 {
|
if len(config) == 1 {
|
||||||
return NewSingleAction(config[0], make(chan bool))
|
return NewSingleAction(config[0], make(chan bool))
|
||||||
}
|
}
|
||||||
|
|||||||
28
actions/printer.go
Normal file
28
actions/printer.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"unknown.com/gokill/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Printer struct {
|
||||||
|
Message string
|
||||||
|
ActionChan chan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Printer) Execute() {
|
||||||
|
fmt.Printf("Print action fires. Message: %s", p.Message)
|
||||||
|
p.ActionChan <- true
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPrint(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||||
|
opts := config.Options
|
||||||
|
message, ok := opts["message"]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, internal.OptionMissingError{"message"}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Printer{fmt.Sprintf("%v", message), c}, nil
|
||||||
|
}
|
||||||
30
actions/timeout.go
Normal file
30
actions/timeout.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"unknown.com/gokill/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTimeOut(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||||
|
opts := config.Options
|
||||||
|
duration, ok := opts["duration"]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, internal.OptionMissingError{"duration"}
|
||||||
|
}
|
||||||
|
|
||||||
|
return TimeOut{time.Duration(duration.(float64)) * time.Second, c}, nil
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"unknown.com/gokill/internal"
|
||||||
"unknown.com/gokill/triggers"
|
"unknown.com/gokill/triggers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -74,7 +75,7 @@ func main() {
|
|||||||
]
|
]
|
||||||
`)
|
`)
|
||||||
|
|
||||||
var f []triggers.KillSwitchConfig
|
var f []internal.KillSwitchConfig
|
||||||
err := json.Unmarshal(b, &f)
|
err := json.Unmarshal(b, &f)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
26
internal/config.go
Normal file
26
internal/config.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
40
triggers/timeout.go
Normal file
40
triggers/timeout.go
Normal 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
|
||||||
|
}
|
||||||
@@ -2,64 +2,15 @@ package triggers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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 {
|
type Trigger interface {
|
||||||
Listen()
|
Listen()
|
||||||
}
|
}
|
||||||
|
|
||||||
type TimeOut struct {
|
func NewTrigger(config internal.KillSwitchConfig) (Trigger, error) {
|
||||||
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) {
|
|
||||||
if config.Type == "TimeOut" {
|
if config.Type == "TimeOut" {
|
||||||
return NewTimeOut(config)
|
return NewTimeOut(config)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user