UpdateWorkflowFrontmatter updates the frontmatter of a workflow file using a callback function
(workflowPath string, updateFunc func(frontmatter map[string]any) error, verbose bool)
| 20 | |
| 21 | // UpdateWorkflowFrontmatter updates the frontmatter of a workflow file using a callback function |
| 22 | func UpdateWorkflowFrontmatter(workflowPath string, updateFunc func(frontmatter map[string]any) error, verbose bool) error { |
| 23 | workflowUpdateLog.Printf("Updating workflow frontmatter: path=%s", workflowPath) |
| 24 | // Read the workflow file |
| 25 | content, err := os.ReadFile(workflowPath) |
| 26 | if err != nil { |
| 27 | return fmt.Errorf("failed to read workflow file: %w", err) |
| 28 | } |
| 29 | |
| 30 | // Parse frontmatter using existing helper |
| 31 | result, err := ExtractFrontmatterFromContent(string(content)) |
| 32 | if err != nil { |
| 33 | return fmt.Errorf("failed to parse frontmatter: %w", err) |
| 34 | } |
| 35 | |
| 36 | // Ensure frontmatter map exists |
| 37 | if result.Frontmatter == nil { |
| 38 | result.Frontmatter = make(map[string]any) |
| 39 | } |
| 40 | |
| 41 | // Apply the update function |
| 42 | if err := updateFunc(result.Frontmatter); err != nil { |
| 43 | return err |
| 44 | } |
| 45 | |
| 46 | // Convert back to YAML |
| 47 | updatedFrontmatter, err := yaml.Marshal(result.Frontmatter) |
| 48 | if err != nil { |
| 49 | return fmt.Errorf("failed to marshal updated frontmatter: %w", err) |
| 50 | } |
| 51 | |
| 52 | // Reconstruct the file content |
| 53 | updatedContent, err := ReconstructWorkflowFile(string(updatedFrontmatter), result.Markdown) |
| 54 | if err != nil { |
| 55 | return fmt.Errorf("failed to reconstruct workflow file: %w", err) |
| 56 | } |
| 57 | |
| 58 | // Write the updated content back to the file |
| 59 | if err := os.WriteFile(workflowPath, []byte(updatedContent), constants.FilePermPublic); err != nil { |
| 60 | return fmt.Errorf("failed to write updated workflow file: %w", err) |
| 61 | } |
| 62 | |
| 63 | workflowUpdateLog.Printf("Successfully updated workflow file: %s", workflowPath) |
| 64 | if verbose { |
| 65 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Updated workflow file: "+console.ToRelativePath(workflowPath))) |
| 66 | } |
| 67 | |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | // EnsureToolsSection ensures the tools section exists in frontmatter and returns it |
| 72 | func EnsureToolsSection(frontmatter map[string]any) map[string]any { |