-
Notifications
You must be signed in to change notification settings - Fork 27
/
stage.go
46 lines (35 loc) · 1 KB
/
stage.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
package staged
import (
"fmt"
"strconv"
"strings"
"time"
)
type stage struct {
startTarget int
endTarget int
duration time.Duration
}
func parseStages(value string) ([]stage, error) {
var stages []stage
stageElements := strings.Split(value, ",")
for i, stageElements := range stageElements {
stageElement := strings.Split(strings.TrimSpace(stageElements), ":")
if len(stageElement) != 2 {
return nil, fmt.Errorf("unable to parse stage %d: `%s` from `%s`", i, stageElements, value)
}
duration, err := time.ParseDuration(strings.TrimSpace(stageElement[0]))
if err != nil {
return nil, fmt.Errorf("unable to parse duration %s in stage %d: %s", stageElement[0], i, stageElements)
}
target, err := strconv.Atoi(strings.TrimSpace(stageElement[1]))
if err != nil {
return nil, fmt.Errorf("unable to parse target %s in stage %d: %s", stageElement[1], i, stageElements)
}
stages = append(stages, stage{
endTarget: target,
duration: duration,
})
}
return stages, nil
}