addFileToDirectory adds a file under root at path. It creates missing intermediate directories.
(root *FileItem, path, content, rootDir string)
| 186 | |
| 187 | // addFileToDirectory adds a file under root at path. It creates missing intermediate directories. |
| 188 | func 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 = ¤t.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 = ¤t.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 | } |
no outgoing calls
no test coverage detected