-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.go
More file actions
81 lines (75 loc) · 1.5 KB
/
utils.go
File metadata and controls
81 lines (75 loc) · 1.5 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package deisctl
import (
"fmt"
"regexp"
"sort"
"strconv"
)
func nextUnitNum(units []string) (num int, err error) {
count, err := countUnits(units)
if err != nil {
return
}
sort.Ints(count)
num = 1
for _, i := range count {
if num < i {
return num, nil
}
num++
}
return num, nil
}
func lastUnitNum(units []string) (num int, err error) {
count, err := countUnits(units)
if err != nil {
return
}
num = 1
sort.Sort(sort.Reverse(sort.IntSlice(count)))
if len(count) == 0 {
return num, fmt.Errorf("Component not found")
}
return count[0], nil
}
func countUnits(units []string) (count []int, err error) {
for _, unit := range units {
_, n, err := splitJobName(unit)
if err != nil {
return count, err
}
count = append(count, n)
}
return
}
func splitJobName(component string) (c string, num int, err error) {
r := regexp.MustCompile(`deis\-([a-z-]+)\.([\d]+)\.service`)
match := r.FindStringSubmatch(component)
if len(match) == 0 {
c, err = "", fmt.Errorf("Could not parse component: %v", component)
return
}
c = match[1]
num, err = strconv.Atoi(match[2])
if err != nil {
return
}
return
}
func splitComponentTarget(target string) (c string, num int, err error) {
r := regexp.MustCompile(`([a-z-]+)\.?([\d]+)?`)
match := r.FindStringSubmatch(target)
if len(match) == 0 {
err = fmt.Errorf("Could not parse: %v", target)
return
}
if match[2] == "" {
return match[1], 0, nil
}
c = match[1]
num, err = strconv.Atoi(match[2])
if err != nil {
return
}
return
}