šŸŒ²

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.
āžœ student-grading-java git:(main) tree src src ā”œā”€ā”€ main ā”‚ ā”œā”€ā”€ java ā”‚ ā”‚ ā””ā”€ā”€ in ā”‚ ā”‚ ā””ā”€ā”€ one2n ā”‚ ā”‚ ā””ā”€ā”€ exercise ā”‚ ā”‚ ā”œā”€ā”€ Grade.java ā”‚ ā”‚ ā”œā”€ā”€ Grader.java ā”‚ ā”‚ ā””ā”€ā”€ Student.java ā”‚ ā””ā”€ā”€ resources ā””ā”€ā”€ test ā”œā”€ā”€ java ā”‚ ā””ā”€ā”€ in ā”‚ ā””ā”€ā”€ one2n ā”‚ ā””ā”€ā”€ exercise ā”‚ ā””ā”€ā”€ GraderTest.java ā””ā”€ā”€ resources ā””ā”€ā”€ grades.csv 12 directories, 5 files
Assumptions:
  • Your program behaviour should be the same as thetree 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.
āžœ student-grading-java git:(main) tree -f src src ā”œā”€ā”€ src/main ā”‚ ā”œā”€ā”€ src/main/java ā”‚ ā”‚ ā””ā”€ā”€ src/main/java/in ā”‚ ā”‚ ā””ā”€ā”€ src/main/java/in/one2n ā”‚ ā”‚ ā””ā”€ā”€ src/main/java/in/one2n/exercise ā”‚ ā”‚ ā”œā”€ā”€ src/main/java/in/one2n/exercise/Grade.java ā”‚ ā”‚ ā”œā”€ā”€ src/main/java/in/one2n/exercise/Grader.java ā”‚ ā”‚ ā””ā”€ā”€ src/main/java/in/one2n/exercise/Student.java ā”‚ ā””ā”€ā”€ src/main/resources ā””ā”€ā”€ src/test ā”œā”€ā”€ src/test/java ā”‚ ā””ā”€ā”€ src/test/java/in ā”‚ ā””ā”€ā”€ src/test/java/in/one2n ā”‚ ā””ā”€ā”€ src/test/java/in/one2n/exercise ā”‚ ā””ā”€ā”€ src/test/java/in/one2n/exercise/GraderTest.java ā””ā”€ā”€ src/test/resources ā””ā”€ā”€ src/test/resources/grades.csv 12 directories, 5 files
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.
āžœ student-grading-java git:(main) tree -d src src ā”œā”€ā”€ main ā”‚ ā”œā”€ā”€ java ā”‚ ā”‚ ā””ā”€ā”€ in ā”‚ ā”‚ ā””ā”€ā”€ one2n ā”‚ ā”‚ ā””ā”€ā”€ exercise ā”‚ ā””ā”€ā”€ resources ā””ā”€ā”€ test ā”œā”€ā”€ java ā”‚ ā””ā”€ā”€ in ā”‚ ā””ā”€ā”€ one2n ā”‚ ā””ā”€ā”€ exercise ā””ā”€ā”€ resources 12 directories
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)

Allow traversing specified nested levels only.
āžœ student-grading-java git:(main) tree -L 3 src src ā”œā”€ā”€ main ā”‚ ā”œā”€ā”€ java ā”‚ ā”‚ ā””ā”€ā”€ in ā”‚ ā””ā”€ā”€ resources ā””ā”€ā”€ test ā”œā”€ā”€ java ā”‚ ā””ā”€ā”€ in ā””ā”€ā”€ resources ā””ā”€ā”€ grades.csv 8 directories, 1 file
Expectations:
  • As mentioned in previous stories, reuse code as much as possible.
  • Unit tests

Story 5 (file option)

Print file permissions for all files.
āžœ student-grading-java git:(main) tree -p src src ā”œā”€ā”€ [drwxr-xr-x] main ā”‚ ā”œā”€ā”€ [drwxr-xr-x] java ā”‚ ā”‚ ā””ā”€ā”€ [drwxr-xr-x] in ā”‚ ā”‚ ā””ā”€ā”€ [drwxr-xr-x] one2n ā”‚ ā”‚ ā””ā”€ā”€ [drwxr-xr-x] exercise ā”‚ ā”‚ ā”œā”€ā”€ [-rw-r--r--] Grade.java ā”‚ ā”‚ ā”œā”€ā”€ [-rw-r--r--] Grader.java ā”‚ ā”‚ ā””ā”€ā”€ [-rw-r--r--] Student.java ā”‚ ā””ā”€ā”€ [drwxr-xr-x] resources ā””ā”€ā”€ [drwxr-xr-x] test ā”œā”€ā”€ [drwxr-xr-x] java ā”‚ ā””ā”€ā”€ [drwxr-xr-x] in ā”‚ ā””ā”€ā”€ [drwxr-xr-x] one2n ā”‚ ā””ā”€ā”€ [drwxr-xr-x] exercise ā”‚ ā””ā”€ā”€ [-rw-r--r--] GraderTest.java ā””ā”€ā”€ [drwxr-xr-x] resources ā””ā”€ā”€ [-rw-r--r--] grades.csv 12 directories, 5 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.
āžœ student-grading-java git:(main) āœ— tree -t src src ā”œā”€ā”€ main ā”‚ ā”œā”€ā”€ java ā”‚ ā”‚ ā””ā”€ā”€ in ā”‚ ā”‚ ā””ā”€ā”€ one2n ā”‚ ā”‚ ā””ā”€ā”€ exercise ā”‚ ā”‚ ā”œā”€ā”€ Grade.java ā”‚ ā”‚ ā”œā”€ā”€ Grader.java ā”‚ ā”‚ ā””ā”€ā”€ Student.java ā”‚ ā””ā”€ā”€ resources ā””ā”€ā”€ test ā”œā”€ā”€ java ā”‚ ā””ā”€ā”€ in ā”‚ ā””ā”€ā”€ one2n ā”‚ ā””ā”€ā”€ exercise ā”‚ ā””ā”€ā”€ GraderTest.java ā””ā”€ā”€ resources ā””ā”€ā”€ grades.csv 12 directories, 5 files

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.
āžœ student-grading-java git:(main) āœ— tree -X -L 4 src <?xml version="1.0" encoding="UTF-8"?> <tree> <directory name="src"> <directory name="main"> <directory name="java"> <directory name="in"> <directory name="one2n"> </directory> </directory> </directory> <directory name="resources"> </directory> </directory> <directory name="test"> <directory name="java"> <directory name="in"> <directory name="one2n"> </directory> </directory> </directory> <directory name="resources"> <file name="grades.csv"></file> </directory> </directory> </directory> <report> <directories>10</directories> <files>1</files> </report> </tree>

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.
āžœ student-grading-java git:(main) āœ— tree -if src src src/main src/main/java src/main/java/in src/main/java/in/one2n src/main/java/in/one2n/exercise src/main/java/in/one2n/exercise/Grade.java src/main/java/in/one2n/exercise/Grader.java src/main/java/in/one2n/exercise/Student.java src/main/resources src/test src/test/java src/test/java/in src/test/java/in/one2n src/test/java/in/one2n/exercise src/test/java/in/one2n/exercise/GraderTest.java src/test/resources src/test/resources/grades.csv 12 directories, 5 files
Feel free to make suitable assumptions if needed, and ensure to document them in README.md
Ā