diff --git a/handler_test.go b/handler_test.go index 9648366780db3c6b5fee59efebf21af3cc33e8b6..490c96245878086a80178ff3ca1ca41bd252c6c7 100644 --- a/handler_test.go +++ b/handler_test.go @@ -56,5 +56,14 @@ func TestOpenDirectory(t *testing.T) { assertEqual(t, len(result), 5, "") assertEqual(t, result[0], "testFolder1", "") assertEqual(t, result[1], "testFolder2", "") +} +/* +Test if the tree function works by observing the counter. +*/ +func TestTree(t *testing.T) { + counter := new(Counter) + tree("", counter, ".") + assertEqual(t, counter.directories, 3, "") + assertEqual(t, counter.files, 12, "") } diff --git a/jsonhandler.go b/jsonhandler.go new file mode 100644 index 0000000000000000000000000000000000000000..807e859a91b4c665eb3374401d2f162d4b8b4e43 --- /dev/null +++ b/jsonhandler.go @@ -0,0 +1,38 @@ +package main + +import ( + "os" + "path" + "strings" +) + +//Recursive function called on each directory. The function checks if the +//current directory is Folder. If so each directory gets recursive call and +//gets appended to the Folder structure. If not the files are appended. +func jsontree(output *Folder, counter *Counter, base string) { + dirnames := opendirectory(base) + + for _, name := range dirnames { + //Exclude hiddenn folders + if name[0] == '.' { + continue + } + + next := path.Join(base, name) + counter.isDir(next) + + stat, _ := os.Stat(next) + if stat.IsDir() { + for i, x := range strings.Split(next, "/") { + if i == len(strings.Split(next, "/"))-1 { + output.Folders[x] = newFolder(x) + jsontree(output.Folders[x], counter, next) + } + } + } else { + output.Files = append(output.Files, File{Name: name}) + jsontree(output, counter, next) + } + + } +} diff --git a/jsonhandler_test.go b/jsonhandler_test.go new file mode 100644 index 0000000000000000000000000000000000000000..ff3f20561094aa9fd3ee5ba8d81670da3eca5b4c --- /dev/null +++ b/jsonhandler_test.go @@ -0,0 +1,35 @@ +package main + +import ( + "testing" +) + +/* +Test if new folders are created. +*/ +func TestNewFolder(t *testing.T) { + folder := newFolder("test") + folder.Folders["test"] = newFolder("test1") + assertEqual(t, folder.Name, "test", "") + assertEqual(t, folder.Folders["test"].Name, "test1", "") +} + +/* +Thoroughly testing JSON tree function. +*/ +func TestJsonTree(t *testing.T) { + folder := newFolder(".") + counter := new(Counter) + + jsontree(folder, counter, ".") + + assertEqual(t, folder.Name, ".", "") + + assertEqual(t, len(folder.Folders), 1, "") + assertEqual(t, len(folder.Files), 8, "") + + assertEqual(t, len(folder.Folders["tests"].Folders), 2, "") + assertEqual(t, len(folder.Folders["tests"].Files), 3, "") + + assertEqual(t, len(folder.Folders["tests"].Folders["testFolder1"].Files), 1, "") +} diff --git a/main.go b/main.go index 6633c54259558a23cebef22db91a042ee86210ab..d95b8d1b224cdb7423a79a70da48749b6c07cebc 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,31 @@ package main import ( + "encoding/json" + "flag" "fmt" + "io/ioutil" "log" "os" ) +//File structure representation +type File struct { + Name string +} + +//Folder structure representation +type Folder struct { + Name string + Files []File + Folders map[string]*Folder +} + +// New folder creation for Folder struction +func newFolder(name string) *Folder { + return &Folder{name, []File{}, make(map[string]*Folder)} +} + //Declaring all the variables needed var ( ErrorLogger *log.Logger @@ -14,13 +34,37 @@ var ( func main() { ErrorLogger = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) - directory = "." - if len(os.Args) > 1 { - directory = os.Args[1] + //Parse the flag and check if present. + boolPtr := flag.Bool("j", false, "Output as JSON file") + flag.Parse() + if *boolPtr { + //Start JSON routine. + directory = "." + if len(os.Args) > 2 { + directory = os.Args[1] + } + output := newFolder(directory) + counter := new(Counter) + //Start recursive json tree. + jsontree(output, counter, directory) + jsonString, err := json.MarshalIndent(output, "", " ") + if err != nil { + ErrorLogger.Fatal(err) + } + _ = ioutil.WriteFile("output.json", jsonString, 0644) + fmt.Printf("%d directories, %d files\n", counter.directories, counter.files) + + } else { + + directory = "." + if len(os.Args) > 1 { + directory = os.Args[1] + } + counter := new(Counter) + //Start regular recursive tree. + tree("", counter, directory) + fmt.Printf("%d directories, %d files\n", counter.directories, counter.files) + } - counter := new(Counter) - fmt.Println(directory) - //Start recursive tree. - tree("", counter, directory) - fmt.Printf("%d directories, %d files\n", counter.directories, counter.files) + } diff --git a/output.json b/output.json new file mode 100644 index 0000000000000000000000000000000000000000..3a4f32ae5926ba7ac2051236f4d7b2eefad56edc --- /dev/null +++ b/output.json @@ -0,0 +1,61 @@ +{ + "Name": ".", + "Files": [ + { + "Name": "go.mod" + }, + { + "Name": "handler.go" + }, + { + "Name": "handler_test.go" + }, + { + "Name": "jsonhandler.go" + }, + { + "Name": "jsonhandler_test.go" + }, + { + "Name": "main.go" + }, + { + "Name": "output.json" + }, + { + "Name": "tree-linux" + } + ], + "Folders": { + "tests": { + "Name": "tests", + "Files": [ + { + "Name": "testfile" + }, + { + "Name": "testfile1" + }, + { + "Name": "testfile2" + } + ], + "Folders": { + "testFolder1": { + "Name": "testFolder1", + "Files": [ + { + "Name": "test123" + } + ], + "Folders": {} + }, + "testFolder2": { + "Name": "testFolder2", + "Files": [], + "Folders": {} + } + } + } + } +} \ No newline at end of file diff --git a/tests/testFolder1/test123 b/tests/testFolder1/test123 new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tree-linux b/tree-linux index a4cfd84d635e981d74bffbcef627c726f37f0c45..73457ffc8dc31d3f68bcf3c6d70cadcc76fe997a 100755 Binary files a/tree-linux and b/tree-linux differ