Skip to content

Commit 25be4a7

Browse files
committed
more comment addressing
1 parent ede9b50 commit 25be4a7

File tree

3 files changed

+56
-82
lines changed

3 files changed

+56
-82
lines changed

pkg/config/draftconfig.go

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,13 @@ func (d *DraftConfig) ApplyDefaultVariables() error {
202202
variable.Value = defaultVal
203203
}
204204

205-
if len(variable.ActiveWhenConstraints) > 0 {
206-
isVarActive := true
207-
for _, activeWhen := range variable.ActiveWhenConstraints {
208-
refVar, err := d.GetVariable(activeWhen.VariableName)
209-
if err != nil {
210-
return fmt.Errorf("unable to get ActiveWhen reference variable: %w", err)
211-
}
212-
213-
isConditionTrue, err := d.CheckActiveWhenConstraint(refVar, activeWhen)
214-
if err != nil {
215-
return fmt.Errorf("unable to check ActiveWhen constraint: %w", err)
216-
}
205+
isVarActive, err := d.CheckActiveWhenConstraint(variable)
206+
if err != nil {
207+
return fmt.Errorf("unable to check ActiveWhen constraint: %w", err)
208+
}
217209

218-
if !isConditionTrue {
219-
isVarActive = false
220-
}
221-
}
222-
if !isVarActive {
223-
continue
224-
}
210+
if !isVarActive {
211+
continue
225212
}
226213

227214
if variable.Value == "" {
@@ -280,26 +267,13 @@ func (d *DraftConfig) ApplyDefaultVariablesForVersion(version string) error {
280267
variable.Value = defaultVal
281268
}
282269

283-
if len(variable.ActiveWhenConstraints) > 0 {
284-
isVarActive := true
285-
for _, activeWhen := range variable.ActiveWhenConstraints {
286-
refVar, err := d.GetVariable(activeWhen.VariableName)
287-
if err != nil {
288-
return fmt.Errorf("unable to get ActiveWhen reference variable: %w", err)
289-
}
290-
291-
isConditionTrue, err := d.CheckActiveWhenConstraint(refVar, activeWhen)
292-
if err != nil {
293-
return fmt.Errorf("unable to check ActiveWhen constraint: %w", err)
294-
}
270+
isVarActive, err := d.CheckActiveWhenConstraint(variable)
271+
if err != nil {
272+
return fmt.Errorf("unable to check ActiveWhen constraint: %w", err)
273+
}
295274

296-
if !isConditionTrue {
297-
isVarActive = false
298-
}
299-
}
300-
if !isVarActive {
301-
continue
302-
}
275+
if !isVarActive {
276+
continue
303277
}
304278

305279
if variable.Value == "" {
@@ -316,34 +290,47 @@ func (d *DraftConfig) ApplyDefaultVariablesForVersion(version string) error {
316290
return nil
317291
}
318292

319-
func (d *DraftConfig) CheckActiveWhenConstraint(refVar *BuilderVar, activeWhen ActiveWhenConstraint) (bool, error) {
320-
checkValue := refVar.Value
321-
if checkValue == "" {
322-
if refVar.Default.Value != "" {
323-
checkValue = refVar.Default.Value
324-
}
325-
326-
if refVar.Default.ReferenceVar != "" {
327-
refValue, err := d.recurseReferenceVars(refVar, refVar, true)
293+
func (d *DraftConfig) CheckActiveWhenConstraint(variable *BuilderVar) (bool, error) {
294+
if len(variable.ActiveWhenConstraints) > 0 {
295+
isVarActive := true
296+
for _, activeWhen := range variable.ActiveWhenConstraints {
297+
refVar, err := d.GetVariable(activeWhen.VariableName)
328298
if err != nil {
329-
return false, err
299+
return false, fmt.Errorf("unable to get ActiveWhen reference variable: %w", err)
330300
}
331-
if refValue == "" {
332-
return false, errors.New("reference variable has no value")
301+
302+
checkValue := refVar.Value
303+
if checkValue == "" {
304+
if refVar.Default.Value != "" {
305+
checkValue = refVar.Default.Value
306+
}
307+
308+
if refVar.Default.ReferenceVar != "" {
309+
refValue, err := d.recurseReferenceVars(refVar, refVar, true)
310+
if err != nil {
311+
return false, err
312+
}
313+
if refValue == "" {
314+
return false, errors.New("reference variable has no value")
315+
}
316+
317+
checkValue = refValue
318+
}
333319
}
334320

335-
checkValue = refValue
321+
switch activeWhen.Condition {
322+
case EqualTo:
323+
isVarActive = checkValue == activeWhen.Value
324+
case NotEqualTo:
325+
isVarActive = checkValue != activeWhen.Value
326+
default:
327+
return false, fmt.Errorf("invalid activeWhen condition: %s", activeWhen.Condition)
328+
}
336329
}
330+
return isVarActive, nil
337331
}
338332

339-
switch activeWhen.Condition {
340-
case EqualTo:
341-
return checkValue == activeWhen.Value, nil
342-
case NotEqualTo:
343-
return checkValue != activeWhen.Value, nil
344-
}
345-
346-
return false, nil
333+
return true, nil
347334
}
348335

349336
// recurseReferenceVars recursively checks each variable's ReferenceVar if it doesn't have a custom input. If there's no more ReferenceVars, it will return the default value of the last ReferenceVar.

pkg/config/validators/validators.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func imagePullPolicyValidator(input string) error {
2525
case "Always", "IfNotPresent", "Never":
2626
return nil
2727
default:
28-
return fmt.Errorf("invalid image pull policy: %s", input)
28+
return fmt.Errorf("invalid image pull policy: %s. valid values: Always, IfNotPresent, Never", input)
2929
}
3030
}
3131

@@ -34,7 +34,7 @@ func scalingResourceTypeValidator(input string) error {
3434
case "cpu", "memory":
3535
return nil
3636
default:
37-
return fmt.Errorf("invalid scaling resource type: %s", input)
37+
return fmt.Errorf("invalid scaling resource type: %s. valid values: cpu, memory", input)
3838
}
3939
}
4040

@@ -43,7 +43,7 @@ func kubernetesProbeTypeValidator(input string) error {
4343
case "httpGet", "tcpSocket":
4444
return nil
4545
default:
46-
return fmt.Errorf("invalid probe type: %s", input)
46+
return fmt.Errorf("invalid probe type: %s. valid values: httpGet, tcpSocket", input)
4747
}
4848
}
4949

pkg/prompts/prompts.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,13 @@ func RunPromptsFromConfigWithSkipsIO(draftConfig *config.DraftConfig, Stdin io.R
5353
continue
5454
}
5555

56-
if len(variable.ActiveWhenConstraints) > 0 {
57-
isVarActive := true
58-
for _, activeWhen := range variable.ActiveWhenConstraints {
59-
refVar, err := draftConfig.GetVariable(activeWhen.VariableName)
60-
if err != nil {
61-
return fmt.Errorf("unable to get ActiveWhen reference variable: %w", err)
62-
}
63-
64-
isConditionTrue, err := draftConfig.CheckActiveWhenConstraint(refVar, activeWhen)
65-
if err != nil {
66-
return fmt.Errorf("unable to check ActiveWhen constraint: %w", err)
67-
}
68-
69-
if !isConditionTrue {
70-
isVarActive = false
71-
}
72-
}
73-
if !isVarActive {
74-
continue
75-
}
56+
isVarActive, err := draftConfig.CheckActiveWhenConstraint(variable)
57+
if err != nil {
58+
return fmt.Errorf("unable to check ActiveWhen constraint: %w", err)
59+
}
60+
61+
if !isVarActive {
62+
continue
7663
}
7764

7865
log.Debugf("constructing prompt for: %s", variable.Name)

0 commit comments

Comments
 (0)