-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete.ts
193 lines (161 loc) · 6.18 KB
/
delete.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
import { Args, Flags } from '@oclif/core'
import chalk from 'chalk'
import * as readline from 'node:readline'
import { ControlBaseCommand } from '../../../control-base-command.js'
export default class ChannelRulesDeleteCommand extends ControlBaseCommand {
static args = {
nameOrId: Args.string({
description: 'Name or ID of the channel rule to delete',
required: true,
}),
}
static description = 'Delete a channel rule'
static examples = [
'$ ably apps channel-rules delete chat',
'$ ably apps channel-rules delete events --app "My App"',
'$ ably apps channel-rules delete notifications --force',
'$ ably apps channel-rules delete chat --json',
'$ ably apps channel-rules delete chat --pretty-json'
]
static flags = {
...ControlBaseCommand.globalFlags,
'app': Flags.string({
description: 'App ID or name to delete the channel rule from',
required: false,
}),
'force': Flags.boolean({
char: 'f',
default: false,
description: 'Force deletion without confirmation',
required: false,
}),
}
async run(): Promise<void> {
const { args, flags } = await this.parse(ChannelRulesDeleteCommand)
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;
}
// Find the namespace by name or ID
const namespaces = await controlApi.listNamespaces(appId)
const namespace = namespaces.find(n => n.id === args.nameOrId)
if (!namespace) {
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({
appId,
error: `Channel rule "${args.nameOrId}" not found`,
status: 'error',
success: false
}, flags));
} else {
this.error(`Channel rule "${args.nameOrId}" not found`);
}
return;
}
// If not using force flag or JSON mode, prompt for confirmation
if (!flags.force && !this.shouldOutputJson(flags)) {
this.log(`\nYou are about to delete the following channel rule:`);
this.log(`ID: ${namespace.id}`);
this.log(`Persisted: ${namespace.persisted ? chalk.green('Yes') : 'No'}`);
this.log(`Push Enabled: ${namespace.pushEnabled ? chalk.green('Yes') : 'No'}`);
if (namespace.authenticated !== undefined) {
this.log(`Authenticated: ${namespace.authenticated ? chalk.green('Yes') : 'No'}`);
}
if (namespace.persistLast !== undefined) {
this.log(`Persist Last: ${namespace.persistLast ? chalk.green('Yes') : 'No'}`);
}
if (namespace.exposeTimeSerial !== undefined) {
this.log(`Expose Time Serial: ${namespace.exposeTimeSerial ? chalk.green('Yes') : 'No'}`);
}
if (namespace.populateChannelRegistry !== undefined) {
this.log(`Populate Channel Registry: ${namespace.populateChannelRegistry ? chalk.green('Yes') : 'No'}`);
}
if (namespace.batchingEnabled !== undefined) {
this.log(`Batching Enabled: ${namespace.batchingEnabled ? chalk.green('Yes') : 'No'}`);
}
if (namespace.batchingInterval !== undefined) {
this.log(`Batching Interval: ${chalk.green(namespace.batchingInterval.toString())}`);
}
if (namespace.conflationEnabled !== undefined) {
this.log(`Conflation Enabled: ${namespace.conflationEnabled ? chalk.green('Yes') : 'No'}`);
}
if (namespace.conflationInterval !== undefined) {
this.log(`Conflation Interval: ${chalk.green(namespace.conflationInterval.toString())}`);
}
if (namespace.conflationKey !== undefined) {
this.log(`Conflation Key: ${chalk.green(namespace.conflationKey)}`);
}
if (namespace.tlsOnly !== undefined) {
this.log(`TLS Only: ${namespace.tlsOnly ? chalk.green('Yes') : 'No'}`);
}
this.log(`Created: ${this.formatDate(namespace.created)}`);
const confirmed = await this.promptForConfirmation(`\nAre you sure you want to delete channel rule with ID "${namespace.id}"? [y/N]`);
if (!confirmed) {
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({
appId,
error: 'Deletion cancelled by user',
ruleId: namespace.id,
status: 'cancelled',
success: false
}, flags));
} else {
this.log('Deletion cancelled');
}
return;
}
}
await controlApi.deleteNamespace(appId, namespace.id)
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({
appId,
rule: {
id: namespace.id
},
success: true,
timestamp: new Date().toISOString()
}, flags));
} else {
this.log(`Channel rule with ID "${namespace.id}" deleted successfully`);
}
} 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 deleting channel rule: ${error instanceof Error ? error.message : String(error)}`);
}
}
}
private promptForConfirmation(prompt: string): Promise<boolean> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
return new Promise((resolve) => {
rl.question(prompt, (answer) => {
rl.close()
resolve(answer.toLowerCase() === 'y')
})
})
}
}