-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.go
More file actions
108 lines (89 loc) · 2.48 KB
/
data.go
File metadata and controls
108 lines (89 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package data
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
// ProjectData is root object collects whole information about this project and
// used for TOC generation as well as HTML version build.
type ProjectData struct {
Name string // Project title
Root *Node // Root fot parsed data
ProjectRootFolder string // path to data folder
}
// FileInfo is a struct created from os.FileInfo interface for serialization.
type FileInfo struct {
Name string `json:"name"`
Size int64 `json:"size"`
Mode os.FileMode `json:"mode"`
ModTime time.Time `json:"mod_time"`
IsDir bool `json:"is_dir"`
}
// Node represents a node in a directory tree.
type Node struct {
FullPath string `json:"path"`
LocalPath string `json:"local_path"`
Info *FileInfo `json:"info"`
Children []*Node `json:"children"`
IsDir bool `json:"is_dir"`
Parent *Node `json:"-"`
}
// New is basic factory that build ProjectData object
func New() ProjectData {
project := ProjectData{
Name: "Go4Rails",
ProjectRootFolder: projectRrootDir(),
}
return project
}
func projectRrootDir() string {
pwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return pwd
}
// Helper function to create a local FileInfo struct from os.FileInfo interface.
func fileInfoFromInterface(v os.FileInfo) *FileInfo {
return &FileInfo{v.Name(), v.Size(), v.Mode(), v.ModTime(), v.IsDir()}
}
// NewTree Create directory hierarchy tree.
func newTree(root string) *Node {
parents := make(map[string]*Node)
// walker function
walkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
parents[path] = &Node{
FullPath: path,
LocalPath: strings.TrimLeft(path, root),
Info: fileInfoFromInterface(info),
Children: make([]*Node, 0),
IsDir: info.IsDir(),
}
return nil
}
filepath.Walk(root+"/data", walkFunc)
var result *Node
for path, node := range parents {
parentPath := filepath.Dir(path)
parent, exists := parents[parentPath]
// If a parent does not exist, this is the root.
if !exists {
result = node
} else {
node.Parent = parent
parent.Children = append(parent.Children, node)
}
}
return result
}
// LoadData will go through data dir and load all examples, parse Ruby & Go files
// Add Sections and Articles into ProjectData object for future use.
func (p *ProjectData) LoadData() {
p.Root = newTree(p.ProjectRootFolder)
}