When writing scripts that create or manipulate file and directories, it’s all too easy to get lost using chmod and chdir operating system directives. Move here, do that, change this, write that. Bah. Unless you track very carefully what directory your script is working in, it’s all too easy to get lost. There is always an easier way.
After a quick hunt around using my Google powers and checking out various Go sites, I didn’t find what I was looking for. That might have been due to not looking for the right thing or using the correct keywords, but find it I did not. Therefore, the next logical step was to build what I required, then consume. The result may prove useful.
How do we use it?
Build a root node
Add directories or files using the concept of parents
Generate the tree
Manipulate the file contents
Steps 1 through 6 rely on exported package level functions with package level constants for ease of use.
What does goFAL stand for?
goFAL = Go File Abstraction Layer.
The example makes use of Godeps, so restore the dependencies with ease and build.
This assumes a working Go toolchain install. I always work with the latest stable. Simples.
This example involves:
Create root directory
Create content directory
Create file in root directory
Create file in content directory
Each file and directory has permissions associated with them. Hashes will only be calculated for files.
Note, it’s possible to print out string representations of the tree nodes in a couple of ways which includes the full tree with hierarchical indentations. Two examples below. TreeString will print the tree from the perspective of the node that TreeString() is being called on.
Tree output as follows:
Write some tests
Add signature capabilities for specific branches or the tree
The Burn Down
This made my life easier!
Fork and work on master. Create PR and I’ll review.