-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathremove.ts
47 lines (40 loc) · 1.72 KB
/
remove.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
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'
import heredoc from 'tsheredoc'
export default class Remove extends Command {
static topic = 'spaces'
static hiddenAliases = ['trusted-ips:remove']
static description = heredoc(`
Remove a range from the list of trusted IP ranges
Uses CIDR notation.`)
static examples = [heredoc(`
$ heroku trusted-ips:remove --space my-space 192.168.2.0/24
Removed 192.168.2.0/24 from trusted IP ranges on my-space
`)]
static flags = {
space: flags.string({required: true, char: 's', description: 'space to remove rule from'}),
confirm: flags.string({description: 'set to space name to bypass confirm prompt'}),
}
static args = {
source: Args.string({required: true, description: 'IP address in CIDR notation'}),
}
public async run(): Promise<void> {
const {flags, args} = await this.parse(Remove)
const space = flags.space
const url = `/spaces/${space}/inbound-ruleset`
const {body: rules} = await this.heroku.get<Heroku.InboundRuleset>(url)
if (rules.rules?.length === 0) {
throw new Error('No IP ranges are configured. Nothing to do.')
}
const originalLength = rules.rules?.length
rules.rules = rules.rules?.filter(r => r.source !== args.source)
if (rules.rules?.length === originalLength) {
throw new Error(`No IP range matching ${args.source} was found.`)
}
await this.heroku.put(url, {body: rules})
ux.log(`Removed ${color.cyan.bold(args.source)} from trusted IP ranges on ${color.cyan.bold(space)}`)
ux.warn('It may take a few moments for the changes to take effect.')
}
}