@@ -41,8 +41,6 @@ type ExpectedNginxField struct {
41
41
// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
42
42
// and returns whether or not that field exists where it should.
43
43
func ValidateNginxFieldExists (conf * Payload , expFieldCfg ExpectedNginxField ) error {
44
- var directiveFoundInServer , directiveFoundInUpstream bool
45
-
46
44
b , err := json .Marshal (conf )
47
45
if err != nil {
48
46
return fmt .Errorf ("error marshaling nginx config: %w" , err )
@@ -61,15 +59,11 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
61
59
continue
62
60
}
63
61
64
- directiveFoundInServer = validateServerBlockDirectives (expFieldCfg , * directive )
65
-
66
- directiveFoundInUpstream = validateUpstreamDirectives (expFieldCfg , * directive )
67
-
68
- if len (expFieldCfg .Server ) > 0 && directiveFoundInServer {
62
+ if len (expFieldCfg .Server ) > 0 && fieldExistsInServer (expFieldCfg , * directive ) {
69
63
return nil
70
64
}
71
65
72
- if len (expFieldCfg .Upstream ) > 0 && directiveFoundInUpstream {
66
+ if len (expFieldCfg .Upstream ) > 0 && fieldExistsInUpstream ( expFieldCfg , * directive ) {
73
67
return nil
74
68
}
75
69
}
@@ -78,37 +72,35 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
78
72
return fmt .Errorf ("directive %s not found in: nginx config %s" , expFieldCfg .Directive , string (b ))
79
73
}
80
74
81
- func validateServerBlockDirectives (
75
+ func fieldExistsInServer (
82
76
expFieldCfg ExpectedNginxField ,
83
77
directive Directive ,
84
78
) bool {
85
- var fieldFound bool
86
79
if directive .Directive == "server" && getServerName (directive .Block ) == expFieldCfg .Server {
87
80
for _ , serverDirective := range directive .Block {
88
81
if expFieldCfg .Location == "" && expFieldCfg .fieldFound (serverDirective ) {
89
- fieldFound = true
82
+ return true
90
83
} else if serverDirective .Directive == "location" &&
91
84
fieldExistsInLocation (serverDirective , expFieldCfg ) {
92
- fieldFound = true
85
+ return true
93
86
}
94
87
}
95
88
}
96
- return fieldFound
89
+ return false
97
90
}
98
91
99
- func validateUpstreamDirectives (
92
+ func fieldExistsInUpstream (
100
93
expFieldCfg ExpectedNginxField ,
101
94
directive Directive ,
102
95
) bool {
103
- var fieldFound bool
104
96
if directive .Directive == "upstream" && directive .Args [0 ] == expFieldCfg .Upstream {
105
97
for _ , directive := range directive .Block {
106
98
if expFieldCfg .fieldFound (directive ) {
107
- fieldFound = true
99
+ return true
108
100
}
109
101
}
110
102
}
111
- return fieldFound
103
+ return false
112
104
}
113
105
114
106
func getServerName (serverBlock Directives ) string {
0 commit comments