@@ -17,16 +17,11 @@ import (
17
17
// docsCmd represents the docs command
18
18
var docsCmd = & cobra.Command {
19
19
Use : "docs" ,
20
- Short : "Document a repository of files" ,
20
+ Short : "Document a repository of files or a single file " ,
21
21
Long : `Document an entire repository of files. Specify the path to the repo as the first positional argument. This command will recursively
22
- search for files in the directory and document them.
22
+ search for files in the directory and document them. If a single file is specified, it will be documented.
23
23
` ,
24
- Args : cobra .PositionalArgs (func (cmd * cobra.Command , args []string ) error {
25
- if len (args ) < 1 {
26
- return fmt .Errorf ("requires a path to a repository" )
27
- }
28
- return nil
29
- }),
24
+ Args : cobra .ExactArgs (1 ),
30
25
Run : func (cmd * cobra.Command , args []string ) {
31
26
repoPath = args [0 ]
32
27
@@ -61,58 +56,119 @@ search for files in the directory and document them.
61
56
os .Exit (1 )
62
57
}
63
58
64
- repo , err := utils . GetRepo (repoPath , ignoreFilePath , ignoreGitignore )
59
+ info , err := os . Stat (repoPath )
65
60
if err != nil {
66
- log .Errorf ("Error: %s" , err )
61
+ log .Errorf ("Error getting file info : %s" , err )
67
62
os .Exit (1 )
68
63
}
69
64
70
- for _ , file := range repo .Files {
71
- var contents string
65
+ if info .IsDir () {
66
+ repo , err := utils .GetRepo (repoPath , ignoreFilePath , ignoreGitignore )
67
+ if err != nil {
68
+ log .Errorf ("Error: %s" , err )
69
+ os .Exit (1 )
70
+ }
72
71
73
- path := filepath .Join (repoPath , file .Path )
72
+ for _ , file := range repo .Files {
73
+ var contents string
74
74
75
- if outputFile != "" {
76
- fmt .Println ("Documenting file" , file .Path )
77
- }
75
+ path := filepath .Join (repoPath , file .Path )
76
+
77
+ if outputFile != "" {
78
+ fmt .Println ("Documenting file" , file .Path )
79
+ }
80
+
81
+ if chatPrompt == "" {
82
+ chatPrompt = "Write documentation for the following code snippet. The file name is" + file .Path + ":"
83
+ }
84
+
85
+ fileContents , err := utils .LoadFile (path )
86
+ if err != nil {
87
+ log .Warnf ("Error loading file %s: %s" , path , err )
88
+ continue
89
+ }
90
+
91
+ if inlineMode || ! markdownMode {
92
+ contents , err = ai .SingleFile (path , fileContents , chatPrompt , conf .APIKey , conf .Model )
93
+ } else {
94
+ contents , err = ai .Markdown (path , fileContents , chatPrompt , conf .APIKey , conf .Model )
95
+ }
96
+
97
+ if err != nil {
98
+ log .Warnf ("Error documenting file %s: %s" , path , err )
99
+ continue
100
+ }
78
101
102
+ if outputFile != "" && markdownMode {
103
+ // write the string to the output file
104
+ // append if the file already exists
105
+ file , err := os .OpenFile (outputFile , os .O_WRONLY | os .O_CREATE | os .O_APPEND , 0644 )
106
+ if err != nil {
107
+ log .Errorf ("Error: %s" , err )
108
+ os .Exit (1 )
109
+ }
110
+
111
+ _ , err = file .WriteString (contents )
112
+ if err != nil {
113
+ log .Errorf ("Error: %s" , err )
114
+ os .Exit (1 )
115
+ }
116
+
117
+ file .Close ()
118
+ } else if overwriteOriginal {
119
+ // overwrite the original file
120
+ // clear the contents of the file
121
+ file , err := os .OpenFile (filePath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0644 )
122
+ if err != nil {
123
+ log .Errorf ("Error: %s" , err )
124
+ os .Exit (1 )
125
+ }
126
+
127
+ // write the new contents to the file
128
+ _ , err = file .WriteString (contents )
129
+ if err != nil {
130
+ log .Errorf ("Error: %s" , err )
131
+ os .Exit (1 )
132
+ }
133
+
134
+ file .Close ()
135
+ } else {
136
+ fmt .Println (contents )
137
+ }
138
+ }
139
+ } else {
79
140
if chatPrompt == "" {
80
- chatPrompt = "Write documentation for the following code snippet. The file name is" + file . Path + " :"
141
+ chatPrompt = "Write documentation for the following code snippet:"
81
142
}
82
143
83
- fileContents , err := utils .LoadFile (path )
144
+ filePath := repoPath
145
+
146
+ var contents string
147
+
148
+ fileContents , err := utils .LoadFile (filePath )
84
149
if err != nil {
85
- log .Warnf ("Error loading file %s : %s" , path , err )
86
- continue
150
+ log .Errorf ("Error: %s" , err )
151
+ os . Exit ( 1 )
87
152
}
88
153
89
154
if inlineMode || ! markdownMode {
90
- contents , err = ai .SingleFile (path , fileContents , chatPrompt , conf .APIKey , conf .Model )
155
+ contents , err = ai .SingleFile (filePath , fileContents , chatPrompt , conf .APIKey , conf .Model )
91
156
} else {
92
- contents , err = ai .Markdown (path , fileContents , chatPrompt , conf .APIKey , conf .Model )
157
+ contents , err = ai .Markdown (filePath , fileContents , chatPrompt , conf .APIKey , conf .Model )
93
158
}
94
159
95
160
if err != nil {
96
- log .Warnf ("Error documenting file %s : %s" , path , err )
97
- continue
161
+ log .Errorf ("Error: %s" , err )
162
+ os . Exit ( 1 )
98
163
}
99
164
100
- if outputFile != "" && markdownMode {
165
+ if outputFile != "" {
101
166
// write the string to the output file
102
- // append if the file already exists
103
- file , err := os .OpenFile (outputFile , os .O_WRONLY | os .O_CREATE | os .O_APPEND , 0644 )
167
+ err = os .WriteFile (outputFile , []byte (contents ), 0644 )
104
168
if err != nil {
105
169
log .Errorf ("Error: %s" , err )
106
170
os .Exit (1 )
107
171
}
108
-
109
- _ , err = file .WriteString (contents )
110
- if err != nil {
111
- log .Errorf ("Error: %s" , err )
112
- os .Exit (1 )
113
- }
114
-
115
- file .Close ()
116
172
} else if overwriteOriginal {
117
173
// overwrite the original file
118
174
// clear the contents of the file
@@ -128,13 +184,10 @@ search for files in the directory and document them.
128
184
log .Errorf ("Error: %s" , err )
129
185
os .Exit (1 )
130
186
}
131
-
132
- file .Close ()
133
187
} else {
134
188
fmt .Println (contents )
135
189
}
136
190
}
137
-
138
191
},
139
192
}
140
193
0 commit comments