addTopLevelNetwork adds a new top-level network configuration
(lines []string, domains []string)
| 241 | |
| 242 | // addTopLevelNetwork adds a new top-level network configuration |
| 243 | func addTopLevelNetwork(lines []string, domains []string) []string { |
| 244 | // Find a good place to insert (after on: field, or at the beginning) |
| 245 | insertIndex := 0 |
| 246 | for i, line := range lines { |
| 247 | trimmed := strings.TrimSpace(line) |
| 248 | if strings.HasPrefix(trimmed, "on:") { |
| 249 | // Insert after the on: block |
| 250 | insertIndex = i + 1 |
| 251 | // Skip any nested content under on: |
| 252 | if !strings.Contains(trimmed, "on: ") || strings.HasPrefix(trimmed, "on:") && len(trimmed) == 3 { |
| 253 | // on: is a block, find the end |
| 254 | onIndent := getIndentation(line) |
| 255 | for j := i + 1; j < len(lines); j++ { |
| 256 | nextLine := lines[j] |
| 257 | nextTrimmed := strings.TrimSpace(nextLine) |
| 258 | if nextTrimmed == "" { |
| 259 | continue |
| 260 | } |
| 261 | if hasExitedBlock(nextLine, onIndent) { |
| 262 | insertIndex = j |
| 263 | break |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | break |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Build network configuration lines |
| 272 | var networkLines []string |
| 273 | networkLines = append(networkLines, "network:") |
| 274 | networkLines = append(networkLines, " allowed:") |
| 275 | for _, domain := range domains { |
| 276 | networkLines = append(networkLines, " - "+domain) |
| 277 | } |
| 278 | |
| 279 | // Insert at the determined position |
| 280 | result := make([]string, 0, len(lines)+len(networkLines)) |
| 281 | result = append(result, lines[:insertIndex]...) |
| 282 | result = append(result, networkLines...) |
| 283 | result = append(result, lines[insertIndex:]...) |
| 284 | |
| 285 | return result |
| 286 | } |
| 287 | |
| 288 | // updateNetworkAllowed updates the existing top-level network.allowed configuration |
| 289 | func updateNetworkAllowed(lines []string, domains []string) []string { |
no test coverage detected