(lines []string, fieldValue any)
| 153 | } |
| 154 | |
| 155 | func mergeFirewallIntoExistingSandbox(lines []string, fieldValue any) ([]string, bool) { |
| 156 | agentLines := sandboxAgentLinesForExistingSandbox(fieldValue) |
| 157 | if len(agentLines) == 0 { |
| 158 | return lines, false |
| 159 | } |
| 160 | |
| 161 | sandboxIdx := -1 |
| 162 | for i, line := range lines { |
| 163 | trimmed := strings.TrimSpace(line) |
| 164 | if isTopLevelKey(line) && strings.HasPrefix(trimmed, "sandbox:") { |
| 165 | sandboxIdx = i |
| 166 | break |
| 167 | } |
| 168 | } |
| 169 | if sandboxIdx == -1 { |
| 170 | return lines, false |
| 171 | } |
| 172 | |
| 173 | sandboxIndent := getIndentation(lines[sandboxIdx]) |
| 174 | agentIndent := sandboxIndent + " " |
| 175 | sandboxEnd := len(lines) |
| 176 | for i := sandboxIdx + 1; i < len(lines); i++ { |
| 177 | if isTopLevelKey(lines[i]) { |
| 178 | sandboxEnd = i |
| 179 | break |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | agentStart := -1 |
| 184 | for i := sandboxIdx + 1; i < sandboxEnd; i++ { |
| 185 | trimmed := strings.TrimSpace(lines[i]) |
| 186 | if strings.HasPrefix(trimmed, "agent:") && getIndentation(lines[i]) == agentIndent { |
| 187 | agentStart = i |
| 188 | break |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | indentedAgentLines := indentLines(agentLines, agentIndent) |
| 193 | if agentStart == -1 { |
| 194 | newLines := make([]string, 0, len(lines)+len(indentedAgentLines)) |
| 195 | newLines = append(newLines, lines[:sandboxIdx+1]...) |
| 196 | newLines = append(newLines, indentedAgentLines...) |
| 197 | newLines = append(newLines, lines[sandboxIdx+1:]...) |
| 198 | return newLines, true |
| 199 | } |
| 200 | |
| 201 | agentEnd := agentStart + 1 |
| 202 | agentFieldIndent := getIndentation(lines[agentStart]) |
| 203 | for agentEnd < sandboxEnd { |
| 204 | trimmed := strings.TrimSpace(lines[agentEnd]) |
| 205 | if trimmed == "" { |
| 206 | agentEnd++ |
| 207 | continue |
| 208 | } |
| 209 | if strings.HasPrefix(trimmed, "#") { |
| 210 | if len(getIndentation(lines[agentEnd])) > len(agentFieldIndent) { |
| 211 | agentEnd++ |
| 212 | continue |
no test coverage detected