| 45 | } |
| 46 | |
| 47 | func runSpec(_ *cobra.Command, _ []string) error { |
| 48 | if specStdout { |
| 49 | fmt.Print(string(specTemplate)) |
| 50 | return nil |
| 51 | } |
| 52 | |
| 53 | targetDir := GetGlobalFlags().TaskDir |
| 54 | |
| 55 | // Verify target directory exists |
| 56 | info, err := os.Stat(targetDir) |
| 57 | if err != nil { |
| 58 | return fmt.Errorf("directory does not exist: %s", targetDir) |
| 59 | } |
| 60 | if !info.IsDir() { |
| 61 | return fmt.Errorf("not a directory: %s", targetDir) |
| 62 | } |
| 63 | |
| 64 | outputPath := filepath.Join(targetDir, specFilename) |
| 65 | |
| 66 | absPath, err := filepath.Abs(outputPath) |
| 67 | if err != nil { |
| 68 | absPath = outputPath |
| 69 | } |
| 70 | |
| 71 | // Check if file exists |
| 72 | if !specForce { |
| 73 | if _, err := os.Stat(outputPath); err == nil { |
| 74 | return fmt.Errorf("%s already exists at %s (use --force to overwrite)", specFilename, absPath) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Write the file |
| 79 | if err := os.WriteFile(outputPath, specTemplate, 0644); err != nil { |
| 80 | return fmt.Errorf("failed to write %s: %w", specFilename, err) |
| 81 | } |
| 82 | |
| 83 | if !GetGlobalFlags().Quiet { |
| 84 | fmt.Printf("Created %s\n", absPath) |
| 85 | } |
| 86 | |
| 87 | return nil |
| 88 | } |