Skip to content

Commit cf51f52

Browse files
Using Base Command class for platform env commands; abstract table printing
1 parent 48f5365 commit cf51f52

File tree

13 files changed

+168
-108
lines changed

13 files changed

+168
-108
lines changed

README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ Create a new environment.
239239

240240
```
241241
USAGE
242-
$ sc platform env create -n <value> [-d <value>] [--isDefault] [--isProduction]
242+
$ sc platform env create -n <value> [--json] [--log-level debug|warn|error|info|trace] [-d <value>] [--isDefault]
243+
[--isProduction]
243244
244245
FLAGS
245246
-d, --desc=<value> Description of the environment to create.
@@ -250,6 +251,11 @@ FLAGS
250251
architecture can be migrated to a new environment with the desired
251252
environment type instead.
252253
254+
GLOBAL FLAGS
255+
--json Format output as json.
256+
--log-level=<option> [default: info] Specify level for logging.
257+
<options: debug|warn|error|info|trace>
258+
253259
DESCRIPTION
254260
Create a new environment.
255261
@@ -267,12 +273,17 @@ Delete an environment using either its name or unique identifier. The default en
267273

268274
```
269275
USAGE
270-
$ sc platform env delete [-e <value>] [-n <value>]
276+
$ sc platform env delete [--json] [--log-level debug|warn|error|info|trace] [-e <value>] [-n <value>]
271277
272278
FLAGS
273279
-e, --env-id=<value> Id of the environment.
274280
-n, --name=<value> Name of the environment.
275281
282+
GLOBAL FLAGS
283+
--json Format output as json.
284+
--log-level=<option> [default: info] Specify level for logging.
285+
<options: debug|warn|error|info|trace>
286+
276287
DESCRIPTION
277288
Delete an environment using either its name or unique identifier. The default environment cannot be deleted.
278289
@@ -292,20 +303,24 @@ Display information about an Environment.
292303

293304
```
294305
USAGE
295-
$ sc platform env display [-e <value>] [-n <value>]
306+
$ sc platform env display [--json] [--log-level debug|warn|error|info|trace] [-e <value>] [-n <value>]
296307
297308
FLAGS
298309
-e, --env-id=<value> Id of the environment.
299310
-n, --name=<value> Name of the environment.
300311
312+
GLOBAL FLAGS
313+
--json Format output as json.
314+
--log-level=<option> [default: info] Specify level for logging.
315+
<options: debug|warn|error|info|trace>
316+
301317
DESCRIPTION
302318
Display information about an Environment.
303319
304320
Use either the Environment's ID (--env-id) or name of the Environment (--name).
305321
306322
Required token permissions: [ environments:view ]
307323
308-
309324
EXAMPLES
310325
$ sc platform env display --name=MyEnvName
311326
@@ -320,14 +335,20 @@ Get a list of all Environments.
320335

321336
```
322337
USAGE
323-
$ sc platform env list [-n <value>] [--pageNumber <value>] [--pageSize <value>] [--sort <value>]
338+
$ sc platform env list [--json] [--log-level debug|warn|error|info|trace] [-n <value>] [-p <value>] [-s <value>]
339+
[--sort <value>]
324340
325341
FLAGS
326342
-n, --name=<value> Name of the environment to match on.
327-
--pageNumber=<value> The page number to get. Defaults to 10
328-
--pageSize=<value> The number of environments to get per page. Defaults to 1
343+
-p, --pageNumber=<value> The page number to get. Defaults to 10
344+
-s, --pageSize=<value> The number of environments to get per page. Defaults to 1
329345
--sort=<value> The query (fieldName:<ASC/DESC>) used to sort the environment list in the response.
330346
347+
GLOBAL FLAGS
348+
--json Format output as json.
349+
--log-level=<option> [default: info] Specify level for logging.
350+
<options: debug|warn|error|info|trace>
351+
331352
DESCRIPTION
332353
Get a list of all Environments.
333354
@@ -347,7 +368,8 @@ Modify an environment's attributes
347368

348369
```
349370
USAGE
350-
$ sc platform env update [-d <value>] [-e <value>] [--isDefault] [-n <value>] [--new-name <value>]
371+
$ sc platform env update [--json] [--log-level debug|warn|error|info|trace] [-d <value>] [-e <value>] [--isDefault] [-n
372+
<value>] [--new-name <value>]
351373
352374
FLAGS
353375
-d, --desc=<value> Description of the environment to update.
@@ -356,6 +378,11 @@ FLAGS
356378
--isDefault Indicates this is the organization's default environment. The default value is false.
357379
--new-name=<value> New name of the environment.
358380
381+
GLOBAL FLAGS
382+
--json Format output as json.
383+
--log-level=<option> [default: info] Specify level for logging.
384+
<options: debug|warn|error|info|trace>
385+
359386
DESCRIPTION
360387
Modify an environment's attributes
361388

src/commands/platform/env/create.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Command, Flags } from '@oclif/core'
2-
import { table } from 'table'
1+
import { Flags } from '@oclif/core'
32

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

8-
export default class PlatformEnvCreate extends Command {
8+
export default class PlatformEnvCreate extends ScCommand<typeof PlatformEnvCreate> {
99
static override args = {}
1010
static override description = `Create a new environment.
1111
@@ -32,7 +32,7 @@ export default class PlatformEnvCreate extends Command {
3232
}),
3333
}
3434

