parse actions json into structs

This commit is contained in:
2023-08-16 00:20:22 +02:00
parent 5d6d9cc9ba
commit 381cc31d4b
3 changed files with 19 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
package actions package actions
import ( import (
"encoding/json"
"fmt" "fmt"
"unknown.com/gokill/internal" "unknown.com/gokill/internal"
@@ -17,14 +18,15 @@ func (p Printer) Execute() {
} }
func NewPrint(config internal.ActionConfig, c chan bool) (Action, error) { func NewPrint(config internal.ActionConfig, c chan bool) (Action, error) {
opts := config.Options var result Printer
message, ok := opts["message"] err := json.Unmarshal(config.Options, &result)
if !ok { if err != nil {
return nil, internal.OptionMissingError{"message"} return nil, internal.OptionMissingError{"message"}
} }
return Printer{fmt.Sprintf("%v", message), c}, nil result.ActionChan = c
return result, nil
} }
func (p Printer) GetName() string { func (p Printer) GetName() string {

View File

@@ -1,6 +1,7 @@
package actions package actions
import ( import (
"encoding/json"
"fmt" "fmt"
"time" "time"
@@ -19,14 +20,15 @@ func (t TimeOut) Execute() {
} }
func NewTimeOut(config internal.ActionConfig, c chan bool) (Action, error) { func NewTimeOut(config internal.ActionConfig, c chan bool) (Action, error) {
opts := config.Options var result TimeOut
duration, ok := opts["duration"] err := json.Unmarshal(config.Options, &result)
if !ok { if err != nil {
return nil, internal.OptionMissingError{"duration"} return nil, internal.OptionMissingError{"duration"}
} }
return TimeOut{time.Duration(duration.(float64)) * time.Second, c}, nil result.ActionChan = c
return result, nil
} }
func (p TimeOut) GetName() string { func (p TimeOut) GetName() string {

View File

@@ -1,6 +1,9 @@
package internal package internal
import "fmt" import (
"encoding/json"
"fmt"
)
type OptionMissingError struct { type OptionMissingError struct {
OptionName string OptionName string
@@ -10,12 +13,10 @@ func (o OptionMissingError) Error() string {
return fmt.Sprintf("Error during config parsing: option %s could not be parsed.", o.OptionName) return fmt.Sprintf("Error during config parsing: option %s could not be parsed.", o.OptionName)
} }
type Options map[string]interface{}
type ActionConfig struct { type ActionConfig struct {
Type string `json:"type"` Type string `json:"type"`
Options Options `json:"options"` Options json.RawMessage `json:"options"`
Stage int `json:"stage"` Stage int `json:"stage"`
} }
type KillSwitchConfig struct { type KillSwitchConfig struct {