/
mailForwarder.go
355 lines (285 loc) · 9.24 KB
/
mailForwarder.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package cmd
import (
"fmt"
"strings"
"github.com/level27/l27-go"
"github.com/level27/lvl/utils"
"github.com/spf13/cobra"
)
func init() {
// MAIL FORWARDER
mailCmd.AddCommand(mailForwarderCmd)
// MAIL FORWARDER GET
mailForwarderCmd.AddCommand(mailForwarderGetCmd)
addCommonGetFlags(mailForwarderGetCmd)
// MAIL FORWARDER CREATE
mailForwarderCmd.AddCommand(mailForwarderCreateCmd)
addWaitFlag(mailForwarderCreateCmd)
mailForwarderCreateCmd.Flags().StringVar(&mailForwarderCreateAddress, "address", "", "Address of the mail forwarder")
mailForwarderCreateCmd.Flags().StringVar(&mailForwarderCreateDestination, "destination", "", "Comma-separated list of destination addresses to forward to")
// MAIL FORWARDER DELETE
mailForwarderCmd.AddCommand(mailForwarderDeleteCmd)
addWaitFlag(mailForwarderDeleteCmd)
mailForwarderDeleteCmd.Flags().BoolVar(&mailForwarderDeleteForce, "force", false, "Do not ask for confirmation to delete the mail forwarder")
// MAIL FORWARDER UPDATE
mailForwarderCmd.AddCommand(mailForwarderUpdateCmd)
settingsFileFlag(mailForwarderUpdateCmd)
settingString(mailForwarderUpdateCmd, updateSettings, "destination", "Comma-separated list of all destinations for this forwarder")
// MAIL FORWARDER DESTINATION
mailForwarderCmd.AddCommand(mailForwarderDestinationCmd)
// MAIL FORWARDER DESTINATION ADD
mailForwarderDestinationCmd.AddCommand(mailForwarderDestinationAddCmd)
// MAIL FORWARDER DESTINATION REMOVE
mailForwarderDestinationCmd.AddCommand(mailForwarderDestinationRemoveCmd)
}
func resolveMailforwarder(mailgroupID l27.IntID, arg string) (l27.IntID, error) {
id, err := l27.ParseID(arg)
if err == nil {
return id, nil
}
options, err := Level27Client.MailgroupsMailforwardersLookup(mailgroupID, arg)
if err != nil {
return 0, err
}
res, err := resolveShared(
options,
arg,
"mailforwarder",
func(app l27.Mailforwarder) string { return fmt.Sprintf("%s (%d)", app.Address, app.ID) })
if err != nil {
return 0, err
}
return res.ID, nil
}
// MAIL FORWARDER
var mailForwarderCmd = &cobra.Command{
Use: "forwarder",
Short: "Commands for managing mail forwarders",
Aliases: []string{"fwd"},
}
// MAIL FORWARDER GET
var mailForwarderGetCmd = &cobra.Command{
Use: "get [mailgroup]",
Short: "Get a list of mail forwarders in a mail group",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
mailgroupID, err := resolveMailgroup(args[0])
if err != nil {
return err
}
mailboxes, err := Level27Client.MailgroupsMailforwardersGetList(mailgroupID, optGetParameters)
if err != nil {
return err
}
outputFormatTableFuncs(
mailboxes,
[]string{"ID", "Status", "Address", "Destimations"},
[]interface{}{"ID", "Status", "Address", func(f l27.Mailforwarder) string {
first := true
result := ""
for _, dest := range f.Destination {
if !first {
result += "\n\t\t\t"
}
first = false
result += dest
}
result += "\n\t\t\t\t"
return result
}})
return nil
},
}
// MAIL FORWARDER CREATE
var mailForwarderCreateAddress string
var mailForwarderCreateDestination string
var mailForwarderCreateCmd = &cobra.Command{
Use: "create [mailgroup]",
Short: "Create a new mail forwarder",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
mailgroupID, err := resolveMailgroup(args[0])
if err != nil {
return err
}
mailforwarder, err := Level27Client.MailgroupsMailforwardersCreate(mailgroupID, l27.MailforwarderCreate{
Address: mailForwarderCreateAddress,
Destination: mailForwarderCreateDestination,
})
if err != nil {
return err
}
if optWait {
mailforwarder, err = waitForStatus(
func() (l27.Mailforwarder, error) {
return Level27Client.MailgroupsMailforwardersGetSingle(mailgroupID, mailforwarder.ID)
},
func(s l27.Mailforwarder) string { return s.Status },
"ok",
[]string{"to_create", "creating"},
)
if err != nil {
return fmt.Errorf("waiting on mailboxforwarder status failed: %s", err.Error())
}
}
outputFormatTemplate(mailforwarder, "templates/entities/mailForwarder/create.tmpl")
return nil
},
}
// MAIL FORWARDER DELETE
var mailForwarderDeleteForce bool
var mailForwarderDeleteCmd = &cobra.Command{
Use: "delete [mailgroup] [mail forwarder]",
Short: "Delete a mail forwarder",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
mailgroupID, err := resolveMailgroup(args[0])
if err != nil {
return err
}
mailforwarderID, err := resolveMailforwarder(mailgroupID, args[1])
if err != nil {
return err
}
if !mailForwarderDeleteForce {
mailbox, err := Level27Client.MailgroupsMailforwardersGetSingle(mailgroupID, mailforwarderID)
if err != nil {
return err
}
if !confirmPrompt(fmt.Sprintf("Delete mail forwarder %s (%d)?", mailbox.Address, mailforwarderID)) {
return nil
}
}
err = Level27Client.MailgroupsMailforwardersDelete(mailgroupID, mailforwarderID)
if err != nil {
return err
}
if optWait {
err = waitForDelete(
func() (l27.Mailforwarder, error) {
return Level27Client.MailgroupsMailforwardersGetSingle(mailgroupID, mailforwarderID)
},
func(a l27.Mailforwarder) string { return a.Status },
[]string{"deleting", "to_delete"},
)
if err != nil {
return fmt.Errorf("waiting on mail forwarder status failed: %s", err.Error())
}
}
outputFormatTemplate(nil, "templates/entities/mailForwarder/delete.tmpl")
return nil
},
}
// MAIL FORWARDER UPDATE
var mailForwarderUpdateCmd = &cobra.Command{
Use: "update [mailgroup] [mail forwarder]",
Short: "Update settings on a mail forwarder",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
settings, err := loadMergeSettings(updateSettingsFile, updateSettings)
if err != nil {
return err
}
mailgroupID, err := resolveMailgroup(args[0])
if err != nil {
return err
}
mailforwarderID, err := resolveMailforwarder(mailgroupID, args[1])
if err != nil {
return err
}
mailforwarder, err := Level27Client.MailgroupsMailforwardersGetSingle(mailgroupID, mailforwarderID)
if err != nil {
return err
}
mailforwarderPut := l27.MailforwarderPut{
Address: mailforwarder.Address,
Destination: strings.Join(mailforwarder.Destination, ","),
}
data := utils.RoundTripJson(mailforwarderPut).(map[string]interface{})
data = mergeMaps(data, settings)
err = Level27Client.MailgroupsMailforwardersUpdate(mailgroupID, mailforwarderID, data)
if err != nil {
return err
}
outputFormatTemplate(nil, "templates/entities/mailForwarder/update.tmpl")
return nil
},
}
// MAIL FORWARDER DESTINATION
var mailForwarderDestinationCmd = &cobra.Command{
Use: "destination",
Short: "Commands for managing destination addresses on a mail forwarder easily",
Aliases: []string{"dest"},
}
// MAIL FORWARDER DESTINATION ADD
var mailForwarderDestinationAddCmd = &cobra.Command{
Use: "add [mail group] [mail forwarder] [new destination]",
Short: "Add a single destination address to a mail forwarder",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
mailgroupID, err := resolveMailgroup(args[0])
if err != nil {
return err
}
mailforwarderID, err := resolveMailforwarder(mailgroupID, args[1])
if err != nil {
return err
}
mailforwarder, err := Level27Client.MailgroupsMailforwardersGetSingle(mailgroupID, mailforwarderID)
if err != nil {
return err
}
destination := append(mailforwarder.Destination, args[2])
mailforwarderPut := l27.MailforwarderPut{
Address: mailforwarder.Address,
Destination: strings.Join(destination, ","),
}
data := utils.RoundTripJson(mailforwarderPut).(map[string]interface{})
err = Level27Client.MailgroupsMailforwardersUpdate(mailgroupID, mailforwarderID, data)
if err != nil {
return err
}
outputFormatTemplate(nil, "templates/entities/mailForwarderDestination/add.tmpl")
return nil
},
}
// MAIL FORWARDER DESTINATION REMOVE
var mailForwarderDestinationRemoveCmd = &cobra.Command{
Use: "remove [mail group] [mail forwarder] [old destination]",
Short: "Remove a single destination address from a mail forwarder",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
mailgroupID, err := resolveMailgroup(args[0])
if err != nil {
return err
}
mailforwarderID, err := resolveMailforwarder(mailgroupID, args[1])
if err != nil {
return err
}
destAddress := args[2]
mailforwarder, err := Level27Client.MailgroupsMailforwardersGetSingle(mailgroupID, mailforwarderID)
if err != nil {
return err
}
destination := mailforwarder.Destination
idx := indexOf(destination, destAddress)
if idx == -1 {
fmt.Printf("'%s' is not a destination on this mail forwarder.", destAddress)
return nil
}
destination = append(destination[:idx], destination[idx+1:]...)
mailforwarderPut := l27.MailforwarderPut{
Address: mailforwarder.Address,
Destination: strings.Join(destination, ","),
}
data := utils.RoundTripJson(mailforwarderPut).(map[string]interface{})
err = Level27Client.MailgroupsMailforwardersUpdate(mailgroupID, mailforwarderID, data)
if err != nil {
return err
}
outputFormatTemplate(nil, "templates/entities/mailForwarderDestination/remove.tmpl")
return nil
},
}