refactor triggger/action registration

This commit is contained in:
2023-09-30 18:49:12 +02:00
parent 64bce5827c
commit 0354d86796
7 changed files with 57 additions and 33 deletions

View File

@@ -10,6 +10,12 @@ import (
type Action interface {
Execute()
DryExecute()
Create(internal.ActionConfig, chan bool) (Action, error)
}
type DocumentedAction interface {
Action
internal.Documenter
}
type Stage struct {
@@ -58,21 +64,15 @@ func (a StagedActions) Execute() {
a.executeInternal(func(a Action) { a.Execute() })
}
func (a StagedActions) Create(config internal.ActionConfig, c chan bool) (Action, error) {
return StagedActions{}, nil
}
func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) {
if config.Type == "Print" {
return NewPrint(config, c)
}
if config.Type == "Timeout" {
return NewTimeOut(config, c)
}
if config.Type == "Command" {
return NewCommand(config, c)
}
if config.Type == "Shutdown" {
return NewShutdown(config, c)
for _, availableAction := range GetAllActions() {
if config.Type == availableAction.GetName() {
return availableAction.Create(config, c)
}
}
return nil, fmt.Errorf("Error parsing config: Action with type %s does not exists", config.Type)
@@ -111,11 +111,21 @@ func NewAction(config []internal.ActionConfig) (Action, error) {
return stagedActions, nil
}
func GetDocumenters() []internal.Documenter {
return []internal.Documenter{
func GetAllActions() []DocumentedAction {
return []DocumentedAction{
Printer{},
TimeOut{},
Command{},
Shutdown{},
}
}
func GetDocumenters() []internal.Documenter {
var result []internal.Documenter
for _, action := range GetAllActions() {
result = append(result, action)
}
return result
}

View File

@@ -22,7 +22,7 @@ func (p Printer) DryExecute() {
p.ActionChan <- true
}
func NewPrint(config internal.ActionConfig, c chan bool) (Action, error) {
func (p Printer) Create(config internal.ActionConfig, c chan bool) (Action, error) {
var result Printer
err := json.Unmarshal(config.Options, &result)

View File

@@ -11,24 +11,24 @@ type Shutdown struct {
ActionChan chan bool
}
func (c Shutdown) DryExecute() {
func (s Shutdown) DryExecute() {
fmt.Println("Test Shutdown executed...")
c.ActionChan <- true
s.ActionChan <- true
}
func (c Shutdown) Execute() {
func (s Shutdown) Execute() {
if err := exec.Command("shutdown", "-h", "now").Run(); err != nil {
fmt.Println("Failed to initiate shutdown:", err)
}
fmt.Println("Shutdown executed...")
c.ActionChan <- true
s.ActionChan <- true
}
func NewShutdown(config internal.ActionConfig, c chan bool) (Action, error) {
func (s Shutdown) Create(config internal.ActionConfig, c chan bool) (Action, error) {
return Shutdown{c}, nil
}

View File

@@ -23,7 +23,7 @@ func (t TimeOut) Execute() {
t.ActionChan <- true
}
func NewTimeOut(config internal.ActionConfig, c chan bool) (Action, error) {
func (t TimeOut) Create(config internal.ActionConfig, c chan bool) (Action, error) {
var result TimeOut
err := json.Unmarshal(config.Options, &result)

View File

@@ -49,7 +49,7 @@ func (t EthernetDisconnect) Listen() {
actions.Fire(t.action)
}
func NewEthernetDisconnect(config internal.KillSwitchConfig) (EthernetDisconnect, error) {
func (e EthernetDisconnect) Create(config internal.KillSwitchConfig) (Trigger, error) {
result := EthernetDisconnect{
WaitTillConnected: true,
}

View File

@@ -22,7 +22,7 @@ func (t TimeOut) Listen() {
actions.Fire(t.action)
}
func NewTimeOut(config internal.KillSwitchConfig) (TimeOut, error) {
func (t TimeOut) Create(config internal.KillSwitchConfig) (Trigger, error) {
var result TimeOut
err := json.Unmarshal(config.Options, &result)

View File

@@ -8,23 +8,37 @@ import (
type Trigger interface {
Listen()
Create(internal.KillSwitchConfig) (Trigger, error)
}
type DocumentedTrigger interface {
internal.Documenter
Trigger
}
func NewTrigger(config internal.KillSwitchConfig) (Trigger, error) {
if config.Type == "Timeout" {
return NewTimeOut(config)
}
if config.Type == "EthernetDisconnect" {
return NewEthernetDisconnect(config)
for _, availableTrigger := range GetAllTriggers() {
if config.Type == availableTrigger.GetName() {
return availableTrigger.Create(config)
}
}
return nil, fmt.Errorf("Error parsing config: Trigger with type %s does not exists", config.Type)
}
func GetDocumenters() []internal.Documenter {
return []internal.Documenter{
func GetAllTriggers() []DocumentedTrigger {
return []DocumentedTrigger{
TimeOut{},
EthernetDisconnect{},
}
}
func GetDocumenters() []internal.Documenter {
var result []internal.Documenter
for _, action := range GetAllTriggers() {
result = append(result, action)
}
return result
}