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

Function addFileToDirectory

cmd/code2context/code.go:188–229  ·  view source on GitHub ↗

addFileToDirectory adds a file under root at path. It creates missing intermediate directories.

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

Source from the content-addressed store, hash-verified

186
187// addFileToDirectory adds a file under root at path. It creates missing intermediate directories.
188func addFileToDirectory(root *FileItem, path, content, rootDir string) {
189 parts := strings.Split(path, string(filepath.Separator))
190
191 if len(parts) == 1 {
192 root.Contents = append(root.Contents, FileItem{
193 Type: "file",
194 Name: parts[0],
195 Content: content,
196 })
197 return
198 }
199
200 current := root
201 for i := 0; i < len(parts)-1; i++ {
202 dirName := parts[i]
203 found := false
204
205 for j, item := range current.Contents {
206 if item.Type == "directory" && item.Name == dirName {
207 current = &current.Contents[j]
208 found = true
209 break
210 }
211 }
212
213 if !found {
214 newDir := FileItem{
215 Type: "directory",
216 Name: dirName,
217 Contents: []FileItem{},
218 }
219 current.Contents = append(current.Contents, newDir)
220 current = &current.Contents[len(current.Contents)-1]
221 }
222 }
223
224 current.Contents = append(current.Contents, FileItem{
225 Type: "file",
226 Name: parts[len(parts)-1],
227 Content: content,
228 })
229}

Callers 2

ScanDirectoryFunction · 0.85
ScanFilesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected