MCPcopy Create free account
hub / github.com/danielmiessler/Fabric / addFileToDirectory

Function addFileToDirectory

cmd/code2context/code.go:204–250  ·  view source on GitHub ↗

addFileToDirectory adds a file to the correct directory in the structure

(root *FileItem, path, content, rootDir string)

Source from the content-addressed store, hash-verified

202
203// addFileToDirectory adds a file to the correct directory in the structure
204func addFileToDirectory(root *FileItem, path, content, rootDir string) {
205 parts := strings.Split(path, string(filepath.Separator))
206
207 // If this is a file at the root level
208 if len(parts) == 1 {
209 root.Contents = append(root.Contents, FileItem{
210 Type: "file",
211 Name: parts[0],
212 Content: content,
213 })
214 return
215 }
216
217 // Otherwise, find or create the directory path
218 current := root
219 for i := 0; i < len(parts)-1; i++ {
220 dirName := parts[i]
221 found := false
222
223 // Look for existing directory
224 for j, item := range current.Contents {
225 if item.Type == "directory" && item.Name == dirName {
226 current = &current.Contents[j]
227 found = true
228 break
229 }
230 }
231
232 // Create directory if not found
233 if !found {
234 newDir := FileItem{
235 Type: "directory",
236 Name: dirName,
237 Contents: []FileItem{},
238 }
239 current.Contents = append(current.Contents, newDir)
240 current = &current.Contents[len(current.Contents)-1]
241 }
242 }
243
244 // Add the file to the current directory
245 current.Contents = append(current.Contents, FileItem{
246 Type: "file",
247 Name: parts[len(parts)-1],
248 Content: content,
249 })
250}

Callers 2

ScanDirectoryFunction · 0.85
ScanFilesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected