Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting directory hierarchy using go language

Tags:

tree

go

I'm trying to extract the directory hierarchy of a folder into a datastructure in go language. filepath.Walk seems to be the way to go but all I can do so far is to print the names of files and folders. Here's what I'm using:

func main() {
    visit := func(path string, info os.FileInfo, err error) error {
        if info.IsDir() {
            fmt.Println("dir:  ", path)
        } else {
            fmt.Println("file: ", path)
        }
        return nil
    }

    err := filepath.Walk("./", visit)
    if err != nil {
        log.Fatal(err)
    }
}

this prints the names of folders like:

dir:   folder1
file:  folder1/file1.txt
file:  folder1/file2.txt
file:  folder1/file3.txt
file:  folder1/file4.txt
dir:   folder1/folder2
file:  folder1/folder2/file5.txt
file:  folder1/folder2/file6.txt
file:  folder1/folder2/file7.txt
file:  folder1/folder2/file8.txt
file:  folder1/folder2/file9.txt

for tree structure I thought about using something like:

type File struct {
    Name string
    Content string
}

type Folder struct {
    Name    string
    Files   []File
    Folders []Folder
}

but of course any suggestions are welcomed.

How can I convert this to a tree structure in go? Is there an easier way to do this?

like image 601
none Avatar asked Sep 29 '12 23:09

none


2 Answers

I needed something similar for a little app of mine so I wrote a tiny separate library which is available for your viewing pleasure on Github. Since I needed built-in JSON serialization for the returned os.FileInfo, I added it as well.

I know it comes too late for the original author of this question but posting it here anyway in case someone is looking for something similar. Pull requests readily accepted :)

like image 116
Marcin Wyszynski Avatar answered Nov 09 '22 00:11

Marcin Wyszynski


AFAIK there is nothing ready-made for this in the Go standard lib.

Tree structures lend themselves well to a recursive approach. I defined addFile and addFolder methods on your File and Folder types. Starting with a root folder, you can then call these methods in Walk. If you get a/b/c, we'll be calling root.addFile(a, b, c), a.addFile(b, c), b.addFile(c).

I also changed Folder.Folders to a map, because filepath.Walk always gives us full paths, so we can split those and look up their components in the folder map.

Here is some quick and dirty code that probably has bugs and doesn't do full error checking. It only works for the current directory, but that should be easy to fix.

I also added a String() method on Folder, which is recognized by the compiler and will be used when printing out instances of the type.

package main

import (
    "log"
    "os"
    "path/filepath"
    "strings"
)

type File struct {
    Name string
}

type Folder struct {
    Name    string
    Files   []File
    Folders map[string]*Folder
}

func newFolder(name string) *Folder {
    return &Folder{name, []File{}, make(map[string]*Folder)}
}

func (f *Folder) getFolder(name string) *Folder {
    if nextF, ok := f.Folders[name]; ok {
        return nextF
    } else {
        log.Fatalf("Expected nested folder %v in %v\n", name, f.Name)
    }
    return &Folder{} // cannot happen
}

func (f *Folder) addFolder(path []string) {
    for i, segment := range path {
        if i == len(path)-1 { // last segment == new folder
            f.Folders[segment] = newFolder(segment)
        } else {
            f.getFolder(segment).addFolder(path[1:])
        }
    }
}

func (f *Folder) addFile(path []string) {
    for i, segment := range path {
        if i == len(path)-1 { // last segment == file
            f.Files = append(f.Files, File{segment})
        } else {
            f.getFolder(segment).addFile(path[1:])
            return
        }
    }
}

func (f *Folder) String() string {
    var str string
    for _, file := range f.Files {
        str += f.Name + string(filepath.Separator) + file.Name + "\n"
    }
    for _, folder := range f.Folders {
        str += folder.String()
    }
    return str
}

func main() {
    startPath := "."
    rootFolder := newFolder(startPath)

    visit := func(path string, info os.FileInfo, err error) error {
        segments := strings.Split(path, string(filepath.Separator))
        if info.IsDir() {
            if path != startPath {
                rootFolder.addFolder(segments)
            }
        } else {
            rootFolder.addFile(segments)
        }
        return nil
    }

    err := filepath.Walk(startPath, visit)
    if err != nil {
        log.Fatal(err)
    }

    log.Printf("%v\n", rootFolder)
}
like image 33
Thomas Kappler Avatar answered Nov 09 '22 00:11

Thomas Kappler