-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate.ts
205 lines (182 loc) · 7.8 KB
/
create.ts
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
import { Flags } from '@oclif/core'
import chalk from 'chalk'
import { ControlBaseCommand } from '../../../control-base-command.js'
export default class ChannelRulesCreateCommand extends ControlBaseCommand {
static description = 'Create a channel rule'
static examples = [
'$ ably apps channel-rules create --name "chat" --persisted',
'$ ably apps channel-rules create --name "events" --push-enabled',
'$ ably apps channel-rules create --name "notifications" --persisted --push-enabled --app "My App"',
]
static flags = {
...ControlBaseCommand.globalFlags,
'app': Flags.string({
description: 'App ID or name to create the channel rule in',
required: false,
}),
'authenticated': Flags.boolean({
description: 'Whether channels matching this rule require clients to be authenticated',
required: false,
}),
'batching-enabled': Flags.boolean({
description: 'Whether to enable batching for messages on channels matching this rule',
required: false,
}),
'batching-interval': Flags.integer({
description: 'The batching interval for messages on channels matching this rule',
required: false,
}),
'conflation-enabled': Flags.boolean({
description: 'Whether to enable conflation for messages on channels matching this rule',
required: false,
}),
'conflation-interval': Flags.integer({
description: 'The conflation interval for messages on channels matching this rule',
required: false,
}),
'conflation-key': Flags.string({
description: 'The conflation key for messages on channels matching this rule',
required: false,
}),
'expose-time-serial': Flags.boolean({
description: 'Whether to expose the time serial for messages on channels matching this rule',
required: false,
}),
'name': Flags.string({
description: 'Name of the channel rule',
required: true,
}),
'persist-last': Flags.boolean({
description: 'Whether to persist only the last message on channels matching this rule',
required: false,
}),
'persisted': Flags.boolean({
default: false,
description: 'Whether messages on channels matching this rule should be persisted',
required: false,
}),
'populate-channel-registry': Flags.boolean({
description: 'Whether to populate the channel registry for channels matching this rule',
required: false,
}),
'push-enabled': Flags.boolean({
default: false,
description: 'Whether push notifications should be enabled for channels matching this rule',
required: false,
}),
'tls-only': Flags.boolean({
description: 'Whether to enforce TLS for channels matching this rule',
required: false,
}),
}
async run(): Promise<void> {
const { flags } = await this.parse(ChannelRulesCreateCommand)
const controlApi = this.createControlApi(flags)
let appId: string | undefined;
try {
let appId = flags.app
if (!appId) {
appId = await this.resolveAppId(flags)
}
if (!appId) {
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({
error: 'No app specified. Use --app flag or select an app with "ably apps switch"',
status: 'error',
success: false
}, flags));
} else {
this.error('No app specified. Use --app flag or select an app with "ably apps switch"');
}
return;
}
const namespaceData = {
authenticated: flags.authenticated,
batchingEnabled: flags['batching-enabled'],
batchingInterval: flags['batching-interval'],
channelNamespace: flags.name,
conflationEnabled: flags['conflation-enabled'],
conflationInterval: flags['conflation-interval'],
conflationKey: flags['conflation-key'],
exposeTimeSerial: flags['expose-time-serial'],
persistLast: flags['persist-last'],
persisted: flags.persisted,
populateChannelRegistry: flags['populate-channel-registry'],
pushEnabled: flags['push-enabled'],
tlsOnly: flags['tls-only'],
}
const createdNamespace = await controlApi.createNamespace(appId, namespaceData)
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({
appId,
rule: {
authenticated: createdNamespace.authenticated,
batchingEnabled: createdNamespace.batchingEnabled,
batchingInterval: createdNamespace.batchingInterval,
conflationEnabled: createdNamespace.conflationEnabled,
conflationInterval: createdNamespace.conflationInterval,
conflationKey: createdNamespace.conflationKey,
created: new Date(createdNamespace.created).toISOString(),
exposeTimeSerial: createdNamespace.exposeTimeSerial,
id: createdNamespace.id,
name: flags.name,
persistLast: createdNamespace.persistLast,
persisted: createdNamespace.persisted,
populateChannelRegistry: createdNamespace.populateChannelRegistry,
pushEnabled: createdNamespace.pushEnabled,
tlsOnly: createdNamespace.tlsOnly
},
success: true,
timestamp: new Date().toISOString()
}, flags));
} else {
this.log('Channel rule created successfully:');
this.log(`ID: ${createdNamespace.id}`);
this.log(`Persisted: ${createdNamespace.persisted ? chalk.green('Yes') : 'No'}`);
this.log(`Push Enabled: ${createdNamespace.pushEnabled ? chalk.green('Yes') : 'No'}`);
if (createdNamespace.authenticated !== undefined) {
this.log(`Authenticated: ${createdNamespace.authenticated ? chalk.green('Yes') : 'No'}`);
}
if (createdNamespace.persistLast !== undefined) {
this.log(`Persist Last: ${createdNamespace.persistLast ? chalk.green('Yes') : 'No'}`);
}
if (createdNamespace.exposeTimeSerial !== undefined) {
this.log(`Expose Time Serial: ${createdNamespace.exposeTimeSerial ? chalk.green('Yes') : 'No'}`);
}
if (createdNamespace.populateChannelRegistry !== undefined) {
this.log(`Populate Channel Registry: ${createdNamespace.populateChannelRegistry ? chalk.green('Yes') : 'No'}`);
}
if (createdNamespace.batchingEnabled !== undefined) {
this.log(`Batching Enabled: ${createdNamespace.batchingEnabled ? chalk.green('Yes') : 'No'}`);
}
if (createdNamespace.batchingInterval !== undefined) {
this.log(`Batching Interval: ${chalk.green(createdNamespace.batchingInterval.toString())}`);
}
if (createdNamespace.conflationEnabled !== undefined) {
this.log(`Conflation Enabled: ${createdNamespace.conflationEnabled ? chalk.green('Yes') : 'No'}`);
}
if (createdNamespace.conflationInterval !== undefined) {
this.log(`Conflation Interval: ${chalk.green(createdNamespace.conflationInterval.toString())}`);
}
if (createdNamespace.conflationKey !== undefined) {
this.log(`Conflation Key: ${chalk.green(createdNamespace.conflationKey)}`);
}
if (createdNamespace.tlsOnly !== undefined) {
this.log(`TLS Only: ${createdNamespace.tlsOnly ? chalk.green('Yes') : 'No'}`);
}
this.log(`Created: ${this.formatDate(createdNamespace.created)}`);
}
} catch (error) {
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({
appId,
error: error instanceof Error ? error.message : String(error),
status: 'error',
success: false
}, flags));
} else {
this.error(`Error creating channel rule: ${error instanceof Error ? error.message : String(error)}`);
}
}
}
}