-
Notifications
You must be signed in to change notification settings - Fork 5
/
sleep-executor.go
66 lines (51 loc) · 1.39 KB
/
sleep-executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package internal
import (
"fmt"
"strings"
"time"
"github.com/bmc-toolbox/actor/internal/actions"
)
type (
SleepExecutorFactory struct{}
SleepExecutor struct{}
)
func NewSleepExecutorFactory() *SleepExecutorFactory {
return &SleepExecutorFactory{}
}
func (f *SleepExecutorFactory) New(_ map[string]interface{}) (actions.Executor, error) {
return &SleepExecutor{}, nil
}
func (e *SleepExecutor) Validate(action string) error {
ok, err := isSleepAction(action)
if !ok {
return fmt.Errorf("%q is not a sleep action", action)
}
return err
}
func (e *SleepExecutor) Run(action string) actions.ActionResult {
duration, err := parserDuration(action)
if err != nil {
return actions.NewActionResult(action, false, "failed", err)
}
time.Sleep(duration)
return actions.NewActionResult(action, true, "ok", nil)
}
func (e *SleepExecutor) Cleanup() {
}
func parserDuration(sleepAction string) (time.Duration, error) {
durationStr := strings.Replace(sleepAction, "sleep ", "", 1)
duration, err := time.ParseDuration(durationStr)
if err != nil {
return 0, fmt.Errorf("failed to parser duration in sleep action: %w", err)
}
return duration, nil
}
func isSleepAction(action string) (bool, error) {
if strings.HasPrefix(action, "sleep") && strings.Contains(action, "sleep ") {
if _, err := parserDuration(action); err != nil {
return true, err
}
return true, nil
}
return false, nil
}