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 { type Action interface {
Execute() Execute()
DryExecute() DryExecute()
Create(internal.ActionConfig, chan bool) (Action, error)
}
type DocumentedAction interface {
Action
internal.Documenter
} }
type Stage struct { type Stage struct {
@@ -58,21 +64,15 @@ func (a StagedActions) Execute() {
a.executeInternal(func(a Action) { a.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) { func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) {
if config.Type == "Print" { for _, availableAction := range GetAllActions() {
return NewPrint(config, c) if config.Type == availableAction.GetName() {
} return availableAction.Create(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)
} }
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)
@@ -111,11 +111,21 @@ func NewAction(config []internal.ActionConfig) (Action, error) {
return stagedActions, nil return stagedActions, nil
} }
func GetDocumenters() []internal.Documenter { func GetAllActions() []DocumentedAction {
return []internal.Documenter{ return []DocumentedAction{
Printer{}, Printer{},
TimeOut{}, TimeOut{},
Command{}, Command{},
Shutdown{}, 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 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 var result Printer
err := json.Unmarshal(config.Options, &result) err := json.Unmarshal(config.Options, &result)

View File

@@ -11,24 +11,24 @@ type Shutdown struct {
ActionChan chan bool ActionChan chan bool
} }
func (c Shutdown) DryExecute() { func (s Shutdown) DryExecute() {
fmt.Println("Test Shutdown executed...") 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 { if err := exec.Command("shutdown", "-h", "now").Run(); err != nil {
fmt.Println("Failed to initiate shutdown:", err) fmt.Println("Failed to initiate shutdown:", err)
} }
fmt.Println("Shutdown executed...") 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 return Shutdown{c}, nil
} }

View File

@@ -23,7 +23,7 @@ func (t TimeOut) Execute() {
t.ActionChan <- true 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 var result TimeOut
err := json.Unmarshal(config.Options, &result) err := json.Unmarshal(config.Options, &result)

View File

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

View File

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

View File

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