|
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' |
2 | 7 |
|
3 | 8 | 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 ]` |
8 | 13 | 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', |
10 | 16 | ] |
11 | 17 | 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.'}), |
16 | 23 | } |
17 | 24 |
|
18 | 25 | 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 | + ] |
20 | 74 |
|
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 | + }, |
25 | 83 | } |
| 84 | + this.log(table(tableRows, config)) |
26 | 85 | } |
27 | 86 | } |
0 commit comments