Skip to content

Commit 4e23752

Browse files
Update environment command
1 parent 210b9c0 commit 4e23752

File tree

1 file changed

+74
-15
lines changed

1 file changed

+74
-15
lines changed
Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,86 @@
1-
import {Args, Command, Flags} from '@oclif/core'
1+
import {Command, Flags} from '@oclif/core'
2+
import {table} from 'table'
3+
4+
import {Environment, EnvironmentApiResponse, EnvironmentDetail} from '../../../types/environment.js'
5+
import {camelCaseToTitleCase} from '../../../util/internal.js'
6+
import {ScConnection} from '../../../util/sc-connection.js'
27

38
export default class PlatformEnvUpdate extends Command {
4-
static override args = {
5-
file: Args.string({description: 'file to read'}),
6-
}
7-
static override description = 'This command has not been implemented yet. It is a placeholder for future functionality related to updating platform environments.'
9+
static override args = {}
10+
static override description = `Modify an environment's attributes using the environment's name or unique identifier
11+
12+
Token Permissions: [ environments:edit ]`
813
static override examples = [
9-
'<%= config.bin %> <%= command.id %>',
14+
'<%= config.bin %> <%= command.id %> --name=MyEnvName --new-name=MyNewEnvName --desc="Updated description" --isDefault',
15+
'<%= config.bin %> <%= command.id %> --env-id=MyEnvId --new-name=MyNewEnvName --desc="Updated description" --isDefault',
1016
]
1117
static override flags = {
12-
// flag with no value (-f, --force)
13-
force: Flags.boolean({char: 'f'}),
14-
// flag with a value (-n, --name=VALUE)
15-
name: Flags.string({char: 'n', description: 'name to print'}),
18+
desc: Flags.string({char: 'd', description: 'Description of the environment to update.'}),
19+
'env-id': Flags.string({char: 'e', description: 'Id of the environment.', exactlyOne: ['env-id', 'name']}),
20+
isDefault: Flags.boolean({description: 'Indicates this is the organization’s default environment.'}),
21+
name: Flags.string({char: 'n', description: 'Existing name of the environment.', exactlyOne: ['env-id', 'name']}),
22+
'new-name': Flags.string({description: 'New name of the environment.'}),
1623
}
1724

1825
public async run(): Promise<void> {
19-
const {args, flags} = await this.parse(PlatformEnvUpdate)
26+
const {flags} = await this.parse(PlatformEnvUpdate)
27+
28+
const name = flags.name ?? ''
29+
const newName = flags['new-name'] ?? ''
30+
const envId = flags['env-id'] ?? ''
31+
const desc = flags.desc ?? ''
32+
const isDefault = flags.isDefault ?? false
33+
34+
const conn = new ScConnection()
35+
36+
// API url
37+
let apiUrl: string = `/platform/environments`
38+
// API body
39+
const body = {
40+
description: desc,
41+
isDefault,
42+
newName,
43+
}
44+
45+
let envIdToUpdate: string | undefined = envId
46+
47+
// If env name provided, get all environments matching provided name
48+
if (name) {
49+
// API call to get environment by name
50+
apiUrl += `?name=${name}`
51+
const resp = await conn.get<EnvironmentApiResponse>(apiUrl)
52+
if (resp.data.length > 1) {
53+
this.error(`Multiple environments found with: ${name}. Exactly one environment must match the provided name.`)
54+
} else {
55+
envIdToUpdate = resp.data[0]?.id
56+
}
57+
} else {
58+
this.error('Either --env-id or --name must be provided.')
59+
}
60+
61+
// API call to update environment by id
62+
apiUrl += `/${envIdToUpdate}`
63+
const resp = await conn.put<EnvironmentDetail>(apiUrl, body)
64+
this.log(`Environment with id '${envIdToUpdate}' has been updated successfully.`)
65+
this.print(resp.data)
66+
}
67+
68+
private print(environment: Environment): void {
69+
this.log()
70+
const tableRows = [
71+
['Key', 'Value'],
72+
...Object.entries(environment).map(([key, value]) => [camelCaseToTitleCase(key), value]),
73+
]
2074

21-
const name = flags.name ?? 'world'
22-
this.log(`hello ${name} from /Users/dishantlangayan/Dev/solace-cloud-cli/src/commands/platform/env/update.ts`)
23-
if (args.file && flags.force) {
24-
this.log(`you input --force and --file: ${args.file}`)
75+
// Table config
76+
const config = {
77+
columns: {
78+
1: {width: 50, wrapWord: true},
79+
},
80+
drawHorizontalLine(lineIndex: number, rowCount: number) {
81+
return lineIndex === 0 || lineIndex === 1 || lineIndex === rowCount
82+
},
2583
}
84+
this.log(table(tableRows, config))
2685
}
2786
}

0 commit comments

Comments
 (0)