MCPcopy Create free account
hub / github.com/github/gh-aw / addTopLevelNetwork

Function addTopLevelNetwork

pkg/cli/codemod_mcp_network.go:243–286  ·  view source on GitHub ↗

addTopLevelNetwork adds a new top-level network configuration

(lines []string, domains []string)

Source from the content-addressed store, hash-verified

241
242// addTopLevelNetwork adds a new top-level network configuration
243func 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
289func updateNetworkAllowed(lines []string, domains []string) []string {

Calls 2

getIndentationFunction · 0.85
hasExitedBlockFunction · 0.85

Tested by

no test coverage detected