-
Notifications
You must be signed in to change notification settings - Fork 28
/
initializer.go
175 lines (156 loc) · 4.02 KB
/
initializer.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"github.com/satori/go.uuid"
"github.com/traPtitech/traQ/model"
"github.com/traPtitech/traQ/repository"
"github.com/traPtitech/traQ/utils/validator"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"os"
"path"
)
type dataRoot struct {
Tags map[string]*dataTag `yaml:"tags"`
Channels map[string]*dataChannel `yaml:"channels"`
Stamps map[string]*dataStamp `yaml:"stamps"`
}
type dataTag struct {
Restricted bool `yaml:"restricted"`
Type string `yaml:"type" validate:"max=30"`
}
type dataStamp struct {
File string `yaml:"file" validate:"required"`
}
type dataChannel struct {
Topic string `yaml:"topic"`
Force bool `yaml:"force"`
Children map[string]*dataChannel `yaml:"children"`
}
func insertInitialData(repo repository.Repository, initDataDir string, data *dataRoot) error {
if err := createTags(repo, data.Tags); err != nil {
return err
}
if err := createStamps(repo, initDataDir, data.Stamps); err != nil {
return err
}
if err := createChannels(repo, data.Channels); err != nil {
return err
}
return nil
}
func createTags(repo repository.Repository, tags map[string]*dataTag) error {
for name, options := range tags {
if err := validator.ValidateVar(name, "max=30"); err != nil {
return err
}
if err := validator.ValidateStruct(options); err != nil {
return err
}
if _, err := repo.CreateTag(name, options.Restricted, options.Type); err != nil {
return err
}
}
return nil
}
func createStamps(repo repository.Repository, initDataDir string, stamps map[string]*dataStamp) error {
for name, data := range stamps {
if err := validator.ValidateVar(name, "name"); err != nil {
return err
}
if err := validator.ValidateStruct(data); err != nil {
return err
}
filepath := path.Join(initDataDir, data.File)
_, filename := path.Split(filepath)
f, err := os.Open(filepath)
if err != nil {
return err
}
stat, _ := f.Stat()
meta, err := repo.SaveFile(filename, f, stat.Size(), "", model.FileTypeStamp, uuid.Nil)
f.Close()
if err != nil {
return err
}
if _, err := repo.CreateStamp(name, meta.ID, uuid.Nil); err != nil {
return err
}
}
return nil
}
func createChannels(repo repository.Repository, channels map[string]*dataChannel) error {
for name, data := range channels {
_, err := channelTreeTraverse(repo, name, data, nil)
if err != nil {
return err
}
}
return nil
}
func channelTreeTraverse(repo repository.Repository, name string, node *dataChannel, parent *model.Channel) (*model.Channel, error) {
if err := validator.ValidateVar(name, "name"); err != nil {
return nil, err
}
if err := validator.ValidateStruct(node); err != nil {
return nil, err
}
parentID := uuid.Nil
if parent != nil {
parentID = parent.ID
}
ch, err := repo.CreatePublicChannel(name, parentID, uuid.Nil)
if err != nil {
return nil, err
}
if err := repo.UpdateChannelTopic(ch.ID, node.Topic, uuid.Nil); err != nil {
return nil, err
}
if err := repo.UpdateChannelAttributes(ch.ID, nil, &node.Force); err != nil {
return nil, err
}
for k, v := range node.Children {
if v != nil {
_, err := channelTreeTraverse(repo, k, v, ch)
if err != nil {
return nil, err
}
}
}
return ch, nil
}
func unmarshalInitData(r io.Reader) (*dataRoot, error) {
data := dataRoot{}
if err := yaml.NewDecoder(r).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}
func initData(repo repository.Repository, initDataDir string) error {
if stat, err := os.Stat(initDataDir); err != nil {
return nil
} else if !stat.IsDir() {
return nil
}
files, err := ioutil.ReadDir(initDataDir)
if err != nil {
return err
}
for _, f := range files {
if f.Mode().IsRegular() && path.Ext(f.Name()) == ".yml" {
is, err := os.Open(path.Join(initDataDir, f.Name()))
if err != nil {
return err
}
data, err := unmarshalInitData(is)
is.Close()
if err != nil {
return err
}
if err := insertInitialData(repo, initDataDir, data); err != nil {
return err
}
}
}
return nil
}