Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit 455877d

Browse files
author
Akash Jain
committed
chore: refactor redundant code
1 parent a0f60ef commit 455877d

File tree

5 files changed

+16
-45
lines changed

5 files changed

+16
-45
lines changed

src/commands/delete-lambda.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class DeleteLambda extends BaseCommand {
2525
async run() {
2626
const opts = this.parse(DeleteLambda)
2727
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
28-
let endpoint = opts.flags.endpoint || ''
28+
const endpoint = await this.convertToGraphQLUid(apiServer, authFile, opts.flags.endpoint) || ''
2929

3030
const token = await this.getAccessToken(apiServer, authFile)
3131
if (!token) {
@@ -37,11 +37,6 @@ export default class DeleteLambda extends BaseCommand {
3737
return
3838
}
3939

40-
if (!endpoint.match(/0x[0-9a-f]+/)) {
41-
const backend = await this.findBackendByUrl(apiServer, token, endpoint)
42-
endpoint = backend?.uid || ''
43-
}
44-
4540
const {error, response} = await this.patchBackend(apiServer, token, endpoint, {lambdaScript: ''})
4641
if (error) {
4742
this.error(error)

src/commands/lambda-logs.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,13 @@ export default class LambdaLogs extends BaseCommand {
2525
async run() {
2626
const opts = this.parse(LambdaLogs)
2727
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
28-
let endpoint = opts.flags.endpoint || ''
28+
const endpoint = await this.convertToGraphQLUid(apiServer, authFile, opts.flags.endpoint) || ''
2929

3030
const token = await this.getAccessToken(apiServer, authFile)
3131
if (!token) {
3232
this.error('Please login with `slash-graphql` login')
3333
}
3434

35-
if (!endpoint.match(/0x[0-9a-f]+/)) {
36-
const backend = await this.findBackendByUrl(apiServer, token, endpoint)
37-
endpoint = backend?.uid || ''
38-
}
39-
4035
const start = new Date()
4136
start.setHours(start.getHours() - opts.flags.hours)
4237
const {errors, data} = await this.sendGraphQLRequest(apiServer, token, GET_LAMBDA_LOGS, {

src/commands/restore-backend.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ export default class RestoreBackend extends BaseCommand {
4040
async run() {
4141
const opts = this.parse(RestoreBackend)
4242
const backend = await this.backendFromOpts(opts)
43-
let sourceID = opts.flags.source
44-
45-
if (!sourceID.match(/0x[0-9a-f]+/)) {
46-
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
47-
sourceID = await this.convertToGraphQLUid(apiServer, authFile, sourceID) || ''
48-
}
43+
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
44+
const sourceID = await this.convertToGraphQLUid(apiServer, authFile, opts.flags.source) || ''
4945

5046
if (!(opts.flags.confirm || await this.confirm())) {
5147
this.log('Aborting')

src/commands/update-backend.ts

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
import {BaseCommand} from '../lib'
22
import {getEnvironment} from '../lib/environments'
33
import {flags} from '@oclif/command'
4-
import fetch from 'node-fetch'
54
import {cli} from 'cli-ux'
65

76
export default class UpdateBackend extends BaseCommand {
87
static description = 'Update Backend'
98

109
static examples = [
11-
'$ slash-graphql update-backend -n "New Name" 0xid',
10+
'$ slash-graphql update-backend -e 0xid -n "New Name" -m flexible',
1211
]
1312

1413
static flags = {
1514
...BaseCommand.commonFlags,
15+
...BaseCommand.endpointFlags,
1616
name: flags.string({char: 'n', description: 'Name'}),
1717
organizationId: flags.string({char: 'o', description: 'Organization UID', default: ''}),
1818
confirm: flags.boolean({char: 'y', description: 'Skip Confirmation', default: false}),
19-
'deployment-mode': flags.string({char: 'm', description: 'Deployment Mode', options: ['readonly', 'graphql', 'flexible']}),
19+
mode: flags.string({char: 'm', description: 'Backend Mode', options: ['readonly', 'graphql', 'flexible']}),
2020
}
2121

22-
static args = [{name: 'id', description: 'Backend UID', required: true}]
23-
2422
confirm() {
25-
this.log('Depending on which properties you are updating, this may cause your cluster to restart. Your data will be preserved')
23+
this.log('Depending on which properties you are updating, this may cause your backend to restart. Your data will be preserved')
2624
return cli.confirm('Are you sure you want to proceed?')
2725
}
2826

2927
async run() {
3028
const opts = this.parse(UpdateBackend)
3129
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
30+
const endpoint = await this.convertToGraphQLUid(apiServer, authFile, opts.flags.endpoint) || ''
3231

3332
const token = await this.getAccessToken(apiServer, authFile)
3433
if (!token) {
@@ -39,8 +38,8 @@ export default class UpdateBackend extends BaseCommand {
3938
if (opts.flags.name) {
4039
updates.name = opts.flags.name
4140
}
42-
if (opts.flags['deployment-mode']) {
43-
updates.deploymentMode = opts.flags['deployment-mode']
41+
if (opts.flags.mode) {
42+
updates.deploymentMode = opts.flags.mode
4443
}
4544
if (opts.flags.organizationId) {
4645
updates.organizationId = opts.flags.organizationId
@@ -55,19 +54,10 @@ export default class UpdateBackend extends BaseCommand {
5554
return
5655
}
5756

58-
const response = await fetch(`${apiServer}/deployment/${opts.args.id}`, {
59-
method: 'PATCH',
60-
headers: {
61-
'Content-Type': 'application/json',
62-
Authorization: `Bearer ${token}`,
63-
},
64-
body: JSON.stringify(updates),
65-
})
66-
if (response.status !== 200) {
67-
this.error(`Error while updating backend\n${await response.text()}`)
68-
}
69-
if (!opts.flags.quiet) {
70-
this.log('Updated Backend')
57+
const {error, response} = await this.patchBackend(apiServer, token, endpoint, updates)
58+
if (error) {
59+
this.error(error)
7160
}
61+
this.log(response.message)
7262
}
7363
}

src/commands/update-lambda.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,13 @@ export default class LambdaLogs extends BaseCommand {
2020
async run() {
2121
const opts = this.parse(LambdaLogs)
2222
const {apiServer, authFile} = getEnvironment(opts.flags.environment)
23-
let endpoint = opts.flags.endpoint || ''
23+
const endpoint = await this.convertToGraphQLUid(apiServer, authFile, opts.flags.endpoint) || ''
2424

2525
const token = await this.getAccessToken(apiServer, authFile)
2626
if (!token) {
2727
this.error('Please login with `slash-graphql` login')
2828
}
2929

30-
if (!endpoint.match(/0x[0-9a-f]+/)) {
31-
const backend = await this.findBackendByUrl(apiServer, token, endpoint)
32-
endpoint = backend?.uid || ''
33-
}
34-
3530
const data = fs.readFileSync(opts.flags.file, 'utf8')
3631
const lambdaScript = Buffer.from(data).toString('base64')
3732
const {error, response} = await this.patchBackend(apiServer, token, endpoint, {lambdaScript})

0 commit comments

Comments
 (0)