Skip to content
Snippets Groups Projects
Commit deb53f6c authored by Ivanov, Ivan (UG - Computer Science)'s avatar Ivanov, Ivan (UG - Computer Science)
Browse files

Merge branch 'third_functionality' into 'master'

Third Functionality Completed. Tests and comments provided.

See merge request !2
parents e3068771 5847c4a2
Branches master
No related tags found
1 merge request!2Third Functionality Completed. Tests and comments provided.
build:
go get
run:
go run . $(directory) $(filter)
json:
go run . -j $(directory)
test:
go test
# Linux Tree CLI
## Usage
```
$ make build
$ make run directory=tests filter=2021-04-22 #Runs the command using filter and prints in terminal.
$ make json directory=tests #Runs the CLI using -j to output to JSON.
```
## Testing
```
$ make test
```
## Discussion
The application runs in 2 different modes: Terminal and JSON. Terminal outputs the tree structure. JSON outputs the structure to a JSON file.
## Dependencies
- You'd need to have GOPATH exported. GOBIN for MACOS for go get command
\ No newline at end of file
......@@ -5,6 +5,7 @@ import (
"os"
"path"
"sort"
"time"
)
//Counter strucutre need for presenting final results
......@@ -41,24 +42,26 @@ func opendirectory(base string) []string {
//Recursive function called on each directory. The function appends each path found
//to a subpath called next, checks if each path is directory or file and appends to counter.
//A print statment is based on the index of the path in the current directory.
func tree(prefix string, counter *Counter, base string) {
func tree(prefix string, counter *Counter, base string, t time.Time) {
dirnames := opendirectory(base)
for index, name := range dirnames {
//Exclude hiddenn folders
if name[0] == '.' {
continue
}
next := path.Join(base, name)
stat, _ := os.Stat(next)
if stat.ModTime().Before(t) {
continue
}
counter.isDir(next)
if index < len(dirnames)-1 {
fmt.Println(prefix+"├──", name)
tree(prefix+"│ ", counter, next)
tree(prefix+"│ ", counter, next, t)
} else {
fmt.Println(prefix+"└──", name)
tree(prefix+" ", counter, next)
tree(prefix+" ", counter, next, t)
}
}
}
......@@ -3,6 +3,7 @@ package main
import (
"fmt"
"testing"
"time"
)
/*
......@@ -63,7 +64,36 @@ Test if the tree function works by observing the counter.
*/
func TestTree(t *testing.T) {
counter := new(Counter)
tree("", counter, ".")
filter := "2021-04-02"
format = "2006-01-02"
time, err := time.Parse(format, filter)
if err != nil {
ErrorLogger.Print("Format:", format)
ErrorLogger.Print(err)
return
}
tree("", counter, ".", time)
assertEqual(t, counter.directories, 3, "")
assertEqual(t, counter.files, 12, "")
assertEqual(t, counter.files, 14, "")
}
/*
Test if the filter works on tree() by observing the counter.
*/
func TestFilter(t *testing.T) {
counter := new(Counter)
filter := "2021-04-23"
format = "2006-01-02"
time, err := time.Parse(format, filter)
if err != nil {
ErrorLogger.Print("Format:", format)
ErrorLogger.Print(err)
return
}
tree("", counter, ".", time)
assertEqual(t, counter.directories, 0, "")
assertEqual(t, counter.files, 9, "")
}
......@@ -26,7 +26,7 @@ func TestJsonTree(t *testing.T) {
assertEqual(t, folder.Name, ".", "")
assertEqual(t, len(folder.Folders), 1, "")
assertEqual(t, len(folder.Files), 8, "")
assertEqual(t, len(folder.Files), 10, "")
assertEqual(t, len(folder.Folders["tests"].Folders), 2, "")
assertEqual(t, len(folder.Folders["tests"].Files), 3, "")
......
......@@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"os"
"time"
)
//File structure representation
......@@ -30,6 +31,8 @@ func newFolder(name string) *Folder {
var (
ErrorLogger *log.Logger
directory string
filter string
format string
)
func main() {
......@@ -41,7 +44,7 @@ func main() {
//Start JSON routine.
directory = "."
if len(os.Args) > 2 {
directory = os.Args[1]
directory = os.Args[2]
}
output := newFolder(directory)
counter := new(Counter)
......@@ -53,18 +56,25 @@ func main() {
}
_ = ioutil.WriteFile("output.json", jsonString, 0644)
fmt.Printf("%d directories, %d files\n", counter.directories, counter.files)
} else {
directory = "."
if len(os.Args) > 1 {
format = "2006-01-02"
if len(os.Args) > 2 {
directory = os.Args[1]
filter = os.Args[2]
}
t, err := time.Parse(format, filter)
if err != nil {
ErrorLogger.Print("Format:", format)
ErrorLogger.Print(err)
return
}
counter := new(Counter)
//Start regular recursive tree.
tree("", counter, directory)
tree("", counter, directory, t)
fmt.Printf("%d directories, %d files\n", counter.directories, counter.files)
}
}
{
"Name": ".",
"Name": "tests",
"Files": [
{
"Name": "go.mod"
"Name": "testfile"
},
{
"Name": "handler.go"
"Name": "testfile1"
},
{
"Name": "handler_test.go"
},
{
"Name": "jsonhandler.go"
},
{
"Name": "jsonhandler_test.go"
},
{
"Name": "main.go"
},
{
"Name": "output.json"
},
{
"Name": "tree-linux"
"Name": "testfile2"
}
],
"Folders": {
"tests": {
"Name": "tests",
"testFolder1": {
"Name": "testFolder1",
"Files": [
{
"Name": "testfile"
},
{
"Name": "testfile1"
},
{
"Name": "testfile2"
"Name": "test123"
}
],
"Folders": {
"testFolder1": {
"Name": "testFolder1",
"Files": [
{
"Name": "test123"
}
],
"Folders": {}
},
"testFolder2": {
"Name": "testFolder2",
"Files": [],
"Folders": {}
}
}
"Folders": {}
},
"testFolder2": {
"Name": "testFolder2",
"Files": [],
"Folders": {}
}
}
}
\ No newline at end of file
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment