addAllowedToNetwork adds an allowed field to an existing network block
(lines []string, domains []string)
| 363 | |
| 364 | // addAllowedToNetwork adds an allowed field to an existing network block |
| 365 | func addAllowedToNetwork(lines []string, domains []string) []string { |
| 366 | var result []string |
| 367 | var inNetworkBlock bool |
| 368 | var networkIndent string |
| 369 | var insertIndex = -1 |
| 370 | |
| 371 | for i, line := range lines { |
| 372 | trimmedLine := strings.TrimSpace(line) |
| 373 | |
| 374 | if strings.HasPrefix(trimmedLine, "network:") { |
| 375 | inNetworkBlock = true |
| 376 | networkIndent = getIndentation(line) |
| 377 | } |
| 378 | |
| 379 | if inNetworkBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") { |
| 380 | if hasExitedBlock(line, networkIndent) { |
| 381 | // Found the end of network block |
| 382 | insertIndex = i |
| 383 | break |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | result = append(result, line) |
| 388 | } |
| 389 | |
| 390 | if insertIndex > 0 { |
| 391 | // Insert allowed before the next top-level block |
| 392 | allowedLines := []string{ |
| 393 | networkIndent + " allowed:", |
| 394 | } |
| 395 | for _, domain := range domains { |
| 396 | allowedLines = append(allowedLines, fmt.Sprintf("%s - %s", networkIndent, domain)) |
| 397 | } |
| 398 | |
| 399 | result = append(result, allowedLines...) |
| 400 | result = append(result, lines[insertIndex:]...) |
| 401 | } else { |
| 402 | // Append at the end of network block |
| 403 | networkIndentStr := "" |
| 404 | for i := range slices.Backward(result) { |
| 405 | trimmed := strings.TrimSpace(result[i]) |
| 406 | if strings.HasPrefix(trimmed, "network:") { |
| 407 | networkIndentStr = getIndentation(result[i]) |
| 408 | break |
| 409 | } |
| 410 | } |
| 411 | result = append(result, networkIndentStr+" allowed:") |
| 412 | for _, domain := range domains { |
| 413 | result = append(result, fmt.Sprintf("%s - %s", networkIndentStr, domain)) |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return result |
| 418 | } |
no test coverage detected