This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
destroy-backend.ts
66 lines (52 loc) · 1.79 KB
/
destroy-backend.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
import {BaseCommand} from '../lib'
import {getEnvironment} from '../lib/environments'
import {flags} from '@oclif/command'
import fetch from 'node-fetch'
import {cli} from 'cli-ux'
export default class DestroyBackend extends BaseCommand {
static description = 'Destroy a Backend by id'
static examples = [
'$ slash-graphql destroy-backend "0xid"',
]
static flags = {
...BaseCommand.commonFlags,
confirm: flags.boolean({char: 'y', description: 'Skip Confirmation', default: false}),
}
static args = [{name: 'id', description: 'Backend id', required: true}]
confirm() {
this.log('This will destroy your backend, and cannot be reversed')
return cli.confirm('Are you sure you want to proceed?')
}
async run() {
const opts = this.parse(DestroyBackend)
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
const id = opts.args.id
if (!id.match(/0x[0-9a-f]+/)) {
this.error(`Invalid id ${id}`)
}
const token = await this.getAccessToken(apiServer, authFile)
if (!token) {
this.error('Please login with `slash-graphql login`')
}
const backend = this.findBackendByUid(apiServer, token, id)
if (!backend) {
this.error('Cannot find the backend that you are trying to delete. Please run `slash-graphql list-backends` to get a list of backends')
}
if (!(opts.flags.confirm || await this.confirm())) {
this.log('Aborting')
return
}
const response = await fetch(`${apiServer}/deployment/${id}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${token}`,
},
})
if (response.status !== 200) {
this.error(`Unable to destroy backend. Try logging in again\n${await response.text()}`)
}
if (!opts.flags.quiet) {
this.log('Deleted')
}
}
}