From 0354d8679695c4e0127e22be386c4af43b4ad8ff Mon Sep 17 00:00:00 2001 From: kalipso Date: Sat, 30 Sep 2023 18:49:12 +0200 Subject: [PATCH] refactor triggger/action registration --- actions/actions.go | 42 ++++++++++++++++++++++++++---------------- actions/printer.go | 2 +- actions/shutdown.go | 10 +++++----- actions/timeout.go | 2 +- triggers/ethernet.go | 2 +- triggers/timeout.go | 2 +- triggers/triggers.go | 30 ++++++++++++++++++++++-------- 7 files changed, 57 insertions(+), 33 deletions(-) diff --git a/actions/actions.go b/actions/actions.go index 248d3fb..e13180b 100644 --- a/actions/actions.go +++ b/actions/actions.go @@ -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 +} diff --git a/actions/printer.go b/actions/printer.go index ee456ab..1c8904b 100644 --- a/actions/printer.go +++ b/actions/printer.go @@ -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) diff --git a/actions/shutdown.go b/actions/shutdown.go index 262f418..b586051 100644 --- a/actions/shutdown.go +++ b/actions/shutdown.go @@ -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 } diff --git a/actions/timeout.go b/actions/timeout.go index fb82f27..ce556dc 100644 --- a/actions/timeout.go +++ b/actions/timeout.go @@ -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) diff --git a/triggers/ethernet.go b/triggers/ethernet.go index b72215e..a002a64 100644 --- a/triggers/ethernet.go +++ b/triggers/ethernet.go @@ -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, } diff --git a/triggers/timeout.go b/triggers/timeout.go index 0f03b0c..4657d71 100644 --- a/triggers/timeout.go +++ b/triggers/timeout.go @@ -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) diff --git a/triggers/triggers.go b/triggers/triggers.go index f5f1705..d9794df 100644 --- a/triggers/triggers.go +++ b/triggers/triggers.go @@ -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 +}