getSandboxFalseToAgentFalseCodemod creates a codemod for converting sandbox: false to sandbox.agent: false
()
| 10 | |
| 11 | // getSandboxFalseToAgentFalseCodemod creates a codemod for converting sandbox: false to sandbox.agent: false |
| 12 | func getSandboxFalseToAgentFalseCodemod() Codemod { |
| 13 | return Codemod{ |
| 14 | ID: "sandbox-false-to-agent-false", |
| 15 | Name: "Convert sandbox: false to sandbox.agent: false", |
| 16 | Description: "Converts top-level 'sandbox: false' to 'sandbox: { agent: false }' as top-level boolean is no longer supported", |
| 17 | IntroducedIn: "0.10.0", |
| 18 | Apply: func(content string, frontmatter map[string]any) (string, bool, error) { |
| 19 | // Check if sandbox exists and is a boolean false |
| 20 | sandboxValue, hasSandbox := frontmatter["sandbox"] |
| 21 | if !hasSandbox { |
| 22 | return content, false, nil |
| 23 | } |
| 24 | |
| 25 | sandboxBool, isBool := sandboxValue.(bool) |
| 26 | if !isBool || sandboxBool { |
| 27 | // Not a boolean false, skip |
| 28 | return content, false, nil |
| 29 | } |
| 30 | |
| 31 | newContent, applied, err := applyFrontmatterLineTransform(content, func(lines []string) ([]string, bool) { |
| 32 | var modified bool |
| 33 | result := make([]string, 0, len(lines)) |
| 34 | for i, line := range lines { |
| 35 | trimmedLine := strings.TrimSpace(line) |
| 36 | |
| 37 | // Check if this is the "sandbox: false" line |
| 38 | if strings.HasPrefix(trimmedLine, "sandbox:") { |
| 39 | if strings.Contains(trimmedLine, "sandbox: false") || strings.Contains(trimmedLine, "sandbox:false") { |
| 40 | // Get the indentation of the original line |
| 41 | indent := getIndentation(line) |
| 42 | |
| 43 | // Replace with sandbox.agent: false format |
| 44 | result = append(result, indent+"sandbox:") |
| 45 | result = append(result, indent+" agent: false") |
| 46 | |
| 47 | modified = true |
| 48 | sandboxAgentCodemodLog.Printf("Converted sandbox: false to sandbox.agent: false on line %d", i+1) |
| 49 | continue |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | result = append(result, line) |
| 54 | } |
| 55 | return result, modified |
| 56 | }) |
| 57 | if applied { |
| 58 | sandboxAgentCodemodLog.Print("Applied sandbox: false to sandbox.agent: false conversion") |
| 59 | } |
| 60 | return newContent, applied, err |
| 61 | }, |
| 62 | } |
| 63 | } |