CreateWorkflowMarkdownFile creates a new workflow markdown file with template content
(workflowName string, verbose bool, force bool, engine string)
| 17 | |
| 18 | // CreateWorkflowMarkdownFile creates a new workflow markdown file with template content |
| 19 | func CreateWorkflowMarkdownFile(workflowName string, verbose bool, force bool, engine string) error { |
| 20 | commandsLog.Printf("Creating new workflow: name=%s, force=%v, engine=%s", workflowName, force, engine) |
| 21 | |
| 22 | // Normalize the workflow name by removing .md extension if present |
| 23 | // This ensures consistent behavior whether user provides "my-workflow" or "my-workflow.md" |
| 24 | workflowName = strings.TrimSuffix(workflowName, ".md") |
| 25 | commandsLog.Printf("Normalized workflow name: %s", workflowName) |
| 26 | |
| 27 | console.LogVerbose(verbose, "Creating new workflow: "+workflowName) |
| 28 | |
| 29 | // Get current working directory for .github/workflows |
| 30 | workingDir, err := os.Getwd() |
| 31 | if err != nil { |
| 32 | commandsLog.Printf("Failed to get working directory: %v", err) |
| 33 | return fmt.Errorf("failed to get current working directory: %w", err) |
| 34 | } |
| 35 | |
| 36 | // Create .github/workflows directory if it doesn't exist |
| 37 | githubWorkflowsDir := filepath.Join(workingDir, constants.GetWorkflowDir()) |
| 38 | commandsLog.Printf("Creating workflows directory: %s", githubWorkflowsDir) |
| 39 | |
| 40 | // Validate the directory path |
| 41 | githubWorkflowsDir, err = fileutil.ValidateAbsolutePath(githubWorkflowsDir) |
| 42 | if err != nil { |
| 43 | commandsLog.Printf("Invalid workflows directory path: %v", err) |
| 44 | return fmt.Errorf("invalid workflows directory path: %w", err) |
| 45 | } |
| 46 | |
| 47 | if err := os.MkdirAll(githubWorkflowsDir, constants.DirPermPublic); err != nil { |
| 48 | commandsLog.Printf("Failed to create workflows directory: %v", err) |
| 49 | return fmt.Errorf("failed to create .github/workflows directory: %w", err) |
| 50 | } |
| 51 | |
| 52 | // Construct the destination file path |
| 53 | destFile := filepath.Join(githubWorkflowsDir, workflowName+".md") |
| 54 | commandsLog.Printf("Destination file: %s", destFile) |
| 55 | |
| 56 | // Validate the destination file path |
| 57 | destFile, err = fileutil.ValidateAbsolutePath(destFile) |
| 58 | if err != nil { |
| 59 | commandsLog.Printf("Invalid destination file path: %v", err) |
| 60 | return fmt.Errorf("invalid destination file path: %w", err) |
| 61 | } |
| 62 | |
| 63 | // Check if destination file already exists |
| 64 | if fileutil.FileExists(destFile) && !force { |
| 65 | commandsLog.Printf("Workflow file already exists and force=false: %s", destFile) |
| 66 | return fmt.Errorf("workflow file '%s' already exists. Use --force to overwrite", destFile) |
| 67 | } |
| 68 | |
| 69 | // Create the template content |
| 70 | template := createWorkflowTemplate(workflowName, engine) |
| 71 | |
| 72 | // Write the template to file with restrictive permissions (owner-only) |
| 73 | if err := os.WriteFile(destFile, []byte(template), constants.FilePermSensitive); err != nil { |
| 74 | return fmt.Errorf("failed to write workflow file '%s': %w", destFile, err) |
| 75 | } |
| 76 |