Directory Structure is a go library for reading and transversing your local filesystem. Currently it consists of one package: structure
NOTE: This library does not do any modification to the actual local filesystem.
Parts of Structure
Structure provides three structs: Directory, File, and Descendants
Directory is the core of structure. It is a tree where each node contains the following:
- Name(): the name of the directory
- Path(): the path to the directory excluding Name()
- FullPath(): the full path to the directory including Name()
- Files(): pointers to the files contained in the directory
- SubDirectories(): pointers to Directories in this Directory
File represents a single file. It consists of the following:
- Name(): the name of the directory
- Path(): the path to the directory excluding Name()
- FullPath(): the full path to the directory including Name()
Descendants is used for convenience when getting all the Files and Directories that are descendants of a Directory. It consists of the following:
- Directories: the Directories that are Descendants of a Directory
- Files: the Files that are Descendants of a Directory
Functionality of Structure
There are two main ways to create a Directory tree
- Call NewDirectory(): This create a new directory with no files or subdirectories.
- Call GetDirectoryStructure(): This walks your local filesystem at the path provided and generates a full Directory tree that matches the given directory.
Directories and Files can be added to a directory tree by calling either directory.AddDirectory() or directory.AddFile() on any directory in the tree that is a ancestor of the new item. Either method takes the full path to the new item, and will create directories as needed between the ancestor Directory and the new item.
A Directory or File that is a direct child of the current directory can be found by calling directory.Directory() or directory.File() on the current directory and passing it the name of the Directory of File.
Descendants of a Directory can be found using the Descendant's full path by calling either directory.GetDirectory() or directory.GetFile()
A depth first search by name can be done by calling directory.FindDirectoryDepth() or directory.FindFileDepth()
A breadth first search by name can be done by calling directory.FindDirectoryBreadth() or directory.FindFileBreadth()