export File and Tree fields

This commit is contained in:
Arnas Udovicius 2021-05-12 08:17:43 +03:00
parent b75c11b5ea
commit a9e6c702b9
2 changed files with 35 additions and 31 deletions

40
tree.go
View file

@ -14,18 +14,18 @@ import (
) )
type File struct { type File struct {
id string Id string
name string Name string
fullPath string FullPath string
category []string Category []string
tags []string Tags []string
meta map[string]string Meta map[string]string
} }
type Tree struct { type Tree struct {
path string Path string
dirs []Tree Dirs []Tree
files []File Files []File
} }
func BuildTree(dirPath string, meta []string) (Tree, error) { func BuildTree(dirPath string, meta []string) (Tree, error) {
@ -194,7 +194,7 @@ func getMaxId(dir string) (int64, error) {
func readPath(dirPath string, category []string, meta []string) (Tree, error) { func readPath(dirPath string, category []string, meta []string) (Tree, error) {
tree := Tree{} tree := Tree{}
tree.path = dirPath tree.Path = dirPath
files, err := ioutil.ReadDir(dirPath) files, err := ioutil.ReadDir(dirPath)
if err != nil { if err != nil {
@ -208,7 +208,7 @@ func readPath(dirPath string, category []string, meta []string) (Tree, error) {
if err != nil { if err != nil {
return tree, err return tree, err
} }
tree.dirs = append(tree.dirs, nextDir) tree.Dirs = append(tree.Dirs, nextDir)
continue continue
} }
@ -220,7 +220,7 @@ func readPath(dirPath string, category []string, meta []string) (Tree, error) {
if err != nil { if err != nil {
return tree, err return tree, err
} }
tree.files = append(tree.files, nextFile) tree.Files = append(tree.Files, nextFile)
} }
return tree, nil return tree, nil
@ -228,10 +228,10 @@ func readPath(dirPath string, category []string, meta []string) (Tree, error) {
func readFile(file fs.FileInfo, fullPath string, category []string, meta []string) (File, error) { func readFile(file fs.FileInfo, fullPath string, category []string, meta []string) (File, error) {
f := File{ f := File{
name: file.Name(), Name: file.Name(),
fullPath: fullPath, FullPath: fullPath,
category: category, Category: category,
meta: map[string]string{}, Meta: map[string]string{},
} }
osf, err := os.Open(fullPath) osf, err := os.Open(fullPath)
@ -254,12 +254,16 @@ func readFile(file fs.FileInfo, fullPath string, category []string, meta []strin
tags = append(tags, strings.Trim(tag, " ")) tags = append(tags, strings.Trim(tag, " "))
} }
f.tags = tags f.Tags = tags
}
if strings.HasPrefix(line, "id:") {
line = strings.TrimPrefix(line, "id:")
f.Id = strings.Trim(line, " ")
} }
for _, option := range meta { for _, option := range meta {
if strings.HasPrefix(line, option) { if strings.HasPrefix(line, option) {
line = strings.TrimPrefix(line, option + ":") line = strings.TrimPrefix(line, option + ":")
f.meta[option] = strings.Trim(line, " ") f.Meta[option] = strings.Trim(line, " ")
} }
} }
} }

View file

@ -28,21 +28,21 @@ func TestSunny(t *testing.T) {
t.Errorf("Got error: %v", err) t.Errorf("Got error: %v", err)
} }
if len(tree.dirs) != 1 { if len(tree.Dirs) != 1 {
t.Errorf("Should be 1 subdirectory, got %v", len(tree.dirs)) t.Errorf("Should be 1 subdirectory, got %v", len(tree.Dirs))
} }
if tree.dirs[0].files[0].name != "file3.md" { if tree.Dirs[0].Files[0].Name != "file3.md" {
t.Errorf("File should be 'file3.md', got %v", tree.dirs[0].files[0].name) t.Errorf("File should be 'file3.md', got %v", tree.Dirs[0].Files[0].Name)
} }
if tree.files[0].name != "file1.md" { if tree.Files[0].Name != "file1.md" {
t.Errorf("File should be 'file1.md', got %v", tree.files[0].name) t.Errorf("File should be 'file1.md', got %v", tree.Files[0].Name)
} }
if len(tree.files[0].tags) != 2 { if len(tree.Files[0].Tags) != 2 {
t.Errorf("File 'file1.md' should have 2 tags, got %v", len(tree.files[0].tags)) t.Errorf("File 'file1.md' should have 2 tags, got %v", len(tree.Files[0].Tags))
} }
if tree.files[0].tags[1] != "t2" { if tree.Files[0].Tags[1] != "t2" {
t.Errorf("File 'file1.md' 2nd tag 't2', got %v", tree.files[0].tags[1]) t.Errorf("File 'file1.md' 2nd tag 't2', got %v", tree.Files[0].Tags[1])
} }
t.Log(tree) t.Log(tree)
} }
@ -62,11 +62,11 @@ func TestMeta(t *testing.T) {
t.Errorf("Got error: %v", err) t.Errorf("Got error: %v", err)
} }
if tree.files[0].name != "file1.md" { if tree.Files[0].Name != "file1.md" {
t.Errorf("File should be 'file1.md', got %v", tree.files[0].name) t.Errorf("File should be 'file1.md', got %v", tree.Files[0].Name)
} }
if tree.files[0].meta["option1"] != "test option" { if tree.Files[0].Meta["option1"] != "test option" {
t.Error("File 'file1.md' should have 'option1' with value 'test option'") t.Error("File 'file1.md' should have 'option1' with value 'test option'")
} }
} }