35-
public async run(): Promise<void> {
35+
public async run(): Promise<Environment> {
3636
const { flags } = await this.parse(PlatformEnvCreate)
3737

3838
const name = flags.name ?? ''
@@ -58,24 +58,17 @@ export default class PlatformEnvCreate extends Command {
5858
// Display results
5959
this.log('Environment created successfully.')
6060
this.print(resp.data)
61+
62+
// Return raw json if --json flag is set
63+
return resp.data
6164
}
6265

6366
private print(environment: Environment): void {
64-
this.log()
6567
const tableRows = [
6668
['Key', 'Value'],
6769
...Object.entries(environment).map(([key, value]) => [camelCaseToTitleCase(key), value]),
6870
]
69-
70-
// Table config
71-
const config = {
72-
columns: {
73-
1: { width: 50, wrapWord: true },
74-
},
75-
drawHorizontalLine(lineIndex: number, rowCount: number) {
76-
return lineIndex === 0 || lineIndex === 1 || lineIndex === rowCount
77-
},
78-
}
79-
this.log(table(tableRows, config))
71+
this.log()
72+
this.log(renderKeyValueTable(tableRows, { 1: { width: 50, wrapWord: true } }))
8073
}
8174
}

src/commands/platform/env/delete.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Command, Flags } from '@oclif/core'
1+
import { Flags } from '@oclif/core'
22

3+
import { ScCommand } from '../../../sc-command.js'
34
import { EnvironmentApiResponse } from '../../../types/environment.js'
45
import { ScConnection } from '../../../util/sc-connection.js'
56

