🌲
Tree exercise
Problem statement
Write a command line program that implements Unix tree
like functionality. Read the man page for grep if you don't know what it does. Implement all options such as -f (print relative filepath), -d (print only directories), -L (print only specified levels of the tree), -p (print file permissions), -t (sort files by last modification time), -X (print output in XML format), -J (print output in JSON format).
The program should have the following features.
Story 1
Print tree structure recursively with proper formatting for a given directory. Also, print a summary at the end.
Assumptions:
Your program behaviour should be the same as the
tree
command.
The output is printed on STDOUT.
Use pipe (|) and dashes (--) to indent vertically and horizontally.
The tree utility is packaged as a standalone binary (./tree). Based on the programming language you're using, you may be able to create a standalone utility (e.g., in Go). If not, use any CLI execution method in your language (e.g. java -jar tree.jar for Java)
Expectations:
Write test cases for empty directories, nested empty directories, directories with multiple files, etc.
Your code should handle errors when there is no directory read (or execute) permission for nested directories.
Story 2 (listing option)
Print the relative path to the directory being searched.
Expectations:
Handle argument parsing in the code
Unit tests
Can you reuse the code and design from the previous story? How will you refactor your existing code to make this code reuse possible?
Story 3 (listing option)
Only print directories, not files.
Expectations:
Reuse code from previous stories as much as possible. Make your code modular and extensible.
Write test cases for this story.
Story 4 (listing option)
Expectations:
As mentioned in previous stories, reuse code as much as possible.
Unit tests
Story 5 (file option)
Print file permissions for all files.
Story 6 (sorting option)
Sort the output by the last modification time instead of alphabetically (the default). Note the actual output is just for indication only.
Story 7 (XML/JSON option)
Print output in the xml format.
X
Turn on XML output. Outputs the directory tree as an XML formatted file.
J
Turn on JSON output. Outputs the directory tree as a JSON formatted array.
Story 8 (graphics option)
Do not print the indentation lines, typically used in conjunction with the -f option. Also, remove as much whitespace as possible when used with the -J or -x options.
Feel free to make suitable assumptions if needed, and ensure to document them in README.md