-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
299 lines (234 loc) · 9.03 KB
/
system.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
package mailinabox
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
// SystemStatus Represents a system status.
type SystemStatus struct {
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
Extra []ExtraStatus `json:"extra,omitempty"`
}
// ExtraStatus Represents extra status.
type ExtraStatus struct {
Monospace bool `json:"monospace,omitempty"`
Text string `json:"text,omitempty"`
}
// BackupStatus Represents a backup status.
type BackupStatus struct {
Backups []Backup `json:"backups,omitempty"`
UnmatchedFileSize int `json:"unmatched_file_size,omitempty"`
Error string `json:"error,omitempty"`
}
// Backup Represents a backup.
type Backup struct {
Date string `json:"date,omitempty"`
DateDelta string `json:"date_delta,omitempty"`
DateStr string `json:"date_str,omitempty"`
DeletedIn string `json:"deleted_in,omitempty"`
Full bool `json:"full,omitempty"`
Size int `json:"size,omitempty"`
Volumes int `json:"volumes,omitempty"`
}
// BackupConfig Represents a backup configuration.
type BackupConfig struct {
EncPwFile string `json:"enc_pw_file,omitempty"`
FileTargetDirectory string `json:"file_target_directory,omitempty"`
MinAgeInDays int `json:"min_age_in_days,omitempty"`
SSHPubKey string `json:"ssh_pub_key,omitempty"`
Target string `json:"target,omitempty"`
TargetUser string `json:"target_user,omitempty"`
TargetPass string `json:"target_pass,omitempty"`
}
// SystemService System API.
// https://mailinabox.email/api-docs.html#tag/System
type SystemService service
// GetStatus Returns an array of statuses which can include headings.
// https://mailinabox.email/api-docs.html#operation/getSystemStatus
func (s *SystemService) GetStatus(ctx context.Context) ([]SystemStatus, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "status")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var results []SystemStatus
err = s.client.doJSON(req, &results)
if err != nil {
return nil, err
}
return results, nil
}
// GetVersion Returns installed Mail-in-a-Box version.
// https://mailinabox.email/api-docs.html#operation/getSystemVersion
func (s *SystemService) GetVersion(ctx context.Context) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "version")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetUpstreamVersion Returns Mail-in-a-Box upstream version.
// https://mailinabox.email/api-docs.html#operation/getSystemUpstreamVersion
func (s *SystemService) GetUpstreamVersion(ctx context.Context) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "latest-upstream-version")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), http.NoBody)
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetUpdates Returns system (apt) updates.
// https://mailinabox.email/api-docs.html#operation/getSystemUpdates
func (s *SystemService) GetUpdates(ctx context.Context) ([]string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "updates")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return nil, err
}
updates := strings.Split(strings.TrimSpace(string(resp)), "\n")
return updates, nil
}
// UpdatePackages Updates system (apt) packages.
// https://mailinabox.email/api-docs.html#operation/getSystemUpdates
func (s *SystemService) UpdatePackages(ctx context.Context) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "update-packages")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), http.NoBody)
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetPrivacyStatus Returns system privacy (new-version check) status.
// https://mailinabox.email/api-docs.html#operation/getSystemPrivacyStatus
func (s *SystemService) GetPrivacyStatus(ctx context.Context) (bool, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "privacy")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return false, fmt.Errorf("unable to create request: %w", err)
}
var result bool
err = s.client.doJSON(req, &result)
if err != nil {
return false, err
}
return result, nil
}
// UpdatePrivacyStatus Updates system privacy (new-version checks).
// - value: `private`: Disable new version checks
// - value: `off`: Enable new version checks
// https://mailinabox.email/api-docs.html#operation/updateSystemPrivacy
func (s *SystemService) UpdatePrivacyStatus(ctx context.Context, value string) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "privacy")
data := url.Values{}
data.Set("value", value)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetRebootStatus Returns the system reboot status.
// https://mailinabox.email/api-docs.html#operation/getSystemRebootStatus
func (s *SystemService) GetRebootStatus(ctx context.Context) (bool, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "reboot")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return false, fmt.Errorf("unable to create request: %w", err)
}
var result bool
err = s.client.doJSON(req, &result)
if err != nil {
return false, err
}
return result, nil
}
// Reboot Reboots the system.
// https://mailinabox.email/api-docs.html#operation/rebootSystem
func (s *SystemService) Reboot(ctx context.Context) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "reboot")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), http.NoBody)
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetBackupStatus Returns the system backup status.
// https://mailinabox.email/api-docs.html#operation/getSystemBackupStatus
func (s *SystemService) GetBackupStatus(ctx context.Context) (*BackupStatus, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "backup", "status")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var result BackupStatus
err = s.client.doJSON(req, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// GetBackupConfig Returns the system backup config.
// https://mailinabox.email/api-docs.html#operation/getSystemBackupConfig
func (s *SystemService) GetBackupConfig(ctx context.Context) (*BackupConfig, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "backup", "config")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var result BackupConfig
err = s.client.doJSON(req, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// UpdateBackupConfig Updates the system backup config.
// https://mailinabox.email/api-docs.html#operation/updateSystemBackupConfig
func (s *SystemService) UpdateBackupConfig(ctx context.Context, target, targetUser, targetPass string, minAge int) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "system", "backup", "config")
data := url.Values{}
data.Set("target", target)
data.Set("targetUser", targetUser)
data.Set("targetPass", targetPass)
data.Set("minAge", strconv.Itoa(minAge))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}