@@ -2,11 +2,7 @@ package cmd
22
33import (
44 "fmt"
5- "io"
65 "os"
7- "path/filepath"
8- "strings"
9-
106 "rules-cli/internal/formats"
117
128 "github.com/spf13/cobra"
@@ -17,8 +13,8 @@ var renderCmd = &cobra.Command{
1713 Use : "render [format]" ,
1814 Short : "Render rules to a specific format" ,
1915 Long : `Renders existing rules to a specified format.
20- Creates .{format}/ rules/ directory and copies all rules from the default location
21- (.rules/) to the target format location. Preserves directory structure of rule sets .` ,
16+ Copies all rules from the default location (. rules/) to the target format
17+ as described in render-formats.md .` ,
2218 Example : ` rules render cursor
2319 rules render continue` ,
2420 RunE : func (cmd * cobra.Command , args []string ) error {
@@ -44,20 +40,10 @@ Creates .{format}/rules/ directory and copies all rules from the default locatio
4440
4541 fmt .Printf ("Rendering rules to %s format...\n " , formatName )
4642
47- // Get target directory
48- targetDir , err := formats .GetRulesDirectory (formatName )
49- if err != nil {
50- return fmt .Errorf ("failed to get target directory for format %s: %w" , formatName , err )
51- }
52-
53- // Create target directory if it doesn't exist
54- if err := os .MkdirAll (targetDir , 0755 ); err != nil {
55- return fmt .Errorf ("failed to create target directory %s: %w" , targetDir , err )
56- }
57-
58- // Copy rules from source to target
59- if err := copyDir (sourceDir , targetDir ); err != nil {
60- return fmt .Errorf ("failed to copy rules to target directory: %w" , err )
43+ // Use the formats package to handle the rendering based on the target format
44+ verbose , _ := cmd .Flags ().GetBool ("verbose" )
45+ if err := formats .RenderRulesToFormat (sourceDir , formatName , verbose ); err != nil {
46+ return fmt .Errorf ("failed to render rules to %s format: %w" , formatName , err )
6147 }
6248
6349 fmt .Printf ("Successfully rendered rules to %s format\n " , formatName )
@@ -66,76 +52,7 @@ Creates .{format}/rules/ directory and copies all rules from the default locatio
6652 },
6753}
6854
69- // copyDir recursively copies a directory tree, preserving directory structure
70- func copyDir (src , dst string ) error {
71- // Get properties of source directory
72- srcInfo , err := os .Stat (src )
73- if err != nil {
74- return err
75- }
76-
77- // Create the destination directory
78- if err = os .MkdirAll (dst , srcInfo .Mode ()); err != nil {
79- return err
80- }
81-
82- // Get directory contents
83- entries , err := os .ReadDir (src )
84- if err != nil {
85- return err
86- }
87-
88- for _ , entry := range entries {
89- srcPath := filepath .Join (src , entry .Name ())
90- dstPath := filepath .Join (dst , entry .Name ())
91-
92- if entry .IsDir () {
93- // Recursively copy subdirectory
94- if err = copyDir (srcPath , dstPath ); err != nil {
95- return err
96- }
97- } else {
98- // Copy the file
99- if err = copyFile (srcPath , dstPath ); err != nil {
100- return err
101- }
102- }
103- }
104-
105- return nil
106- }
107-
108- // copyFile copies a single file from src to dst
109- func copyFile (src , dst string ) error {
110- // Skip if not a markdown file
111- if ! strings .HasSuffix (src , ".md" ) {
112- return nil
113- }
114-
115- // Open source file
116- in , err := os .Open (src )
117- if err != nil {
118- return err
119- }
120- defer in .Close ()
121-
122- // Create destination file
123- out , err := os .Create (dst )
124- if err != nil {
125- return err
126- }
127- defer out .Close ()
128-
129- // Copy contents
130- _ , err = io .Copy (out , in )
131- if err != nil {
132- return err
133- }
134-
135- // Flush to disk
136- return out .Sync ()
137- }
138-
13955func init () {
14056 rootCmd .AddCommand (renderCmd )
57+ renderCmd .Flags ().BoolP ("verbose" , "v" , false , "Enable verbose output" )
14158}
0 commit comments