/
walking.go
148 lines (138 loc) · 4.24 KB
/
walking.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package portfoliodb
import (
"io/ioutil"
"os"
"path"
)
// ProjectTreeElement represents a project
type ProjectTreeElement struct {
ID string
DescriptionRaw string
MediaFilepaths []string
ScatteredMode bool // Whether the build was run with --scattered
}
// GetProjectPath returns the project's folder path with regard to databaseDirectory
func (p *ProjectTreeElement) GetProjectPath(databaseDirectory string) string {
if p.ScatteredMode {
return path.Join(databaseDirectory, p.ID, ".portfoliodb")
}
return path.Join(databaseDirectory, p.ID)
}
// MediaAbsoluteFilepaths is like MediaFilepaths but returns absolute paths with regard to databaseDirectory
func (p *ProjectTreeElement) MediaAbsoluteFilepaths(databaseDirectory string) []string {
absoluted := make([]string, len(p.MediaFilepaths))
for _, item := range p.MediaFilepaths {
absoluted = append(absoluted, path.Join(p.GetProjectPath(databaseDirectory), item))
}
return absoluted
}
// BuildProjectsTree scans databaseDirectory to return a slice of ProjectTreeElement's, gathering media files and other various information
func BuildProjectsTree(databaseDirectory string) ([]ProjectTreeElement, error) {
var tree []ProjectTreeElement
files, err := ioutil.ReadDir(databaseDirectory)
if err != nil {
return nil, err
}
for _, projectFolder := range files {
// If it's not a directory, it's not a project folder
// so it has nothing to do with this
if !projectFolder.IsDir() {
continue
}
projectFolderPath := path.Join(databaseDirectory, projectFolder.Name())
// Read the description.md file
// If description is empty, then the project is not portfoliodb-enabled.
// See ReadDescriptionFile for more info on why
descriptionRaw, err := ReadDescriptionFile(projectFolderPath)
if err != nil {
return nil, err
}
// Build the list of media filepaths
mediaFilepaths, err := buildMediaFilepaths(projectFolderPath)
if err != nil {
return nil, err
}
// Append the new project
tree = append(tree, ProjectTreeElement{
ID: projectFolder.Name(),
DescriptionRaw: descriptionRaw,
MediaFilepaths: mediaFilepaths,
})
}
return tree, nil
}
func BuildProjectsTreeScatteredMode(projectsDirectory string) ([]ProjectTreeElement, error) {
var tree []ProjectTreeElement
files, err := ioutil.ReadDir(projectsDirectory)
if err != nil {
return nil, err
}
for _, projectFolder := range files {
// Not a project folder
if !projectFolder.IsDir() {
continue
}
portfoliodbDirPath := path.Join(projectsDirectory, projectFolder.Name(), ".portfoliodb")
portfoliodbDir, err := os.Stat(portfoliodbDirPath)
// Project has no .portfoliodb file/folder
if err != nil {
continue
}
// .portfoliodb is not a folder
if !portfoliodbDir.IsDir() {
continue
}
// Read the description.md file
// If description is empty, then the project is not portfoliodb-enabled.
// See ReadDescriptionFile for more info on why
descriptionRaw, err := ReadDescriptionFile(portfoliodbDirPath)
if err != nil {
return nil, err
}
if descriptionRaw == "" {
continue
}
// Build the lest of media filepaths
mediaFilepaths, err := buildMediaFilepaths(portfoliodbDirPath)
if err != nil {
return nil, err
}
// Append the new project
tree = append(tree, ProjectTreeElement{
ID: projectFolder.Name(),
DescriptionRaw: descriptionRaw,
MediaFilepaths: mediaFilepaths,
})
}
return tree, nil
}
// ReadDescriptionFile reads the description.md file in directory.
// Returns an empty string if the file is a directory or does not exist.
func ReadDescriptionFile(directory string) (string, error) {
descriptionFilepath := path.Join(directory, "description.md")
if !FileExists(descriptionFilepath) {
return "", nil
}
descriptionFile, err := os.Stat(descriptionFilepath)
if err != nil {
return "", err
}
if descriptionFile.IsDir() {
return "", nil
}
return ReadFile(descriptionFilepath)
}
func buildMediaFilepaths(at string) ([]string, error) {
mediaFiles, err := ioutil.ReadDir(at)
var mediaFilepaths []string
if err != nil {
return nil, err
}
for _, mediaFile := range mediaFiles {
if mediaFile.Name() == "description.md" {
continue
}
mediaFilepaths = append(mediaFilepaths, mediaFile.Name())
}
return mediaFilepaths, nil
}