6-
export default class PlatformEnvDelete extends Command {
7+
export default class PlatformEnvDelete extends ScCommand<typeof PlatformEnvDelete> {
78
static override args = {}
89
static override description = `Delete an environment using either its name or unique identifier. The default environment cannot be deleted.
910
@@ -22,7 +23,7 @@ export default class PlatformEnvDelete extends Command {
2223
}),
2324
}
2425

25-
public async run(): Promise<void> {
26+
public async run(): Promise<{ message: string }> {
2627
const { flags } = await this.parse(PlatformEnvDelete)
2728

2829
const name = flags.name ?? ''
@@ -36,20 +37,31 @@ export default class PlatformEnvDelete extends Command {
3637

3738
// If env name provided, get the environment matching provided name and delete. If more than one environment matches, an error will be thrown.
3839
// If env id provided, delete environment with that id
40+
let foundMultiple: boolean = false
3941
if (name) {
4042
// API call to get environment by name
4143
const getEnvApiUrl = `${apiUrl}?name=${name}`
4244
const resp = await conn.get<EnvironmentApiResponse>(getEnvApiUrl)
4345
if (resp.data.length > 1) {
44-
this.error(`Multiple environments found with: ${name}. Exactly one environment must match the provided name.`)
46+
foundMultiple = true
4547
} else {
4648
envIdToDelete = resp.data[0]?.id
4749
}
4850
}
4951

50-
// API call to delete environment by id
51-
apiUrl += `/${envIdToDelete}`
52-
await conn.delete<string>(apiUrl)
53-
this.log(`Environment with id '${envIdToDelete}' has been deleted successfully.`)
52+
let message = ''
53+
if (foundMultiple) {
54+
message = `Multiple environments found with: ${name}. Exactly one environment must match the provided name.`
55+
this.warn(message)
56+
} else {
57+
// API call to delete environment by id
58+
apiUrl += `/${envIdToDelete}`
59+
await conn.delete<string>(apiUrl)
60+
message = `Environment with id '${envIdToDelete}' has been deleted successfully.`
61+
this.log(message)
62+
}
63+
64+
// Return raw json if --json flag is set
65+
return { message }
5466
}
5567
}
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import { Command, Flags } from '@oclif/core'
2-
import { table } from 'table'
1+
import { Flags } from '@oclif/core'
32

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

8-
export default class PlatformEnvDisplay extends Command {
8+
export default class PlatformEnvDisplay extends ScCommand<typeof PlatformEnvDisplay> {
99
static override args = {}
1010
static override description = `Display information about an Environment.
1111
1212
Use either the Environment's ID (--env-id) or name of the Environment (--name).
1313
14-
Required token permissions: [ environments:view ]
15-
`
14+
Required token permissions: [ environments:view ]`
1615
static override examples = ['<%= config.bin %> <%= command.id %> --name=MyEnvName', '<%= config.bin %> <%= command.id %> --env-id=MyEnvId']
1716
static override flags = {
1817
'env-id': Flags.string({
@@ -27,7 +26,7 @@ export default class PlatformEnvDisplay extends Command {
2726
}),
2827
}
2928

30-
public async run(): Promise<void> {
29+
public async run(): Promise<Environment[]> {
3130
const { flags } = await this.parse(PlatformEnvDisplay)
3231

3332
const name = flags.name ?? ''
@@ -39,39 +38,36 @@ export default class PlatformEnvDisplay extends Command {
3938
// If env name provided, get all environments matching provided name
4039
// If env id provided, get environment with that id
4140
let apiUrl: string = `/platform/environments`
41+
let rawResp: Environment[]
4242
if (envId) {
4343
// API call to get environment by id
4444
apiUrl += `/${envId}`
4545
const resp = await conn.get<EnvironmentDetail>(apiUrl)
4646
this.print(resp.data)
47+
rawResp = [resp.data]
4748
} else if (name) {
4849
// API call to get environment by name
4950
apiUrl += `?name=${name}`
5051
const resp = await conn.get<EnvironmentApiResponse>(apiUrl)
5152
for (const env of resp.data) {
5253
this.print(env)
5354
}
55+
56+
rawResp = resp.data
5457
} else {
5558
this.error('Either --env-id or --name must be provided.')
5659
}
60+
61+
// Return raw json if --json flag is set
62+
return rawResp
5763
}
5864

5965
private print(environment: Environment): void {
60-
this.log()
6166
const tableRows = [
6267
['Key', 'Value'],
6368
...Object.entries(environment).map(([key, value]) => [camelCaseToTitleCase(key), value]),
6469
]
65-
66-
// Table config
67-
const config = {
68-
columns: {
69-
1: { width: 50, wrapWord: true },
70-
},
71-
drawHorizontalLine(lineIndex: number, rowCount: number) {
72-
return lineIndex === 0 || lineIndex === 1 || lineIndex === rowCount
73-
},
74-
}
75-
this.log(table(tableRows, config))
70+
this.log()
71+
this.log(renderKeyValueTable(tableRows, { 1: { width: 50, wrapWord: true } }))
7672
}
7773
}

src/commands/platform/env/list.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
import { Command, Flags } from '@oclif/core'
2-
import { table } from 'table'
1+
import { Flags } from '@oclif/core'
32

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

8-
export default class PlatformEnvList extends Command {
8+
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 ]`
1313
static override examples = ['<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> --name=Default --pageNumber=1 --pageSize=10 --sort=name:ASC']
1414
static override flags = {
15-
// flag with a value (-n, --name=VALUE)
16-
name: Flags.string({ char: 'n', description: 'Name of the environment to match on.' }),
17-
// pageNumber (--pageNumber=VALUE)
18-
pageNumber: Flags.integer({ description: 'The page number to get. Defaults to 10' }),
19-
// pageSize (--pageSize=VALUE)
15+
name: Flags.string({
16+
char: 'n',
17+
description: 'Name of the environment to match on.'
18+
}),
19+
pageNumber: Flags.integer({
20+
char: 'p',
21+
description: 'The page number to get. Defaults to 10'
22+
}),
2023
pageSize: Flags.integer({
24+
char: 's',
2125
description: 'The number of environments to get per page. Defaults to 1',
2226
max: 100,
2327
min: 1,
2428
}),
25-
// sort (--sort=VALUE)
2629
sort: Flags.string({
2730
description: 'The query (fieldName:<ASC/DESC>) used to sort the environment list in the response.',
2831
}),
2932
}
3033

31-
public async run(): Promise<void> {
34+
public async run(): Promise<Environment[]> {
3235
const { flags } = await this.parse(PlatformEnvList)
3336

3437
const conn = new ScConnection()
@@ -46,21 +49,21 @@ export default class PlatformEnvList extends Command {
4649
const resp = await conn.get<EnvironmentApiResponse>(apiUrl)
4750

4851
// Array to output as table
49-
const envArray = [['Name', 'Id', 'Is Default', 'Is Production', 'Description'], ...resp.data.map((item: Environment) => [
50-
item.name,
51-
item.id,
52-
item.isDefault,
53-
item.isProduction,
54-
item.description,
55-
])]
52+
const envArray = [
53+
['Name', 'Id', 'Is Default', 'Is Production', 'Description'],
54+
...resp.data.map((item: Environment) => [
55+
item.name,
56+
item.id,
57+
item.isDefault,
58+
item.isProduction,
59+
item.description,
60+
])]
5661

5762
// Display results as a table
58-
const config = {
59-
columns: {
60-
4: { width: 50, wrapWord: true },
61-
},
62-
}
6363
this.log()
64-
this.log(table(envArray, config))
64+
this.log(renderTable(envArray, { 4: { width: 50, wrapWord: true } }))
65+
66+
// Return raw json if --json flag is set
67+
return resp.data
6568
}
6669
}

0 commit comments

Comments
 (0)