-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_structures.go
84 lines (74 loc) · 1.81 KB
/
data_structures.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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
type Namespace struct {
Mnemonic string
Name string
}
type Article struct {
File string `json:"file"`
Title string `json:"title"`
Children []Article `json:"children"`
}
type NamespaceFile struct {
Description string `json:"description"`
Name string `json:"name"`
Mnemonic string `json:"mnemonic"`
Entries []struct {
File string `json:"file"`
Title string `json:"title"`
Children []Article `json:"children"`
} `json:"entries"`
}
func retrieveAvailableNamespaces() []Namespace {
namespaces := make([]Namespace, 0)
err := filepath.Walk(*datadir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
namespaceData := NamespaceFile{}
f, e := os.ReadFile(filepath.Join(path, "namespace.json"))
if e != nil {
return nil
}
e = json.Unmarshal(f, &namespaceData)
if e != nil {
panic(e)
}
namespace := Namespace{Mnemonic: namespaceData.Mnemonic, Name: namespaceData.Name}
namespace.Name = fmt.Sprintf("%s", namespaceData.Name)
namespaces = append(namespaces, namespace)
}
return nil
})
if err != nil {
panic(err)
}
return namespaces
}
func getArticles(namespace string) []Article {
articles := make([]Article, 0)
namespaceData := NamespaceFile{}
err := filepath.Walk(filepath.Join(*datadir, namespace), func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
f, e := os.ReadFile(filepath.Join(path, "namespace.json"))
if e != nil {
return nil
}
e = json.Unmarshal(f, &namespaceData)
if e != nil {
panic(e)
}
}
return nil
})
if err != nil {
panic(err)
}
for _, elem := range namespaceData.Entries {
articles = append(articles, Article{File: elem.File, Title: elem.Title})
}
return articles
}