/
main.go
313 lines (250 loc) · 8.11 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// inout creates compressed backup files and can restore a flaarum project from the said files.
package main
import (
"fmt"
"os"
"io/ioutil"
"time"
"path/filepath"
"compress/gzip"
"github.com/otiai10/copy"
"github.com/bankole7782/mof"
"github.com/bankole7782/flaarum/flaarum_shared"
"github.com/gookit/color"
"encoding/json"
"sync"
"strings"
"strconv"
"github.com/pkg/errors"
)
func main() {
if len(os.Args) < 2 {
color.Red.Println("Wrong number of inputs. Run this program with --help to view the help message.")
os.Exit(1)
}
switch os.Args[1] {
case "help", "h", "--help":
fmt.Println(`flaarum's inout program backs up and restores a flaarum project. It doesn't add the indexes to a backup
thereby compressing it. This is because the index can be created from the data.
Available Commands:
out Out command creates a backup of a flaarum project. It expects the flaarum project as its only argument.
in In command restores a backup of a flaarum project. It expects the name of the flaarum project to be
restored into and a file containing the backup data. This file must reside in the directory gotten from
"flaarum.cli pwd"
`)
case "out":
if len(os.Args) < 3 {
color.Red.Println("Expecting the name of the project as the last argument.")
os.Exit(1)
}
projName := os.Args[2]
dataPath, err := flaarum_shared.GetDataPath()
if err != nil {
panic(err)
}
tmpFolderName := fmt.Sprintf(".flaarumout-%s", flaarum_shared.UntestedRandomString(10))
tmpFolder, err := flaarum_shared.GetFlaarumPath(tmpFolderName)
if err != nil {
panic(err)
}
err = os.MkdirAll(tmpFolder, 0777)
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpFolder)
projPath := filepath.Join(dataPath, projName)
tblFIs, err := ioutil.ReadDir(projPath)
for _, tblFI := range tblFIs {
if projName == "first_proj" && tblFI.Name() == "server_stats" {
continue
}
err := copy.Copy(filepath.Join(projPath, tblFI.Name(), "structures"), filepath.Join(tmpFolder, "out", tblFI.Name(), "structures"))
if err != nil {
panic(err)
}
err = copy.Copy(filepath.Join(projPath, tblFI.Name(), "data"), filepath.Join(tmpFolder, "out", tblFI.Name(), "data"))
if err != nil {
panic(err)
}
_ = copy.Copy(filepath.Join(projPath, tblFI.Name(), "lastId"), filepath.Join(tmpFolder, "out", tblFI.Name(), "lastId"))
}
err = mof.MOF(filepath.Join(tmpFolder, "out"), filepath.Join(tmpFolder, "out.mof"))
if err != nil {
panic(err)
}
outFileName := fmt.Sprintf("flaarumout-%s.%s", time.Now().Format("20060102T1504"), flaarum_shared.BACKUP_EXT)
outFilePath, err := flaarum_shared.GetFlaarumPath(outFileName)
if err != nil {
panic(err)
}
outFile, err := os.Create(outFilePath)
if err != nil {
panic(err)
}
defer outFile.Close()
zw := gzip.NewWriter(outFile)
zw.Name = outFileName
zw.Comment = "backup output from flaarum"
zw.ModTime = time.Now()
mofBytes, err := ioutil.ReadFile(filepath.Join(tmpFolder, "out.mof"))
if err != nil {
panic(err)
}
_, err = zw.Write(mofBytes)
if err != nil {
panic(err)
}
if err := zw.Close(); err != nil {
panic(err)
}
fmt.Printf(outFilePath)
case "in":
if len(os.Args) < 4 {
color.Red.Println("Expecting the project name and the input file name.")
os.Exit(1)
}
projName := os.Args[2]
inputPath, err := flaarum_shared.GetFlaarumPath(os.Args[3])
if err != nil {
panic(err)
}
if filepath.Ext(os.Args[3]) != "." + flaarum_shared.BACKUP_EXT {
color.Red.Printf("This file '%s' was not generated by flaarum inout.\n", os.Args[3])
os.Exit(1)
}
if ! flaarum_shared.DoesPathExists(inputPath) {
color.Red.Printf("The file '%s' does not exists in the flaarum working directory.\n", os.Args[3])
os.Exit(1)
}
dataPath, err := flaarum_shared.GetDataPath()
if err != nil {
panic(err)
}
tmpFolderName := fmt.Sprintf(".flaarumin-%s", flaarum_shared.UntestedRandomString(10))
tmpFolder, err := flaarum_shared.GetFlaarumPath(tmpFolderName)
if err != nil {
panic(err)
}
err = os.MkdirAll(tmpFolder, 0777)
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpFolder)
inputFile, err := os.Open(inputPath)
if err != nil {
panic(err)
}
defer inputFile.Close()
zr, err := gzip.NewReader(inputFile)
if err != nil {
panic(err)
}
mofBytes, err := ioutil.ReadAll(zr)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(filepath.Join(tmpFolder, "out.mof"), mofBytes, 0777)
if err != nil {
panic(err)
}
err = mof.UndoMOF(filepath.Join(tmpFolder, "out.mof"), tmpFolder)
if err != nil {
panic(err)
}
tblFIs, err := ioutil.ReadDir(filepath.Join(tmpFolder, "out"))
if err != nil {
panic(err)
}
var wg sync.WaitGroup
for _, tblFI := range tblFIs {
wg.Add(1)
go func(tblFI os.FileInfo, wg *sync.WaitGroup) {
defer wg.Done()
err = copy.Copy(filepath.Join(tmpFolder, "out", tblFI.Name(), "structures"), filepath.Join(dataPath, projName, tblFI.Name(), "structures"))
if err != nil {
panic(err)
}
err = copy.Copy(filepath.Join(tmpFolder, "out", tblFI.Name(), "data"), filepath.Join(dataPath, projName, tblFI.Name(), "data"))
if err != nil {
os.MkdirAll(filepath.Join(dataPath, projName, tblFI.Name(), "data"), 0777)
}
_ = copy.Copy(filepath.Join(tmpFolder, "out", tblFI.Name(), "lastId"), filepath.Join(dataPath, projName, tblFI.Name(), "lastId"))
toMakePath := filepath.Join(dataPath, projName, tblFI.Name(), "indexes")
err = os.MkdirAll(toMakePath, 0777)
if err != nil {
panic(err)
}
toMakePath = filepath.Join(dataPath, projName, tblFI.Name(), "txtinstrs")
err = os.MkdirAll(toMakePath, 0777)
if err != nil {
panic(err)
}
rowFIs, err := ioutil.ReadDir(filepath.Join(dataPath, projName, tblFI.Name(), "data"))
if err != nil {
panic(err)
}
ts, err := flaarum_shared.GetTableStructureParsed(projName, tblFI.Name(), 1)
if err != nil {
panic(err)
}
for _, rowFI := range rowFIs {
rowMap := make(map[string]string)
rowBytes, err := ioutil.ReadFile(filepath.Join(dataPath, projName, tblFI.Name(), "data", rowFI.Name()))
if err != nil {
panic(err)
}
err = json.Unmarshal(rowBytes, &rowMap)
if err != nil {
panic(err)
}
if ts.TableType == "proper" {
// create indexes
for k, v := range rowMap {
if k == "id" {
continue
}
if isFieldExemptedFromIndexingVersioned(projName, tblFI.Name(), k, rowMap["_version"]) {
// create a .text file which is a message to the tindexer program.
newTextFileName := rowFI.Name() + flaarum_shared.TEXT_INTR_DELIM + k + ".text"
err = ioutil.WriteFile(filepath.Join(dataPath, projName, tblFI.Name(), "txtinstrs", newTextFileName), []byte(v), 0777)
if err != nil {
fmt.Printf("%+v\n", errors.Wrap(err, "ioutil error."))
}
} else if flaarum_shared.IsNotIndexedFieldVersioned(projName, tblFI.Name(), k, rowMap["_version"]) {
// don't create indexes
} else {
err := flaarum_shared.MakeIndex(projName, tblFI.Name(), k, v, rowFI.Name())
if err != nil {
panic(err)
}
}
}
} else {
for k, v := range rowMap {
if k == "created" || strings.HasPrefix(k, "created_") || k == "_version" {
err = flaarum_shared.MakeIndex(projName, tblFI.Name(), k, v, rowFI.Name())
if err != nil {
fmt.Printf("%+v\n", err)
}
}
}
}
}
}(tblFI, &wg)
}
wg.Wait()
default:
color.Red.Println("Unexpected command. Run this program 'inout' with --help to find out the supported commands.")
os.Exit(1)
}
}
func isFieldExemptedFromIndexingVersioned(projName, tableName, fieldName, version string) bool {
versionNum, _ := strconv.Atoi(version)
td, _ := flaarum_shared.GetTableStructureParsed(projName, tableName, versionNum)
for _, fd := range td.Fields {
if fd.FieldName == fieldName && fd.FieldType == "text" {
return true
}
}
return false
}