[actions] handle errors via channel
This commit is contained in:
@@ -7,10 +7,12 @@ import (
|
|||||||
"unknown.com/gokill/internal"
|
"unknown.com/gokill/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ActionResultChan chan error
|
||||||
|
|
||||||
type Action interface {
|
type Action interface {
|
||||||
Execute()
|
Execute()
|
||||||
DryExecute()
|
DryExecute()
|
||||||
Create(internal.ActionConfig, chan bool) (Action, error)
|
Create(internal.ActionConfig, ActionResultChan) (Action, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type DocumentedAction interface {
|
type DocumentedAction interface {
|
||||||
@@ -23,7 +25,7 @@ type Stage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type StagedActions struct {
|
type StagedActions struct {
|
||||||
ActionChan chan bool
|
ActionChan ActionResultChan
|
||||||
StageCount int
|
StageCount int
|
||||||
Stages []Stage
|
Stages []Stage
|
||||||
}
|
}
|
||||||
@@ -40,7 +42,11 @@ func (a StagedActions) executeInternal(f func(Action)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for range stage.Actions {
|
for range stage.Actions {
|
||||||
<-a.ActionChan
|
err := <-a.ActionChan
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error occured on Stage %d: %s", idx+1, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,11 +70,11 @@ 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) {
|
func (a StagedActions) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||||
return StagedActions{}, nil
|
return StagedActions{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) {
|
func NewSingleAction(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||||
for _, availableAction := range GetAllActions() {
|
for _, availableAction := range GetAllActions() {
|
||||||
if config.Type == availableAction.GetName() {
|
if config.Type == availableAction.GetName() {
|
||||||
return availableAction.Create(config, c)
|
return availableAction.Create(config, c)
|
||||||
@@ -83,7 +89,7 @@ func NewAction(config []internal.ActionConfig) (Action, error) {
|
|||||||
return config[i].Stage < config[j].Stage
|
return config[i].Stage < config[j].Stage
|
||||||
})
|
})
|
||||||
|
|
||||||
stagedActions := StagedActions{make(chan bool), 0, []Stage{}}
|
stagedActions := StagedActions{make(ActionResultChan), 0, []Stage{}}
|
||||||
|
|
||||||
stageMap := make(map[int][]Action)
|
stageMap := make(map[int][]Action)
|
||||||
|
|
||||||
|
|||||||
@@ -9,20 +9,20 @@ import (
|
|||||||
|
|
||||||
type Printer struct {
|
type Printer struct {
|
||||||
Message string
|
Message string
|
||||||
ActionChan chan bool
|
ActionChan ActionResultChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Printer) Execute() {
|
func (p Printer) Execute() {
|
||||||
fmt.Printf("Print action fires. Message: %s\n", p.Message)
|
fmt.Printf("Print action fires. Message: %s\n", p.Message)
|
||||||
p.ActionChan <- true
|
p.ActionChan <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Printer) DryExecute() {
|
func (p Printer) DryExecute() {
|
||||||
fmt.Printf("Print action fire test. Message: %s\n", p.Message)
|
fmt.Printf("Print action fire test. Message: %s\n", p.Message)
|
||||||
p.ActionChan <- true
|
p.ActionChan <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Printer) Create(config internal.ActionConfig, c chan bool) (Action, error) {
|
func (p Printer) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||||
var result Printer
|
var result Printer
|
||||||
err := json.Unmarshal(config.Options, &result)
|
err := json.Unmarshal(config.Options, &result)
|
||||||
|
|
||||||
@@ -44,6 +44,11 @@ func (p Printer) GetDescription() string {
|
|||||||
|
|
||||||
func (p Printer) GetOptions() []internal.ConfigOption {
|
func (p Printer) GetOptions() []internal.ConfigOption {
|
||||||
return []internal.ConfigOption{
|
return []internal.ConfigOption{
|
||||||
{"message", "string", "Message that should be printed", "\"\""},
|
{
|
||||||
|
Name: "message",
|
||||||
|
Type: "string",
|
||||||
|
Description: "Message that should be printed",
|
||||||
|
Default: "\"\"",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Shutdown struct {
|
type Shutdown struct {
|
||||||
ActionChan chan bool
|
ActionChan ActionResultChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Shutdown) DryExecute() {
|
func (s Shutdown) DryExecute() {
|
||||||
fmt.Println("Test Shutdown executed...")
|
fmt.Println("Test Shutdown executed...")
|
||||||
|
|
||||||
s.ActionChan <- true
|
s.ActionChan <- nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,10 +25,10 @@ func (s Shutdown) Execute() {
|
|||||||
|
|
||||||
fmt.Println("Shutdown executed...")
|
fmt.Println("Shutdown executed...")
|
||||||
|
|
||||||
s.ActionChan <- true
|
s.ActionChan <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Shutdown) Create(config internal.ActionConfig, c chan bool) (Action, error) {
|
func (s Shutdown) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||||
return Shutdown{c}, nil
|
return Shutdown{c}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
type TimeOut struct {
|
type TimeOut struct {
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
ActionChan chan bool
|
ActionChan ActionResultChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TimeOut) DryExecute() {
|
func (t TimeOut) DryExecute() {
|
||||||
@@ -20,10 +20,10 @@ func (t TimeOut) DryExecute() {
|
|||||||
func (t TimeOut) Execute() {
|
func (t TimeOut) Execute() {
|
||||||
fmt.Printf("Waiting %d seconds\n", t.Duration)
|
fmt.Printf("Waiting %d seconds\n", t.Duration)
|
||||||
time.Sleep(time.Duration(t.Duration) * time.Second)
|
time.Sleep(time.Duration(t.Duration) * time.Second)
|
||||||
t.ActionChan <- true
|
t.ActionChan <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TimeOut) Create(config internal.ActionConfig, c chan bool) (Action, error) {
|
func (t TimeOut) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||||
var result TimeOut
|
var result TimeOut
|
||||||
err := json.Unmarshal(config.Options, &result)
|
err := json.Unmarshal(config.Options, &result)
|
||||||
|
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ import (
|
|||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
ActionChan chan bool
|
ActionChan ActionResultChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Command) DryExecute() {
|
func (c Command) DryExecute() {
|
||||||
fmt.Printf("Test Executing Command:\n%s ", c.Command)
|
fmt.Printf("Test Executing Command:\n%s ", c.Command)
|
||||||
c.ActionChan <- true
|
c.ActionChan <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Command) splitCommandString() (string, []string, error) {
|
func (c Command) splitCommandString() (string, []string, error) {
|
||||||
@@ -38,8 +38,7 @@ func (c Command) Execute() {
|
|||||||
fmt.Println("Executing command: ", c.Command)
|
fmt.Println("Executing command: ", c.Command)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
c.ActionChan <- err
|
||||||
c.ActionChan <- false
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,10 +52,10 @@ func (c Command) Execute() {
|
|||||||
|
|
||||||
fmt.Println(string(stdout[:]))
|
fmt.Println(string(stdout[:]))
|
||||||
|
|
||||||
c.ActionChan <- true
|
c.ActionChan <- nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateCommand(config internal.ActionConfig, c chan bool) (Command, error) {
|
func CreateCommand(config internal.ActionConfig, c ActionResultChan) (Command, error) {
|
||||||
result := Command{}
|
result := Command{}
|
||||||
|
|
||||||
err := json.Unmarshal(config.Options, &result)
|
err := json.Unmarshal(config.Options, &result)
|
||||||
@@ -74,7 +73,7 @@ func CreateCommand(config internal.ActionConfig, c chan bool) (Command, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc Command) Create(config internal.ActionConfig, c chan bool) (Action, error) {
|
func (cc Command) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||||
return CreateCommand(config, c)
|
return CreateCommand(config, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user