-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconfig.go
More file actions
237 lines (214 loc) · 5.61 KB
/
Copy pathconfig.go
File metadata and controls
237 lines (214 loc) · 5.61 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
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
package cli
import (
"encoding/json"
"fmt"
"time"
"github.com/OpenTollGate/tollgate-module-basic-go/src/config_manager"
)
func (s *CLIServer) handleConfigCommand(args []string, flags map[string]string) CLIResponse {
if len(args) == 0 {
return CLIResponse{
Success: false,
Error: "Config command requires a subcommand (get, set, schema, save, save-identities)",
Timestamp: time.Now(),
}
}
subcommand := args[0]
switch subcommand {
case "get":
return s.handleConfigGet()
case "set":
if len(args) < 3 {
return CLIResponse{
Success: false,
Error: "config set requires <key> <value>",
Timestamp: time.Now(),
}
}
return s.handleConfigSet(args[1], args[2])
case "schema":
return s.handleConfigSchema()
case "save":
if len(args) < 2 {
return CLIResponse{
Success: false,
Error: "config save requires <json-string>",
Timestamp: time.Now(),
}
}
return s.handleConfigSave(args[1])
case "save-identities":
if len(args) < 2 {
return CLIResponse{
Success: false,
Error: "config save-identities requires <json-string>",
Timestamp: time.Now(),
}
}
return s.handleIdentitiesSave(args[1])
default:
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Unknown config subcommand: %s (supported: get, set, schema, save, save-identities)", subcommand),
Timestamp: time.Now(),
}
}
}
func (s *CLIServer) handleConfigGet() CLIResponse {
if s.configManager == nil {
return CLIResponse{
Success: false,
Error: "Config manager not available",
Timestamp: time.Now(),
}
}
cfg := s.configManager.GetConfig()
identities := s.configManager.GetIdentities()
return CLIResponse{
Success: true,
Message: "Configuration retrieved",
Data: map[string]interface{}{
"config": cfg,
"identities": identities,
},
Timestamp: time.Now(),
}
}
func (s *CLIServer) handleConfigSet(key, value string) CLIResponse {
if s.configManager == nil {
return CLIResponse{
Success: false,
Error: "Config manager not available",
Timestamp: time.Now(),
}
}
err := config_manager.SetDotPath(s.configManager, key, value)
if err != nil {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Failed to set %s: %v", key, err),
Timestamp: time.Now(),
}
}
return CLIResponse{
Success: true,
Message: fmt.Sprintf("Set %s = %s (restart tollgate-wrt to apply)", key, value),
Data: map[string]interface{}{
"key": key,
"value": value,
},
Timestamp: time.Now(),
}
}
func (s *CLIServer) handleConfigSchema() CLIResponse {
return CLIResponse{
Success: true,
Message: "Configuration schema",
Data: map[string]interface{}{
"config": config_manager.GetConfigSchema(),
"identities": config_manager.GetIdentitiesSchema(),
},
Timestamp: time.Now(),
}
}
func (s *CLIServer) handleConfigSave(jsonStr string) CLIResponse {
if s.configManager == nil {
return CLIResponse{
Success: false,
Error: "Config manager not available",
Timestamp: time.Now(),
}
}
var cfg config_manager.Config
if err := json.Unmarshal([]byte(jsonStr), &cfg); err != nil {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Invalid JSON: %v", err),
Timestamp: time.Now(),
}
}
requiredFields := []string{"config_version", "metric", "step_size", "accepted_mints", "profit_share"}
var raw map[string]interface{}
if err := json.Unmarshal([]byte(jsonStr), &raw); err != nil {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("JSON parse error: %v", err),
Timestamp: time.Now(),
}
}
var missing []string
for _, f := range requiredFields {
if _, ok := raw[f]; !ok {
missing = append(missing, f)
}
}
if len(missing) > 0 {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Missing required fields: %v", missing),
Timestamp: time.Now(),
}
}
if err := cfg.ValidateProfitShare(); err != nil {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Invalid profit_share: %v", err),
Timestamp: time.Now(),
}
}
if err := config_manager.SaveConfig(s.configManager.ConfigFilePath, &cfg); err != nil {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Failed to save config: %v", err),
Timestamp: time.Now(),
}
}
if err := s.configManager.ReloadConfig(); err != nil {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Config saved but reload failed: %v", err),
Timestamp: time.Now(),
}
}
return CLIResponse{
Success: true,
Message: "Configuration saved (restart tollgate-wrt to apply)",
Timestamp: time.Now(),
}
}
func (s *CLIServer) handleIdentitiesSave(jsonStr string) CLIResponse {
if s.configManager == nil {
return CLIResponse{
Success: false,
Error: "Config manager not available",
Timestamp: time.Now(),
}
}
var identities config_manager.IdentitiesConfig
if err := json.Unmarshal([]byte(jsonStr), &identities); err != nil {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Invalid JSON: %v", err),
Timestamp: time.Now(),
}
}
if err := config_manager.SaveIdentities(s.configManager.IdentitiesFilePath, &identities); err != nil {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Failed to save identities: %v", err),
Timestamp: time.Now(),
}
}
if err := s.configManager.ReloadIdentities(); err != nil {
return CLIResponse{
Success: false,
Error: fmt.Sprintf("Identities saved but reload failed: %v", err),
Timestamp: time.Now(),
}
}
return CLIResponse{
Success: true,
Message: "Identities saved (restart tollgate-wrt to apply)",
Timestamp: time.Now(),
}
}