generateWorkflow creates the markdown workflow file based on user selections
(force bool)
| 478 | |
| 479 | // generateWorkflow creates the markdown workflow file based on user selections |
| 480 | func (b *InteractiveWorkflowBuilder) generateWorkflow(force bool) error { |
| 481 | interactiveLog.Printf("Generating workflow file: name=%s, engine=%s, trigger=%s", b.WorkflowName, b.Engine, b.Trigger) |
| 482 | |
| 483 | // Get current working directory for .github/workflows |
| 484 | workingDir, err := os.Getwd() |
| 485 | if err != nil { |
| 486 | return fmt.Errorf("failed to get current working directory: %w", err) |
| 487 | } |
| 488 | |
| 489 | // Create .github/workflows directory if it doesn't exist |
| 490 | githubWorkflowsDir := filepath.Join(workingDir, constants.GetWorkflowDir()) |
| 491 | if err := os.MkdirAll(githubWorkflowsDir, constants.DirPermPublic); err != nil { |
| 492 | return fmt.Errorf("failed to create .github/workflows directory: %w", err) |
| 493 | } |
| 494 | |
| 495 | // Construct the destination file path |
| 496 | destFile := filepath.Join(githubWorkflowsDir, b.WorkflowName+".md") |
| 497 | |
| 498 | // Check if destination file already exists |
| 499 | if _, err := os.Stat(destFile); err == nil && !force { |
| 500 | var overwrite bool |
| 501 | confirmForm := huh.NewForm( |
| 502 | huh.NewGroup( |
| 503 | huh.NewConfirm(). |
| 504 | Title(fmt.Sprintf("Workflow file '%s' already exists. Overwrite?", filepath.Base(destFile))). |
| 505 | Affirmative("Yes, overwrite"). |
| 506 | Negative("No, cancel"). |
| 507 | Value(&overwrite), |
| 508 | ), |
| 509 | ).WithTheme(styles.HuhTheme).WithAccessible(console.IsAccessibleMode()) |
| 510 | |
| 511 | if err := confirmForm.Run(); err != nil { |
| 512 | return fmt.Errorf("confirmation failed: %w", err) |
| 513 | } |
| 514 | |
| 515 | if !overwrite { |
| 516 | return errors.New("workflow creation cancelled") |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // Generate workflow content |
| 521 | content := b.generateWorkflowContent() |
| 522 | |
| 523 | // Write the workflow to file with owner-only read/write permissions (0600) for security best practices |
| 524 | if err := os.WriteFile(destFile, []byte(content), constants.FilePermSensitive); err != nil { |
| 525 | return fmt.Errorf("failed to write workflow file '%s': %w", destFile, err) |
| 526 | } |
| 527 | |
| 528 | interactiveLog.Printf("Workflow file created successfully: %s", destFile) |
| 529 | fmt.Fprintf(os.Stderr, "Created new workflow: %s\n", destFile) |
| 530 | return nil |
| 531 | } |
| 532 | |
| 533 | // generateWorkflowContent creates the workflow markdown content |
| 534 | func (b *InteractiveWorkflowBuilder) generateWorkflowContent() string { |
no test coverage detected