-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_handler.go
81 lines (72 loc) · 1.73 KB
/
json_handler.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
package utils
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/StevenRojas/goaccess/pkg/entities"
)
// JSONHandler interface
type JSONHandler interface {
Modules() ([]entities.ModuleInit, error)
}
type jsonHandler struct {
folder string
}
// NewJSONHandler json handler instance
func NewJSONHandler(folderPath string) JSONHandler {
return &jsonHandler{
folder: folderPath,
}
}
// Modules read json files from init folder and return a list of modules
func (jh *jsonHandler) Modules() ([]entities.ModuleInit, error) {
var files []string
err := filepath.Walk(jh.folder+"/modules", collect(&files))
if err != nil {
return nil, err
}
modules := []entities.ModuleInit{}
for _, file := range files {
content, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
module := entities.ModuleInit{}
err = json.Unmarshal([]byte(content), &module)
if err != nil {
return nil, err
}
for si, submodule := range module.SubModules {
sections := make(map[string]bool)
for _, section := range submodule.SectionList {
sections[section] = false
}
submodule.Sections = sections
submodule.SectionList = nil
actions := make(map[string]entities.Action)
for a, title := range submodule.ActionList {
actions[a] = entities.Action{
Title: title,
Allowed: false,
}
}
submodule.Actions = actions
submodule.ActionList = nil
module.SubModules[si] = submodule
}
modules = append(modules, module)
}
return modules, nil
}
func collect(files *[]string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) == ".json" {
*files = append(*files, path)
}
return nil
}
}