parse trigger json into structs
This commit is contained in:
@@ -19,10 +19,10 @@ type ActionConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type KillSwitchConfig struct {
|
type KillSwitchConfig struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Options Options `json:"options"`
|
Options json.RawMessage `json:"options"`
|
||||||
Actions []ActionConfig `json:"actions"`
|
Actions []ActionConfig `json:"actions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigOption struct {
|
type ConfigOption struct {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package triggers
|
package triggers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -20,14 +21,12 @@ func (t TimeOut) Listen() {
|
|||||||
t.action.Execute()
|
t.action.Execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
// func NewTimeOut(d time.Duration, action actions.Action) TimeOut {
|
|
||||||
func NewTimeOut(config internal.KillSwitchConfig) (TimeOut, error) {
|
func NewTimeOut(config internal.KillSwitchConfig) (TimeOut, error) {
|
||||||
opts := config.Options
|
result := TimeOut{d: 0 * time.Second}
|
||||||
|
err := json.Unmarshal(config.Options, &result)
|
||||||
|
|
||||||
duration, ok := opts["duration"]
|
if err != nil {
|
||||||
|
return TimeOut{}, err
|
||||||
if !ok {
|
|
||||||
return TimeOut{}, internal.OptionMissingError{"duration"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
action, err := actions.NewAction(config.Actions)
|
action, err := actions.NewAction(config.Actions)
|
||||||
@@ -36,7 +35,8 @@ func NewTimeOut(config internal.KillSwitchConfig) (TimeOut, error) {
|
|||||||
return TimeOut{}, err
|
return TimeOut{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return TimeOut{time.Duration(duration.(float64)) * time.Second, action}, nil
|
result.action = action
|
||||||
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p TimeOut) GetName() string {
|
func (p TimeOut) GetName() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user