buildFromTemplate resolves a template, renders it with variables, and applies CLI flag overrides.
(cmd *cobra.Command, id, title string)
| 184 | |
| 185 | // buildFromTemplate resolves a template, renders it with variables, and applies CLI flag overrides. |
| 186 | func buildFromTemplate(cmd *cobra.Command, id, title string) (string, error) { |
| 187 | projectRoot := resolveProjectRoot() |
| 188 | userHome, _ := os.UserHomeDir() |
| 189 | |
| 190 | tmpl, ok := template.Resolve(addTemplate, projectRoot, userHome) |
| 191 | if !ok { |
| 192 | available := template.Discover(projectRoot, userHome) |
| 193 | names := make([]string, len(available)) |
| 194 | for i, t := range available { |
| 195 | names[i] = t.Name |
| 196 | } |
| 197 | return "", fmt.Errorf("template %q not found (available: %s)", addTemplate, strings.Join(names, ", ")) |
| 198 | } |
| 199 | |
| 200 | vars := map[string]string{ |
| 201 | "id": id, |
| 202 | "title": title, |
| 203 | "date": time.Now().Format("2006-01-02"), |
| 204 | } |
| 205 | |
| 206 | content := template.RenderTask(tmpl, vars) |
| 207 | |
| 208 | // Apply CLI flag overrides (only for explicitly-set flags) |
| 209 | overrides := buildTemplateOverrides(cmd) |
| 210 | content = template.ApplyOverrides(content, overrides) |
| 211 | |
| 212 | return content, nil |
| 213 | } |
| 214 | |
| 215 | // buildTemplateOverrides collects frontmatter overrides from explicitly-set CLI flags. |
| 216 | func buildTemplateOverrides(cmd *cobra.Command) map[string]string { |
no test coverage detected