forked from Edru2/DeMuddler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptFunctions.go
51 lines (45 loc) · 1.19 KB
/
ScriptFunctions.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
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
)
func handleScripts(scripts *[]Script, parentDir string) {
if len(*scripts) == 0 {
return
}
var jsonFile []Script
if parentDir != "" {
if err := os.MkdirAll(parentDir, 0755); err != nil {
panic(err)
}
}
for _, script := range *scripts {
scriptFileName := strings.ReplaceAll(script.Name, " ", "_")
scriptFilePath := filepath.Join(parentDir, scriptFileName+".lua")
if len(script.Script) > 0 && !containsIllegalCharacters(scriptFileName) {
if err := os.WriteFile(scriptFilePath, []byte(script.Script), 0644); err != nil {
panic(err)
}
script.Script = ""
}
jsonFile = append(jsonFile, script)
}
jsonFilePath := filepath.Join(parentDir, "scripts.json")
jsonData, err := json.MarshalIndent(jsonFile, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(jsonFilePath, jsonData, 0644)
if err != nil {
panic(err)
}
}
func handleScriptGroups(groups *[]ScriptGroup, baseDir string) {
for i := range *groups {
groupPath := filepath.Join(baseDir, (*groups)[i].Name)
handleScripts(&((*groups)[i].Scripts), groupPath)
handleScriptGroups(&((*groups)[i].ScriptGroup), groupPath)
}
}