Skip to content

Commit 978a5c9

Browse files
Use generic print table function, formatting code, and improving unit test for environment commands
1 parent 27dfd09 commit 978a5c9

File tree

11 files changed

+204
-231
lines changed

11 files changed

+204
-231
lines changed
Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1-
import { Flags } from '@oclif/core'
1+
import {Flags} from '@oclif/core'
22

3-
import { ScCommand } from '../../../sc-command.js'
4-
import { Environment, EnvironmentDetail } from '../../../types/environment.js'
5-
import { camelCaseToTitleCase, renderKeyValueTable } from '../../../util/internal.js'
6-
import { ScConnection } from '../../../util/sc-connection.js'
3+
import {ScCommand} from '../../../sc-command.js'
4+
import {EnvironmentApiResponse} from '../../../types/environment.js'
5+
import {printObjectAsKeyValueTable} from '../../../util/internal.js'
6+
import {ScConnection} from '../../../util/sc-connection.js'
77

88
export default class PlatformEnvCreate extends ScCommand<typeof PlatformEnvCreate> {
99
static override args = {}
1010
static override description = `Create a new environment.
1111
1212
Token Permissions: [ environments:edit ]`
13-
static override examples = ['<%= config.bin %> <%= command.id %> --name=MyEnvironment --desc="My environment description" --isDefault --isProduction']
13+
static override examples = [
14+
'<%= config.bin %> <%= command.id %> --name=MyEnvironment --description="My environment description" --isDefault --isProduction',
15+
]
1416
static override flags = {
1517
description: Flags.string({
1618
char: 'd',
17-
description: 'Description of the environment to create.'
19+
description: 'Description of the environment to create.',
1820
}),
1921
isDefault: Flags.boolean({
20-
description: 'Indicates this is the organizations default environment.'
22+
description: `Indicates this is the organization's default environment.`,
2123
}),
2224
isProduction: Flags.boolean({
23-
description: `Indicates this is an organizations production environment.
25+
description: `Indicates this is an organization's production environment.
2426
This is an immutable field. If an environment needs to be migrated,
2527
architecture can be migrated to a new environment with the desired
2628
environment type instead.`,
2729
}),
2830
name: Flags.string({
2931
char: 'n',
3032
description: 'Name of the environment to create.',
31-
required: true
33+
required: true,
3234
}),
3335
}
3436

35-
public async run(): Promise<Environment> {
36-
const { flags } = await this.parse(PlatformEnvCreate)
37+
public async run(): Promise<EnvironmentApiResponse> {
38+
const {flags} = await this.parse(PlatformEnvCreate)
3739

3840
const name = flags.name ?? ''
3941
const desc = flags.description ?? ''
@@ -46,29 +48,19 @@ export default class PlatformEnvCreate extends ScCommand<typeof PlatformEnvCreat
4648
const apiUrl: string = `/platform/environments`
4749
// API body
4850
const body = {
49-
...(desc && { description: desc }),
51+
...(desc && {description: desc}),
5052
isDefault,
5153
isProduction,
5254
name,
5355
}
5456

5557
// API call
56-
const resp = await conn.post<EnvironmentDetail>(apiUrl, body)
58+
const resp = await conn.post<EnvironmentApiResponse>(apiUrl, body)
5759

5860
// Display results
59-
this.log('Environment created successfully.')
60-
this.print(resp.data)
61+
this.log(printObjectAsKeyValueTable(resp.data as unknown as Record<string, unknown>))
6162

6263
// Return raw json if --json flag is set
63-
return resp.data
64-
}
65-
66-
private print(environment: Environment): void {
67-
const tableRows = [
68-
['Key', 'Value'],
69-
...Object.entries(environment).map(([key, value]) => [camelCaseToTitleCase(key), value]),
70-
]
71-
this.log()
72-
this.log(renderKeyValueTable(tableRows))
64+
return resp
7365
}
7466
}

src/commands/platform/env/delete.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
import { Flags } from '@oclif/core'
1+
import {Flags} from '@oclif/core'
22

3-
import { ScCommand } from '../../../sc-command.js'
4-
import { EnvironmentApiResponse } from '../../../types/environment.js'
5-
import { ScConnection } from '../../../util/sc-connection.js'
3+
import {ScCommand} from '../../../sc-command.js'
4+
import {EnvironmentListApiResponse} from '../../../types/environment.js'
5+
import {ScConnection} from '../../../util/sc-connection.js'
66

77
export default class PlatformEnvDelete extends ScCommand<typeof PlatformEnvDelete> {
88
static override args = {}
99
static override description = `Delete an environment using either its name or unique identifier. The default environment cannot be deleted.
1010
1111
Token Permissions: [ environments:edit ]`
12-
static override examples = ['<%= config.bin %> <%= command.id %> --name=MyEnvName', '<%= config.bin %> <%= command.id %> --env-id=MyEnvId']
12+
static override examples = [
13+
'<%= config.bin %> <%= command.id %> --name=MyEnvName',
14+
'<%= config.bin %> <%= command.id %> --env-id=MyEnvId',
15+
]
1316
static override flags = {
1417
'env-id': Flags.string({
1518
char: 'e',
1619
description: 'Id of the environment.',
17-
exactlyOne: ['env-id', 'name']
20+
exactlyOne: ['env-id', 'name'],
1821
}),
1922
name: Flags.string({
2023
char: 'n',
2124
description: 'Name of the environment.',
22-
exactlyOne: ['env-id', 'name']
25+
exactlyOne: ['env-id', 'name'],
2326
}),
2427
}
2528

26-
public async run(): Promise<{ message: string }> {
27-
const { flags } = await this.parse(PlatformEnvDelete)
29+
public async run(): Promise<{message: string}> {
30+
const {flags} = await this.parse(PlatformEnvDelete)
2831

2932
const name = flags.name ?? ''
3033
const envId = flags['env-id'] ?? ''
@@ -41,7 +44,7 @@ export default class PlatformEnvDelete extends ScCommand<typeof PlatformEnvDelet
4144
if (name) {
4245
// API call to get environment by name
4346
const getEnvApiUrl = `${apiUrl}?name=${name}`
44-
const resp = await conn.get<EnvironmentApiResponse>(getEnvApiUrl)
47+
const resp = await conn.get<EnvironmentListApiResponse>(getEnvApiUrl)
4548
if (resp.data.length > 1) {
4649
foundMultiple = true
4750
} else {
@@ -62,6 +65,6 @@ export default class PlatformEnvDelete extends ScCommand<typeof PlatformEnvDelet
6265
}
6366

6467
// Return raw json if --json flag is set
65-
return { message }
68+
return {message}
6669
}
6770
}
Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Flags } from '@oclif/core'
1+
import {Flags} from '@oclif/core'
22

3-
import { ScCommand } from '../../../sc-command.js'
4-
import { Environment, EnvironmentApiResponse, EnvironmentDetail } from '../../../types/environment.js'
5-
import { camelCaseToTitleCase, renderKeyValueTable } from '../../../util/internal.js'
6-
import { ScConnection } from '../../../util/sc-connection.js'
3+
import {ScCommand} from '../../../sc-command.js'
4+
import {EnvironmentApiResponse, EnvironmentListApiResponse} from '../../../types/environment.js'
5+
import {printObjectAsKeyValueTable} from '../../../util/internal.js'
6+
import {ScConnection} from '../../../util/sc-connection.js'
77

88
export default class PlatformEnvDisplay extends ScCommand<typeof PlatformEnvDisplay> {
99
static override args = {}
@@ -12,22 +12,25 @@ export default class PlatformEnvDisplay extends ScCommand<typeof PlatformEnvDisp
1212
Use either the Environment's ID (--env-id) or name of the Environment (--name).
1313
1414
Required token permissions: [ environments:view ]`
15-
static override examples = ['<%= config.bin %> <%= command.id %> --name=MyEnvName', '<%= config.bin %> <%= command.id %> --env-id=MyEnvId']
15+
static override examples = [
16+
'<%= config.bin %> <%= command.id %> --name=MyEnvName',
17+
'<%= config.bin %> <%= command.id %> --env-id=MyEnvId',
18+
]
1619
static override flags = {
1720
'env-id': Flags.string({
1821
char: 'e',
1922
description: 'Id of the environment.',
20-
exactlyOne: ['env-id', 'name']
23+
exactlyOne: ['env-id', 'name'],
2124
}),
2225
name: Flags.string({
2326
char: 'n',
2427
description: 'Name of the environment.',
25-
exactlyOne: ['env-id', 'name']
28+
exactlyOne: ['env-id', 'name'],
2629
}),
2730
}
2831

29-
public async run(): Promise<Environment[]> {
30-
const { flags } = await this.parse(PlatformEnvDisplay)
32+
public async run(): Promise<EnvironmentApiResponse | EnvironmentListApiResponse> {
33+
const {flags} = await this.parse(PlatformEnvDisplay)
3134

3235
const name = flags.name ?? ''
3336
const envId = flags['env-id'] ?? ''
@@ -38,36 +41,22 @@ export default class PlatformEnvDisplay extends ScCommand<typeof PlatformEnvDisp
3841
// If env name provided, get all environments matching provided name
3942
// If env id provided, get environment with that id
4043
let apiUrl: string = `/platform/environments`
41-
let rawResp: Environment[]
44+
let resp: EnvironmentApiResponse | EnvironmentListApiResponse
4245
if (envId) {
4346
// API call to get environment by id
4447
apiUrl += `/${envId}`
45-
const resp = await conn.get<EnvironmentDetail>(apiUrl)
46-
this.print(resp.data)
47-
rawResp = [resp.data]
48-
} else if (name) {
48+
resp = await conn.get<EnvironmentApiResponse>(apiUrl)
49+
this.log(printObjectAsKeyValueTable(resp.data as unknown as Record<string, unknown>))
50+
} else {
4951
// API call to get environment by name
5052
apiUrl += `?name=${name}`
51-
const resp = await conn.get<EnvironmentApiResponse>(apiUrl)
53+
resp = await conn.get<EnvironmentListApiResponse>(apiUrl)
5254
for (const env of resp.data) {
53-
this.print(env)
55+
this.log(printObjectAsKeyValueTable(env as unknown as Record<string, unknown>))
5456
}
55-
56-
rawResp = resp.data
57-
} else {
58-
this.error('Either --env-id or --name must be provided.')
5957
}
6058

6159
// Return raw json if --json flag is set
62-
return rawResp
63-
}
64-
65-
private print(environment: Environment): void {
66-
const tableRows = [
67-
['Key', 'Value'],
68-
...Object.entries(environment).map(([key, value]) => [camelCaseToTitleCase(key), value]),
69-
]
70-
this.log()
71-
this.log(renderKeyValueTable(tableRows))
60+
return resp
7261
}
7362
}

src/commands/platform/env/list.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
import { Flags } from '@oclif/core'
1+
import {Flags} from '@oclif/core'
22

3-
import { ScCommand } from '../../../sc-command.js'
4-
import { Environment, EnvironmentApiResponse } from '../../../types/environment.js'
5-
import { renderTable } from '../../../util/internal.js'
6-
import { ScConnection } from '../../../util/sc-connection.js'
3+
import {ScCommand} from '../../../sc-command.js'
4+
import {Environment, EnvironmentListApiResponse} from '../../../types/environment.js'
5+
import {renderTable} from '../../../util/internal.js'
6+
import {ScConnection} from '../../../util/sc-connection.js'
77

88
export default class PlatformEnvList extends ScCommand<typeof PlatformEnvList> {
99
static override args = {}
1010
static override description = `Get a list of all Environments.
1111
1212
Required token permissions: [ environments:view ]`
13-
static override examples = ['<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> --name=Default --pageNumber=1 --pageSize=10 --sort=name:ASC']
13+
static override examples = [
14+
'<%= config.bin %> <%= command.id %>',
15+
'<%= config.bin %> <%= command.id %> --name=Default --pageNumber=1 --pageSize=10 --sort=name:ASC',
16+
]
1417
static override flags = {
1518
name: Flags.string({
1619
char: 'n',
17-
description: 'Name of the environment to match on.'
20+
description: 'Name of the environment to match on.',
1821
}),
1922
pageNumber: Flags.integer({
2023
char: 'p',
21-
description: 'The page number to get. Defaults to 10'
24+
description: 'The page number to get. Defaults to 10',
2225
}),
2326
pageSize: Flags.integer({
2427
char: 's',
@@ -31,8 +34,8 @@ export default class PlatformEnvList extends ScCommand<typeof PlatformEnvList> {
3134
}),
3235
}
3336

34-
public async run(): Promise<Environment[]> {
35-
const { flags } = await this.parse(PlatformEnvList)
37+
public async run(): Promise<EnvironmentListApiResponse> {
38+
const {flags} = await this.parse(PlatformEnvList)
3639

3740
const conn = new ScConnection()
3841

@@ -46,7 +49,7 @@ export default class PlatformEnvList extends ScCommand<typeof PlatformEnvList> {
4649
}
4750

4851
// API call
49-
const resp = await conn.get<EnvironmentApiResponse>(apiUrl)
52+
const resp = await conn.get<EnvironmentListApiResponse>(apiUrl)
5053

5154
// Array to output as table
5255
const envArray = [
@@ -57,13 +60,13 @@ export default class PlatformEnvList extends ScCommand<typeof PlatformEnvList> {
5760
item.isDefault,
5861
item.isProduction,
5962
item.description,
60-
])]
63+
]),
64+
]
6165

6266
// Display results as a table
63-
this.log()
64-
this.log(renderTable(envArray, { 4: { width: 50, wrapWord: true } }))
67+
this.log(renderTable(envArray, {4: {width: 50, wrapWord: true}}))
6568

6669
// Return raw json if --json flag is set
67-
return resp.data
70+
return resp
6871
}
6972
}

0 commit comments

Comments
 (0)