...
1 package conditions
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8
9
10 type mergeOptions struct {
11 conditionTypes []string
12 negativePolarityConditionTypes []string
13
14 addSourceRef bool
15 addSourceRefIfConditionTypes []string
16 addCounter bool
17 addCounterOnlyIfConditionTypes []string
18 addStepCounter bool
19 addStepCounterIfOnlyConditionTypes []string
20
21 stepCounter int
22
23 withLatestGeneration bool
24 }
25
26
27 type MergeOption func(*mergeOptions)
28
29
30
31
32
33
34
35
36
37
38 func WithConditions(t ...string) MergeOption {
39 return func(c *mergeOptions) {
40 c.conditionTypes = t
41 }
42 }
43
44
45
46
47
48
49 func WithNegativePolarityConditions(t ...string) MergeOption {
50 return func(c *mergeOptions) {
51 c.negativePolarityConditionTypes = t
52 }
53 }
54
55
56
57 func WithCounter() MergeOption {
58 return func(c *mergeOptions) {
59 c.addCounter = true
60 }
61 }
62
63
64
65
66
67
68
69 func WithCounterIfOnly(t ...string) MergeOption {
70 return func(c *mergeOptions) {
71 c.addCounterOnlyIfConditionTypes = t
72 }
73 }
74
75
76
77 func WithStepCounter() MergeOption {
78 return func(c *mergeOptions) {
79 c.addStepCounter = true
80 }
81 }
82
83
84
85
86 func WithStepCounterIf(value bool) MergeOption {
87 return func(c *mergeOptions) {
88 c.addStepCounter = value
89 }
90 }
91
92
93
94
95
96
97
98 func WithStepCounterIfOnly(t ...string) MergeOption {
99 return func(c *mergeOptions) {
100 c.addStepCounterIfOnlyConditionTypes = t
101 }
102 }
103
104
105
106 func WithSourceRef() MergeOption {
107 return func(c *mergeOptions) {
108 c.addSourceRef = true
109 }
110 }
111
112
113 func WithSourceRefIf(t ...string) MergeOption {
114 return func(c *mergeOptions) {
115 c.addSourceRefIfConditionTypes = t
116 }
117 }
118
119
120
121 func WithLatestGeneration() MergeOption {
122 return func(c *mergeOptions) {
123 c.withLatestGeneration = true
124 }
125 }
126
127
128
129 func getReason(groups conditionGroups, options *mergeOptions) string {
130 return getFirstReason(groups, options.conditionTypes, options.addSourceRef)
131 }
132
133
134
135 func getFirstReason(g conditionGroups, order []string, addSourceRef bool) string {
136 if condition := getFirstCondition(g, order); condition != nil {
137 reason := condition.Reason
138 if addSourceRef {
139 return localizeReason(reason, condition.Getter)
140 }
141 return reason
142 }
143 return ""
144 }
145
146
147 func localizeReason(reason string, from Getter) string {
148 if strings.Contains(reason, "@") {
149 return reason
150 }
151 return fmt.Sprintf("%s @ %s/%s", reason, from.GetObjectKind().GroupVersionKind().Kind, from.GetName())
152 }
153
154
155
156
157 func getMessage(groups conditionGroups, options *mergeOptions) string {
158 if options.addStepCounter {
159 return getStepCounterMessage(groups, options.stepCounter)
160 }
161 if options.addCounter {
162 return getCounterMessage(groups, options.stepCounter)
163 }
164 return getFirstMessage(groups, options.conditionTypes)
165 }
166
167
168
169 func getCounterMessage(groups conditionGroups, to int) string {
170 topGroup := groups.TopGroup()
171 if topGroup == nil {
172 return fmt.Sprintf("%d of %d", 0, to)
173 }
174 ct := len(topGroup.conditions)
175 return fmt.Sprintf("%d of %d %s", ct, to, topGroup.conditions[0].Type)
176 }
177
178
179
180 func getStepCounterMessage(groups conditionGroups, to int) string {
181 ct := 0
182 if trueGroup := groups.TruePositivePolarityGroup(); trueGroup != nil {
183 ct = len(trueGroup.conditions)
184 }
185 return fmt.Sprintf("%d of %d completed", ct, to)
186 }
187
188
189 func getFirstMessage(groups conditionGroups, order []string) string {
190 if condition := getFirstCondition(groups, order); condition != nil {
191 return condition.Message
192 }
193 return ""
194 }
195
196
197 func getFirstCondition(g conditionGroups, priority []string) *localizedCondition {
198 topGroup := g.TopGroup()
199 if topGroup == nil {
200 return nil
201 }
202
203 switch len(topGroup.conditions) {
204 case 0:
205 return nil
206 case 1:
207 return &topGroup.conditions[0]
208 default:
209 for _, p := range priority {
210 for _, c := range topGroup.conditions {
211 if c.Type == p {
212 return &c
213 }
214 }
215 }
216 return &topGroup.conditions[0]
217 }
218 }
219
View as plain text