-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
93 lines (74 loc) · 1.9 KB
/
config.go
File metadata and controls
93 lines (74 loc) · 1.9 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
)
// copies user config template to `configFile`, and sets permissions
func C_Create(configFile string, uid uint32, gid uint32) error {
log.Println("C_Create: Creating new configuration at", configFile+".")
// copy the defaults
defaultConfig, err := G_Frontend.ReadFile("frontend-dist/assets/defaultconfig.json")
if err != nil {
return err
}
if err := os.WriteFile(configFile, defaultConfig, 0664); err != nil {
return err
}
// set permissions
if err := os.Chmod(configFile, 0600); err != nil {
return err
}
if err := os.Chown(configFile, int(uid), int(gid)); err != nil {
return err
}
return nil
}
// sets a user config value
func C_SetValue(userId string, key string, val any) error {
A_SessionsMutex.Lock()
defer A_SessionsMutex.Unlock()
u, ok := A_Sessions[userId]
if !ok {
return fmt.Errorf("user not found: %s", userId)
}
return IT_Set(u.config, key, val)
}
// saves user config
func C_Save(userId string) error {
A_SessionsMutex.RLock()
defer A_SessionsMutex.RUnlock()
u, ok := A_Sessions[userId]
if !ok {
return fmt.Errorf("user not found: %s", userId)
}
data, err := json.MarshalIndent(u.config, "", " ")
if err != nil {
return fmt.Errorf("config serialization error: %s", err)
}
if err := os.WriteFile(u.configFile, data, 0600); err != nil {
return fmt.Errorf("config write error: %s", err)
}
return nil
}
// handles config.set
func Comm_ConfigSet(data Comm_Message, keyCookie string) (any, error) {
// get data
keys, ok := data.Data.(map[string]any)
if !ok {
return nil, fmt.Errorf("data doesn't exist or isn't an object")
}
// loop through and set value
for key, value := range keys {
if err := C_SetValue(keyCookie, key, value); err != nil {
return nil, err
}
}
// save json
if err := C_Save(keyCookie); err != nil {
return nil, err
}
// return success
return map[string]any{}, nil
}